Rubendo Posted September 14, 2017 Share Posted September 14, 2017 hi i need that when i putt submit empty message will writen in each field if empty firstname write down firstname, thank you if (isset($_POST['submit'])) { function validate($key, $value) { $error = false; switch ($key) { case 'firstname': if (empty($value)) { $error = 'Putt Your Firstname'; } break; case 'lastname' : if (empty($value)) { $error = 'Putt Your Lastname'; } break; case 'email': if (empty($value)) { $error = 'Putt Your Email'; } break; case 'pass' : if (empty($value)) { $error = 'Putt Your Password'; } break; case 'confirmpass' : if (empty($value)) { $error = 'Putt Your Confirmpassword '; } } return $error; } // $arr = array('firstname', 'lastname', 'email', 'pass', 'confirmpass'); foreach ($_POST as $key => $value) { $error = validate($key, $value); // echo $error; } } Link to comment Share on other sites More sharing options...
Barand Posted September 14, 2017 Share Posted September 14, 2017 Use html placeholder attributes. Give your topics meaningful titles. Use code tags when posting code. Link to comment Share on other sites More sharing options...
Rubendo Posted September 14, 2017 Author Share Posted September 14, 2017 bro i m not using msql if it for you not difficult write please Link to comment Share on other sites More sharing options...
Jacques1 Posted September 14, 2017 Share Posted September 14, 2017 bro i m not using msql You're talking to a signature, brah. Link to comment Share on other sites More sharing options...
Barand Posted September 14, 2017 Share Posted September 14, 2017 Put the error messages in an array. At the moment, if there is more than one error, only the last is returned. And who said anything about sql? Link to comment Share on other sites More sharing options...
Rubendo Posted September 14, 2017 Author Share Posted September 14, 2017 Barand i want to put in div firstname <?php echo "$errors" ?> but this $errors is not difined Link to comment Share on other sites More sharing options...
Barand Posted September 14, 2017 Share Posted September 14, 2017 Your variable is $error, not $errors Link to comment Share on other sites More sharing options...
Rubendo Posted September 14, 2017 Author Share Posted September 14, 2017 yes its word wrong but how can i put ander field ?? my function returned $error and then i do $error = function "for axample dod" but then what can i do Link to comment Share on other sites More sharing options...
cyberRobot Posted September 14, 2017 Share Posted September 14, 2017 You can do something like this: <?php if (isset($_POST['submit'])) { function validate($key, $value) { $error = array(); switch ($key) { case 'firstname': $error[$key] = (empty($value)) ? 'Putt Your Firstname' : ''; break; case 'lastname' : $error[$key] = (empty($value)) ? 'Putt Your Lastname' : ''; break; //... } return $error; } // $arr = array('firstname', 'lastname', 'email', 'pass', 'confirmpass'); foreach ($_POST as $key => $value) { $error = validate($key, $value); echo $error[$key]; } } ?> The above code uses the ternary operator. More information can be found here: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary Side note: Put is spelled with one (1) "t". "Putt" is something else. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.