Looping form errors

I have a form-is used so that the user can update its password:

 <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:

if($new!=$retypednew)
         {$passerrors['notsame']='passwords do not much...';
         return $passerrors;
         }

Then I use a foreach loop to loop through the errors.

 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.

Use an associative error array where the key is the input’s name or id:

<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.

You should not loop through the array, instead you use them something like this just just after each relevant inputs:


<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.

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:

if(code to check current = true) {
   $error['current'] = "Current is in error";
} else {
   $error['current'] = "";
}

or before the error checking:

$error['current'] = "";
$error['other'] = "";
$error['something'] = "";

or before outputting the form:

if(!isset($error['current'])) { $error['current'] = ""; }
if(!isset($error['other'])) { $error['other'] = ""; }
if(!isset($error['something'])) { $error['something'] = ""; }

Υ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?

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.