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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 15:30, micahpauleen748
In a compound condition, both conditions on either side of the logical operator and must be true for the overall condition to be true. a: true b: false
Answers: 1
image
Computers and Technology, 23.06.2019 15:00, puppylove899
Barbara is interested in pursuing a career in the science and math pathway. which qualifications will her reach that goal? a. an advanced knowledge of physics and math b. an advanced knowledge of engineering and math c. an advanced knowledge of physics and robotics an d. advanced knowledge of machinery and math
Answers: 1
image
Computers and Technology, 23.06.2019 19:30, sallybob0
What are loans to a company or government for a set amount of time
Answers: 1
image
Computers and Technology, 24.06.2019 04:30, NEUROPHARMACOLOGICAL
The ieee 802.11: defines standards for wireless local area network (wlan) communication protocols. identifies various computers or devices connected to a network. verifies any resource attached to another computer on a network that is different from the computer to which the user is logged on. connects multiple local area networks (lans) and wide area networks (wans).
Answers: 2
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 in other subjects:

Konu
Mathematics, 29.01.2020 03:53
Konu
Mathematics, 29.01.2020 03:53
Konu
Advanced Placement (AP), 29.01.2020 03:53