Jump to content

Displaying multiple error messages


Rubendo
Go to solution Solved by cyberRobot,

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;
    }
}
Edited by cyberRobot
Changed title from "php php php php"; Added [code][/code] tags
Link to comment
Share on other sites

  • Solution

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.