I've got a contact us form on a generated php page, so if the user completes and does mistakes it will advise on the next page...(this works fine) BUT, when I ask them to click back the data is gone. (I understand this) So how do I get it so when the next page is shown, it says "Form submitted, thank you" OR it shows the form again with the errors to correct and of course stays on this page until the serrors are corrected. Thanks, Ian
Set up your form to set the value tag of your fields to be the variable you are passing them as. That way if they aren't set, they will be blank, if they are set then it will display it as the default.
OK, I'm cooking with gas (It's working - but only 90%)!! My validation isn't working 100%, it only check 1 and not both....I think I've messed up my IF's Anyone help? Ian <?php ini_set ('sendmail_from', "myemail@domain.com"); $email = 'myemail@domain.com'; $subject = $HTTP_POST_VARS['subject']; $message = $HTTP_POST_VARS['message']; $from = $HTTP_POST_VARS['from']; //persons email address $name = $HTTP_POST_VARS['name']; $from_check = $HTTP_POST_VARS['from_check']; $message = "Additional Requests: ".$message; $message .= "\nName: ".$name; $message .= "\nEmail address 2 (could be different): ".$from_check; if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $from)) { echo "Your email address is not valid"; if ($from <> $from_check) echo "different emails…try again"; ?> <form name="form" method="post" action="http://www.domain.com/mail/1.php"> MY FORM IS HERE ALL WORKING FINE </form> <?php } elseif (mail($email, $subject, $from, $message)){ echo $name; echo "Thank you"; } else { echo "Sorry we have a problem, try again or send an email to: email@domain.com"; echo "Thank you"; } ?> Code (markup):
I think the problem is the bare html in the middle of the if-else statement. I rewrote it a little.. See if you can use this: <?php ini_set ('sendmail_from', "myemail@domain.com"); $form = '<form name="form" method="post" action="http://www.domain.com/mail/1.php">MY FORM IS HERE ALL WORKING FINE</form>'; $email = 'myemail@domain.com'; $subject = $HTTP_POST_VARS['subject']; $message = $HTTP_POST_VARS['message']; $from = $HTTP_POST_VARS['from']; //persons email address $name = $HTTP_POST_VARS['name']; $from_check = $HTTP_POST_VARS['from_check']; $message = "Additional Requests: ".$message; $message .= "\nName: ".$name; $message .= "\nEmail address 2 (could be different): ".$from_check; if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $from)) { echo "Your email address is not valid"; } if ($from <> $from_check) { echo "different emails! Try again"; } elseif { (mail($email, $subject, $from, $message)); echo $name; echo "Thank you"; } else { echo "Sorry we have a problem, try again or send an email to: email@domain.com"; echo "Thank you"; } ?> Code (markup): (You can echo $form where you want your form to appear.)
Here's the logic I use with forms and validations. Validation and forms are in the same file (or included). Validation failed errors are assigned to an $errors array. if ( user submitted form ) validate input if ( !valid email1) $errors[] = invalid email1 if ( !valid email2) $errors[] = invalid email2 if ( email1 != email2 ) $errors[] = emails do not match. repeat for password, address, zip, etc. all input you accept from a user should be validated by you. if ( no errors ) do whatever you want. email, insert database. end if if ( errors exist or user has not submitted form ) if ( errors exist ) display $errors array. e.g. echo "there were errors:<br> " . join("<br /> - ",$errors); display form. and in your <input> tags set the value to $_REQUEST[email1], $_REQUEST[city], etc. like Shawn suggested. end if