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..."; } }
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.
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.
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.
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.
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..."; }
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With