Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts

    applying errors to inputs

    Good morning,

    I'm looking to process a form with PHP and usually put all the errors either above or below the form altogether for the user.

    I want to be able to put the individual errors next to the relative form input but not sure how to do this.

    My code so far is:
    PHP Code:
    <?php
    if(isset($_POST['fname'])){    
         
    $errors = array();     
         
    $required = array($_POST['fname'], $_POST['lname'], $_POST['dob'], $_POST['mphone'], $_POST['wphone'], $_POST['address'], $_POST['city'], $_POST['province'], $_POST['ages']);
         
         foreach(
    $required as $k => &$v)  {         
              
    $v trim($v);
               if(
    strlen($v) == 0)  {       
                   
    array_push($errors"true");         
               }         
               else         
               {              
                   
    array_push($errors"false");         
               }     
          }          
          
          if(
    in_array("true"$errors))     
          {          
                
    $_SESSION['msg'] = $errors;         
                
    header("Location: Register-Now.php");          
                die();     
          }         
    }else
    {     
          
    //someone has tried to directly access the form script without a submit, send them back!     
          
    header("Location: Register-Now.php");     
          die();
    }
    ?>
    I'm sure whether I'm having a total brain malfunction or what.

    I can do this with JavaSript/jQuery no problem but not sure how to do this with PHP?

    Thank you for your time.

    Kind regards,
    Tom
    Last edited by LearningCoder; 05-06-2014 at 10:18 AM.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,331
    Thanks
    13
    Thanked 348 Times in 344 Posts
    you need to do that in your form building script by adding the appropriate HTML code to the affected form control.

    PS. templates do make that easier.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    i think you can just alter your required string slightly,

    PHP Code:
    if(isset($_POST['fname'])){    
         
    $_SESSION['errors'] = array();     
         
    $required = array('fname' => $_POST['fname'], 'lname' => $_POST['lname'], 'dob' => $_POST['dob'], 'mphone' => $_POST['mphone'], 'fname' => $_POST['fname'], 'address' => $_POST['address'], 'city' => $_POST['city'], 'province' => $_POST['province'], 'ages' => $_POST['ages']);
         
         foreach(
    $required as $k => &$v)  {         
              
    $v trim($v);
               if(
    strlen($v) == 0)  {       
                  
    $_SESSION['errors'][$$k] = "true";      
                  
    $_SESSION['errors']['fail'] = "true";   
               }         
               else         
               {              
                   
    $_SESSION['errors'][$$k] = "false";         
               }     
          }           
          
          if(
    $_SESSION['errors']['fail'] == "true")
          {                
                
    header("Location: Register-Now.php");          
                die();     
          }         
    }else
    {     
          
    //someone has tried to directly access the form script without a submit, send them back!     
          
    header("Location: Register-Now.php");     
          die();

    this will set $_SESSION['errors']['fname'] etc to either true or false,

    then simply check that on the form and highlight accordingly
    Last edited by Arcticwarrio; 05-07-2014 at 12:35 PM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    also i dont like to use editable for fields for post checking
    <input type="hidden" name="formsent" value="yes">

    if(isset($_POST['fname'])){

    if(isset($_POST['formsent'])){

    maybe something like
    PHP Code:
    echo '<input type="text" name="fname">';
    if(
    $_SESSION['errors']['fname'] == "true"){echo '* First Name Required';} 
    etc
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •