Genesis730 Posted January 11, 2010 Share Posted January 11, 2010 What I want this to do is, if there is an error, I want the page to scroll down to the form. If I have the header code, the page will scroll down where I want but there is no error message displayed, if I take out the header line, the error message will echo... if ($error!="") { header('Location: mypage.php#error'); echo "<span id='red'><font size='2'>Please fill out the following required fields:</font><br />$error</span>"; } else { //SUBMIT DATA Quote Link to comment https://forums.phpfreaks.com/topic/188076-if-theres-an-error-code-doesnt-all-execute/ Share on other sites More sharing options...
JonnoTheDev Posted January 11, 2010 Share Posted January 11, 2010 Dont do this! if ($error!=""){header('Location: mypage.php#error');echo "<span id='red'><font size='2'>Please fill out the following required fields:</font><br />$error</span>";} Submit the form to the page along with an anchor tag where your errors will be displayed. Only redirect the user to a new page if the form was filled out successfully i.e <a name="results"></a> <?php if($errors) { // display errors } ?> <form method="post" action="process-form.php#results"> Quote Link to comment https://forums.phpfreaks.com/topic/188076-if-theres-an-error-code-doesnt-all-execute/#findComment-992905 Share on other sites More sharing options...
Genesis730 Posted January 11, 2010 Author Share Posted January 11, 2010 neil.johnson, That is what I want it to do, there is another header further down that links to "thankyou.php" however i want it to scroll down to the the error message (and anchor located on the same page) if they make a mistake, is there any other code besides header that will go to a link Quote Link to comment https://forums.phpfreaks.com/topic/188076-if-theres-an-error-code-doesnt-all-execute/#findComment-992909 Share on other sites More sharing options...
JonnoTheDev Posted January 11, 2010 Share Posted January 11, 2010 is there any other code besides header that will go to a link The example that I have just posted does this. It will post the form and return to the anchor point. If you use a header redirect you will lose all the data on the page including any errors. That is why I have stated you should only use a header redirect when the form is correctly validated and you have finished with the post data. headers should only be used prior to sending any output to the screen or you will receive an error. Look closely at the example I have posted again. You should not be trying to reload the page to scroll to an anchor point. Quote Link to comment https://forums.phpfreaks.com/topic/188076-if-theres-an-error-code-doesnt-all-execute/#findComment-992913 Share on other sites More sharing options...
Genesis730 Posted January 11, 2010 Author Share Posted January 11, 2010 Not quite how I want it to work.. If there IS an error, i want the page to scroll down to show the errors and form so they can fix their problem, else submit the data and redirect to thankyou.php here's the entire code $error = ''; if (!$firstname) $error = $error."<b>First Name</b><br />"; if (!$lastname) $error = $error."<b>Last Name</b><br />"; if ($error!="") echo "<span id='red'><font size='2'>Please fill out the following required fields:</font><br />$error</span>"; else { //SUBMIT DATA mysql_query("INSERT INTO contact VALUES(`id`,'". $firstname ."','". $lastname ."')") or die(mysql_error()); //REDIRECT header('Location: thankyou.php'); } I thought this could be done by putting one header directing to the anchor if there is an error doesn't matter if there is two headers because it will hit an error, stop the execution and not redirect to thankyou.php anyways... but if everything is good, then the error script won't execute and the page will redirect to thankyou.php. I hope this clears my situation up a little better Quote Link to comment https://forums.phpfreaks.com/topic/188076-if-theres-an-error-code-doesnt-all-execute/#findComment-992920 Share on other sites More sharing options...
piyush23424 Posted January 11, 2010 Share Posted January 11, 2010 can you send me the file on my mail : piyush_sfgroup@hotmail.com Quote Link to comment https://forums.phpfreaks.com/topic/188076-if-theres-an-error-code-doesnt-all-execute/#findComment-992923 Share on other sites More sharing options...
Genesis730 Posted January 11, 2010 Author Share Posted January 11, 2010 Why do you need the whole file? The rest is just the html, the form and stuff... Has nothing to do with the error not displaying or going to anchor if there is an error Quote Link to comment https://forums.phpfreaks.com/topic/188076-if-theres-an-error-code-doesnt-all-execute/#findComment-992925 Share on other sites More sharing options...
JonnoTheDev Posted January 11, 2010 Share Posted January 11, 2010 Not quite how I want it to work.. If there IS an error, i want the page to scroll down to show the errors and form so they can fix their problem, else submit the data and redirect to thankyou.php As I have stated, the example I have posted will do this. LOOK AT THE FORM ACTION ATTRIBUTE! Just add your validation code into the appropriate place. Quote Link to comment https://forums.phpfreaks.com/topic/188076-if-theres-an-error-code-doesnt-all-execute/#findComment-992928 Share on other sites More sharing options...
Genesis730 Posted January 11, 2010 Author Share Posted January 11, 2010 I tried the above example <a name="results"></a> <?php if($errors) { // display errors } ?> <form method="post" action="process-form.php#results"> and it doesn't scroll down to the anchor.. atleast on the first submit, i have to submit the data twice (once for the error code then the second time it scrolls down to show me the form) but the first submit doesn't go to the anchor Quote Link to comment https://forums.phpfreaks.com/topic/188076-if-theres-an-error-code-doesnt-all-execute/#findComment-992934 Share on other sites More sharing options...
JonnoTheDev Posted January 11, 2010 Share Posted January 11, 2010 Put the anchor under the form further down the page! </form> <a name="results"></a> It does work. I use this method a lot. Quote Link to comment https://forums.phpfreaks.com/topic/188076-if-theres-an-error-code-doesnt-all-execute/#findComment-992941 Share on other sites More sharing options...
Genesis730 Posted January 11, 2010 Author Share Posted January 11, 2010 When I submit the form with page.php#error the data doesn't enter into the database... Quote Link to comment https://forums.phpfreaks.com/topic/188076-if-theres-an-error-code-doesnt-all-execute/#findComment-992972 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.