Jump to content

if there's an error, code doesn't all execute


Genesis730

Recommended Posts

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

Link to comment
Share on other sites

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">

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.