SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2013
    Location
    Greece
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    looping form errors

    I have a form-is used so that the user can update its password:
    Code:
     <label class="label" for="current">current</label><?php echo  $error ?> 
                              <input class="service" size="40"  type="text" name="current"><br>
                              <label class="label" for="new">New</label>
                              <input class="price" size="3"type="text" name="new"><br>
                              <label class="label" for="retyped">retyped</label>
                              <input class="price" size="3"type="text" name="retypednew"><br>
    I have made a function where all the checks are made (no reason to show the code here)-the error messages are stored to an array and returned from the function:
    Code:
    if($new!=$retypednew)
             {$passerrors['notsame']='passwords do not much...';
             return $passerrors;
             }
    Then I use a foreach loop to loop through the errors.
    Code:
     foreach ($passerrors as $error) {
                          ?>  <label class="label" for="current">current</label>
                              <input class="service" size="40"  type="text" name="current"><br>
                              <label class="label" for="new">new</label>
                              <input class="price" size="3"type="text" name="new"><br>
                              <label class="label" for="retyped">retyped"</label>
                              <input class="price" size="3"type="text" name="retypednew"><br>
              
                              <?php  
                          }?>
    What I want is that errors looped appear aside the input elements where a specific error appears
    What code should a write(inside the one above) that for example the error that is related with the passwords not being the same appear aside the relevant input elements.

    I have tried a lot various things but without success.

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Use an associative error array where the key is the input's name or id:

    HTML Code:
    <label class="label" for="current">current</label><?php echo  $error['current'] ?>
    Denny Schlesinger
    web services

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2013
    Location
    Greece
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by captainccs View Post
    Use an associative error array where the key is the input's name or id:

    HTML Code:
    <label class="label" for="current">current</label><?php echo  $error['current'] ?>
    Yes...that method works OK but with a catch.
    If an error is not returned from the function then I get the usual "not defined index" error message.
    Here is a code example:

    $passerrors=update_password($_POST,$_SESSION['valid_user']);
    // var_dump($passerrors);
    ?>
    <label class="label" for="current">current</label><?php echo $passerrors['current'] ?>
    <input class="service" id="service" size="40" type="text" name="current"><br>
    I abandoned the foreach loop completely.
    update_password() makes the checks and return the array of errors but if IT DOES not return the error associated with the current input element as seen above
    than I get the error I describe above.

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by designtrooper View Post
    Yes...that method works OK but with a catch.
    If an error is not returned from the function then I get the usual "not defined index" error message.
    Here is a code example:

    $passerrors=update_password($_POST,$_SESSION['valid_user']);
    // var_dump($passerrors);
    ?>
    <label class="label" for="current">current</label><?php echo $passerrors['current'] ?>
    <input class="service" id="service" size="40" type="text" name="current"><br>
    I abandoned the foreach loop completely.
    update_password() makes the checks and return the array of errors but if IT DOES not return the error associated with the current input element as seen above
    than I get the error I describe above.
    You should not loop through the array, instead you use them something like this just just after each relevant inputs:

    Code php:
    <label class="label" for="current">current</label><?php echo  $error ?> 
    <input class="service" size="40"  type="text" name="current"><br>
    <?php echo isset($passerrors['current']) ? $passerrors['current'] . '<br />' : ""; ?>
    <label class="label" for="new">New</label>
     <input class="price" size="3"type="text" name="new"><br>
    <?php echo isset($passerrors['new']) ? $passerrors['new'] . '<br />' : ""; ?>
    <label class="label" for="retyped">retyped</label>
    <input class="price" size="3"type="text" name="retypednew"><br>

    Hope you understand what I mean ! Let us know if you still don't understand.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There are at least three ways of initializing the error array which make for cleaner code. Either add an else clause to each error check:
    PHP Code:
    if(code to check current true) {
       
    $error['current'] = "Current is in error";
    } else {
       
    $error['current'] = "";

    or before the error checking:
    PHP Code:
    $error['current'] = "";
    $error['other'] = "";
    $error['something'] = ""
    or before outputting the form:
    PHP Code:
    if(!isset($error['current'])) { $error['current'] = ""; }
    if(!isset(
    $error['other'])) { $error['other'] = ""; }
    if(!isset(
    $error['something'])) { $error['something'] = ""; } 
    Denny Schlesinger
    web services

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2013
    Location
    Greece
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raju Gautam View Post
    You should not loop through the array, instead you use them something like this just just after each relevant inputs:

    Code php:
    <label class="label" for="current">current</label><?php echo  $error ?> 
    <input class="service" size="40"  type="text" name="current"><br>
    <?php echo isset($passerrors['current']) ? $passerrors['current'] . '<br />' : ""; ?>
    <label class="label" for="new">New</label>
     <input class="price" size="3"type="text" name="new"><br>
    <?php echo isset($passerrors['new']) ? $passerrors['new'] . '<br />' : ""; ?>
    <label class="label" for="retyped">retyped</label>
    <input class="price" size="3"type="text" name="retypednew"><br>

    Hope you understand what I mean ! Let us know if you still don't understand.
    Υes I understand now, thanks...I used your approach-the ternary operator.
    Here is it
    <?php echo isset($passerrors['current'])? $passerrors['current']:false ;?><br>
    The only thing-and for which I want to make a question- is that instead of an empty string I set to false is the error is not returned.

    Do you think there is anything wrong to it?

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by designtrooper View Post
    is that instead of an empty string I set to false is the error is not returned.

    Do you think there is anything wrong to it?
    That's fine I think, there should not be problem with it. I would suggest you to use one more check with empty() function along with isset() which will check the false as well.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

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