Hello I have a contact form bottom of the page. I did all checking and coding for submit with php. The question is when I submit form and some field is empty it get back and says please fill in for example Name or phone(as required fields) but because the form is in the bottom of the page visitor need to scroll down and see why it didn't submit. Is there any way that after submitting the form when there is a error that it will go to the position where is form? I can do redirect to anchor point(anchor point form) when there is the error but it loads again new page and I am loosing my error messages as I have on same-page php code for submitting. any help please? Thank you
If its an action=something.php form, have you tried javascript:history.go(-1) instead, the browser should remember at what point the visitor left the page. <form> <input type="button" value="Return to previous page" onClick="javascript:history.go(-1)" /> </form> Code (markup): or <a href="javascript:history.go(-1)" title="Return to the previous page">« Go back</a> PHP:
I tried this... it does job but same like anchor point. It goes back where visitor submit form but without error messages... thank you anyway
What about printing the error messages on the php page with the go back javascript link bellow it. Alternatively you could make the form page php and stick the html form between the php tags (?> Html form here <?php) and in the form use (action="") so it posts to it's self, you could then make it display an error message if a field is empty when posted or whatever you have set, without the page refreshing or changing.
Well in all fairness, it shouldn't submit to the next page, if there's an error, ie: an empty field. Here's something basic to get you going or show us your script that's supposed to show errors ? Somewhere up to the top of the page paste: <script type="text/javascript"> <!-- function validate_form ( ) { valid = true; if ( document.contact_form.contact_name.value == "" ) // change contact_name to suite your requirements { alert ( "Please fill in the 'Your Name' box." ); valid = false; } return valid; } //--> </script> Code (markup): Then add to your <form action=...> at the end put onsubmit="return validate_form ();"> PHP: You can add more if ( conditions as you need them
if (// form successfull) { // do whatever you do here } else { header("Location: page_with_form.php?error=1111111000#anchorpoint"); } and in your page_with_form.php use the $_GET[error] and parse it.. you may do something like this ... first digit = name second digit = phone third digit = email ..... so, in this case, 010 will mean name and email are fine, phone is not fine 110 will mean name and phone are fine, email is not fine good luck
Thank you for your replays... finally what i did and it work is code like this : when is empty field it will set variable form... and then I test if variable form was set in the body tag with some JavaScript like this <body id="page8" <?php if(isset($form)){ echo "onload=\"self.scrollTo(0,550)\"";}?> > "by the way form and php is on one page" and this does the job when there is empty field it will scroll down where is my form and also show there error messages... my php code: <?php if(isset($_POST['submit'])) { $errors = array(); $required_fields = array('meno', 'email', 'telefon' ); //form validation foreach($required_fields as $fieldname){ if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]))){ if ($fieldname == 'meno') {$errors[0] = 'meno' ;} if ($fieldname == 'email') {$errors[1] = 'email' ;} if ($fieldname == 'telefon') {$errors[2] = 'telefon' ; } $form =a; } } } ?>