subject

I am unable to check if my $check array is empty or contains information passed via post from a checkbox group.

if ($_POST) {
if ($error_array = validate_form() ) {
show_form ($error_array);
}
else {
process_form( );
}
}
else {
show_form ( );
}

function show_form($errors = []) {
include ("survey_inc. php");
}

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);

return $data;
}

// valid name, valid email, not empty check box, and valid radio and not empty.
function validate_form() {
$errors = []; // start with empty array
$name = test_input($_POST['name']);
$email = test_input($_POST['email']);
$check = test_input($_POST['bucket']);
$radio = test_input($_POST['species']);

// checking to see if name is 2+ characters
if (strlen($name) < 2) {
$errors[] = "Name is too short or empty.";
}

// check email
if (! filter_var($email, FILTER_VALIDATE_EMAIL) ){
$errors[] = "Enter a valid email.";
}

// check check group
if (empty($check)) {
$errors[] = "Please select a Bucket list item, even if it's not on your list.";
}
/*if (!empty($check)) {
switch ($check) {
case "skydiving":
break;
case "rocky_mountain_climbing":
break;
case "bull_riding":
break;
default:
$errors[] = "Please select a bucket list item...from the list.";
}
}
else {
$errors[] = "Please select a bucket list item...even if it isn't one on your list!";
}
if (!empty($check)) {
if (in_array($check, $bucket_arr)) {
$errors[] = "Please select a bucket list item...from the list.";
}
} else {
$errors[] = "Please select a bucket list item...even if it isn't one on your list!";
}*/

//check radio item
/*
if (!empty($radio)) {
switch ($radio) {
case "yes":
break;
case "no":
break;
case "other":
break;
default:
$errors[] = "Please select your species...from the list.";
}
}
else {
$errors[] = "Please select your species...even if you lie!";
}
*/
if (!empty($radio)) {
if (in_array($radio, $species_arr)) {
$errors[] = "Please select your species...from the list.";
}
} else {
$errors[] = "Please select your species...even if you lie!";
}

return $errors;
}

function process_form() {
$name = test_input($_POST['name']);
$email = test_input($_POST['email']);
$check = test_input($_POST['bucket']);
$radio = test_input($_POST['species']);

echo $name."\n", $email."\n", $check."\n", $radio."\n";
}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 15:10
Chiko bought 75 shares of stock at $19.58 per share. he received dividends of $73.42 during the year. at the end of the year, his stock was valued at $22.14. what was his purchase price?
Answers: 2
question
Computers and Technology, 23.06.2019 03:00
Your business be in google top rank.more the rank more the business leads.for best seo and digital marketing services be confident to contact you can get best seo solutions by assistance experts provide digital marketing, website development, seo expert services and social media internet seo expert services your branding solutions. seo expert services ,best seo expert services,online seo expert services,
Answers: 3
question
Computers and Technology, 23.06.2019 13:30
What is the primary difference between the header section of a document and the body? a. the body is displayed on the webpage and the header is not. b. the header is displayed on the webpage and the body is not. c. the tag for the body is self-closing, but the tags for the headers must be closed. d. the tag for the header is self closing, but the tag for the body must be closed.
Answers: 3
question
Computers and Technology, 23.06.2019 17:00
1. which of the following is not an example of an objective question? a. multiple choice. b. essay. c. true/false. d. matching 2. why is it important to recognize the key word in the essay question? a. it will provide the answer to the essay. b. it will show you a friend's answer. c. it will provide you time to look for the answer. d. it will guide you on which kind of answer is required.
Answers: 1
You know the right answer?
I am unable to check if my $check array is empty or contains information passed via post from a chec...
Questions
question
History, 11.09.2019 18:30
Questions on the website: 13722363