Rubendo Posted September 14, 2017 Share Posted September 14, 2017 (edited) 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; } } Edited September 14, 2017 by cyberRobot Changed title from "php php php php"; Added [code][/code] tags Quote 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. Quote 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 Quote 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. Quote 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? Quote 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 Quote 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 Quote 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 Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted September 14, 2017 Solution 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.