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 13:30
In mmf2, what is the grid? a. a toolbar that lets you open libraries and use the objects in them in your games b. an object that creates the background or other scenery for a game c. a set of vertical and horizontal lines that are usually invisible in the play area d. a set of vertical and horizontal lines that are usually visible in the play area
Answers: 1
question
Computers and Technology, 22.06.2019 19:20
1)consider the following code snippet: #ifndef book_h#define book_hconst double max_cost = 1000.0; class book{public: book(); book(double new_cost); void set_cost(double new_cost); double get_cost() const; private: double cost; }; double calculate_terms(book bk); #endifwhich of the following is correct? a)the header file is correct as given.b)the definition of max_cost should be removed since header files should not contain constants.c)the definition of book should be removed since header files should not contain class definitions.d)the body of the calculate_terms function should be added to the header file.
Answers: 1
question
Computers and Technology, 23.06.2019 11:20
Http is the protocol that governs communications between web servers and web clients (i.e. browsers). part of the protocol includes a status code returned by the server to tell the browser the status of its most recent page request. some of the codes and their meanings are listed below: 200, ok (fulfilled)403, forbidden404, not found500, server errorgiven an int variable status, write a switch statement that prints out the appropriate label from the above list based on status.
Answers: 2
question
Computers and Technology, 23.06.2019 13:30
Jace needs to answer a question on square roots to win a quiz. how can he use a spreadsheet to find the square root of 786? a. use the functions round and count b. create a table and chart c. use the function sqrt d. use the function now
Answers: 3
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
Mathematics, 04.02.2020 00:12
Questions on the website: 13722360