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

    Join Date
    Oct 2012
    São Paulo - Brazil
    Rep Power

    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

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Rep Power
    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
    Seattle, U.S.A.
    Rep Power
    It's pretty straight foward to do something like this:

    PHP Code:
    // 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';

        <!-- Check if there is an error message for and display error message -->
        <input type="text" name="email" /> <?php echo !empty( $errors['email'] ) ? $errors['email'] : '' ?>
    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