Oct 28th, 2011, 1:43 PM
(This post was last modified: Oct 28th, 2011, 2:29 PM by michatmaster7.)
I searched the forums on this one and couldn't find a good answer.
I have been testing a new form with .php and using the PHP validation rules and so far, so good. I even got all of my fields setup to refill when there is an error.
But now it's about how to best display the error messages. I'm sure there is some simple tweak to the PHP script from your tutorial to do this, but I'd like to have each field's error message be displayed next to the corresponding field. For example, rather than having one list of errors somewhere in the page, I'd like the error to display next to the field it corresponds to.
Is there a way to tweak the code to this? I imagine I'd need some kind of if-else statement for every single field in my form, but I'd be ok with that.
PS - I have somewhat limited knowledge of PHP, but I think I can follow terminology.
Thanks in advance!
Brandon
I think if I could tweak this code so that it only called one specific error, then I could paste the tweaked code next to each field and change the code to reflect THAT field, right?
I have been testing a new form with .php and using the PHP validation rules and so far, so good. I even got all of my fields setup to refill when there is an error.
But now it's about how to best display the error messages. I'm sure there is some simple tweak to the PHP script from your tutorial to do this, but I'd like to have each field's error message be displayed next to the corresponding field. For example, rather than having one list of errors somewhere in the page, I'd like the error to display next to the field it corresponds to.
Is there a way to tweak the code to this? I imagine I'd need some kind of if-else statement for every single field in my form, but I'd be ok with that.
PS - I have somewhat limited knowledge of PHP, but I think I can follow terminology.
Thanks in advance!
Brandon
I think if I could tweak this code so that it only called one specific error, then I could paste the tweaked code next to each field and change the code to reflect THAT field, right?
Code:
<?php
// if $errors is not empty, the form must have failed one or more validation
// tests. Loop through each and display them on the page for the user
if (!empty($errors))
{
echo "<div class='error'>Please fix the following errors:\n<ul>";
foreach ($errors as $error)
echo "<li>$error</li>\n";
echo "</ul></div>";
}
?>