#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Location
    São Paulo - Brazil
    Posts
    35
    Rep Power
    2

    How to show a message in place of a wrong value?


    I'm a PHP novice.

    Suppose I receive the value a variable from a html form via POST method.

    Suppose the value received is, for example, greater than the maximum allowed. I would like to know if is there a way to format an error message and exhibit this message in the same text box where the user types the wrong value. The screen remains the same, the only alteration is the message showed in place of the value typed by the user.

    Greetings from São Paulo - Brazil

    Ricardo
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,465
    Rep Power
    594
    This is normally done on the client side with Javascript. Making the user wait for the server to get an error is not user friendly. However, if you want to do this on the server side (PHP) then simply use that error as the 'value' for that particular field for that <input> tag when you reissue the page.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    12
    It's pretty straight foward to do something like this:

    PHP Code:
    <?php
    // check if we're receiving data from form
    if( !empty( $_POST['email'] ) ) {
        
    // check that email is valid
        
    if( is_email_valid$_POST['email'] ) ) {
            
    //set an error message
            
    $errors['email'] = 'Invalid email';
        }
        
    }

    ?>
    <form>
        <!-- Check if there is an error message for and display error message -->
        <input type="text" name="email" /> <?php echo !empty( $errors['email'] ) ? $errors['email'] : '' ?>
    </form>
    I would also chime in that, validating on the front-end (like using javascript) is great for the user, it's also important that your backend also validates. Validating just on the server side is pretty standard.

IMN logo majestic logo threadwatch logo seochat tools logo