Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: check if any posted vars are empty - form: all fields required

Is there a simpler function to something like this:

if (isset($_POST['Submit'])) {     if ($_POST['login'] == "" || $_POST['password'] == "" || $_POST['confirm'] == "" || $_POST['name'] == "" || $_POST['phone'] == "" || $_POST['email'] == "") {         echo "error: all fields are required";     } else {         echo "proceed...";     } } 
like image 704
FFish Avatar asked Jul 06 '10 21:07

FFish


People also ask

How do I check if a field is empty in PHP?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true. The following values evaluates to empty: 0.

Which function is used to check if post variable is empty?

Empty() Function This function is used to check if a variable is empty or not. This function is of the boolean return type. The empty() function will return false if the variable is not empty and contains a value that is not equivalent to zero, otherwise, it will return true.

Does Isset check for empty?

The isset() function is an inbuilt function in PHP that is used to determine if the variable is declared and its value is not equal to NULL. The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not.

How can handling empty form fields?

PHP - Handling Empty Form Fields Users often forget to (or don't want to) fill in certain fields in a form. When this happens, some data is not sent to the server. Sometimes the field is sent as an empty string; sometimes no field name is sent at all. The field name is sent, along with an empty value.


2 Answers

Something like this:

// Required field names $required = array('login', 'password', 'confirm', 'name', 'phone', 'email');  // Loop over field names, make sure each one exists and is not empty $error = false; foreach($required as $field) {   if (empty($_POST[$field])) {     $error = true;   } }  if ($error) {   echo "All fields are required."; } else {   echo "Proceed..."; } 
like image 97
Harold1983- Avatar answered Oct 01 '22 00:10

Harold1983-


if( isset( $_POST['login'] ) &&  strlen( $_POST['login'] )) {   // valid $_POST['login'] is set and its value is greater than zero } else {   //error either $_POST['login'] is not set or $_POST['login'] is empty form field } 
like image 43
Nahser Bakht Avatar answered Oct 01 '22 00:10

Nahser Bakht