Jump to content

Displaying multiple error messages


Rubendo

Recommended Posts

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

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.  :happy-04:

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.