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

    Join Date
    Dec 2012
    Posts
    9
    Rep Power
    0

    PHP Form Validation Input Error


    Hello,

    I'm fairly new to PHP and after a tutorial, I have the following PHP form validation code:

    PHP Code:
    if(isset($_POST['name'], $_POST['email'], $_POST['phone'], $_POST['message'])){
    $errors = array();
        
    if(empty(
    $name) || empty($email) || empty($phone) || empty($message))
    {
    $errors[] = 'All fields are required.';}
    else{

    if(
    strlen($name) < 3)
    {
    $errors[] = 'Name is too short.';}

    if (!
    preg_match("/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/"$phone))
    {
    $errors[] = 'Please specify a valid phone number.';}

    if(
    filter_var($emailFILTER_VALIDATE_EMAIL) == FALSE)
    {
    $errors[] = 'Please specify a valid email address.';}
    }

    if(!empty(
    $errors))
    {foreach(
    $errors as $error){
       echo 
    $error;
    }
    }
    else{
    mail($emailTo$emailSubject$body$headers);
    }

    I'm trying to get the error messages to display next to the input fields on my form, and not on the top of the form. Any assistance would be helpful. Thank you.

    HTML Input Excerpt:
    <form id="form" method="post" action="">
    <fieldset>
    <legend>Contact Info</legend>
    <label>Your Name<span class="red">*</span></label>
    <input type="text" name="name" id="name" class="required letters" value="<?php echo $_POST['name']; ?>"/>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,337
    Rep Power
    594
    Rather than sequentially push the errors onto the array, use an associative array where the key is the field next to which you want it to display. Then when you redisplay the form, you use the key to determine if that particular error 'isset' and display it.

    A word to the wise concerning validation. IMO, it is user unfriendly to do validation in PHP alone. Validation should be done on the client via Javascript first. That way a user does not have to wait for the server to see an entry error and enter everything a 2nd time, just correct the erroneous field. The PHP validate should be done just to stop those trying to get around your validation in an attempt to hack your web site or damage your database. I have no sympathy for them and they can enter everything again from scratch.

    Comments on this post

    • richpri agrees : Save PHP validation for stuff that can only be done on a server like stuff needing a database access.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    I disagree with the statement that PHP validation isn't user friendly. If you simply fill out every field with the previous value, there's no problem at all.

    Of course you can add "live validation" for extra convenience. But this means you have to do write the validation code twice (once in JavaScript and once in PHP). JavaScript validation alone does not work, because it can simply be turned off or circumvented (which has nothing to do with "hacking").

    So I'd just keep the current solution and simply change the array type as suggested.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,337
    Rep Power
    594
    To me, it is very annoying to submit a form, wait on a slow server or network only to find out that there was an entry error. I hate those and generally won't use them because a programmer is too lazy to validate with Jatascript.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    88
    Rep Power
    10
    Users can turn Javascript off, you have to have a server side validation
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Yeah, we already knew that four months ago.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    88
    Rep Power
    10
    You would have to put an echo statement outside <input />

    Code:
    <input  />
    
    
    <div class="someclassnameforstyling>
    <?php echo $error ?>
    
    </div
    Where $error is the php string variable for your error message

    Comments on this post

    • Jacques1 disagrees : What's the matter with you? Why do you keep posting in a thread from last year?

IMN logo majestic logo threadwatch logo seochat tools logo