Jump to content

2 forms 1 page error processing


countrygyrl

Recommended Posts

I have a page that I have two forms on, both forms have error processing, I am banging my head against the wall and I need some fresh eyes. It's probably something simple but I can't wrap my head around it.  What I need is an if statement that says only show the num_errors and the errors themselves if this form has been submitted or something?

 

<?
if($form->num_errors > 0){
   echo "<p><span class=\"error\">".$form->num_errors." error(s) found</span><br />";
   echo "<p><span class=\"error\">".$form->error("user2")."</span><br />";
   echo "<span class=\"error\">".$form->error("pass2")."</span></p>";
}
?>
<form action="process.php" method="POST">
<p><label for="user2">Username:</label><input type="text" name="user2" maxlength="30" size="10" value="<? echo $form->value("user2"); ?>" /></p>
<p><label for="pass2">Password:</label><input type="password" name="pass2" maxlength="30" size="10" value="<? echo $form->value("pass2"); ?>" /></p>
<p><input type="checkbox" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?> />
Remember me 
<input type="hidden" name="sublogin" value="1" />
<input type="submit" value="Login" /></p>
<p class="legal">[<a href="forgotpass.php">Forgot Password?</a>]</p>
</form>

Link to comment
https://forums.phpfreaks.com/topic/202330-2-forms-1-page-error-processing/
Share on other sites

I usually stick all errors into an array and then do something along the lines of:

 

if (count($errorArray)) {
foreach ($errorArray as $key => $value):
HTML here.
endforeach;

 

Works like a charm for me, and it'd seemingly allow you to do what you want with multiple forms without hassle.

Unfortunately, that won't work for me because the error messages for the two different forms show up in two different areas of the page. I am looking for something like this, but of course this doesn't work lol

 

if (sublogin == 1) {
if($form->num_errors > 0){
   echo "<p><span class=\"error\">".$form->num_errors." error(s) found</span><br />";
   echo "<p><span class=\"error\">".$form->error("user2")."</span><br />";
   echo "<span class=\"error\">".$form->error("pass2")."</span></p>";
}
}

Ok I got it, I gave a value to a session variable if there were errors on that particular form and only ran the php error display code if that value was equal to 1 and then unset the session variable as soon as the code was displayed, thanks for your help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.