Form Submission Redirect Issue
Did not work...I added the following: Your inquiry ha开发者_如何学Pythons been sent!
'; header('Location: index.php'); die (); }index.php code
<?php
if (isset($_SESSION['success'])) {
echo($_SESSION['success']);
unset($_SESSION['success']);
}
?>
?>
Once you have redirected them, your page cannot show them anything anymore.
Whatever message you want to show, will now have to reside on the page they are being sent to, which in your case is index.php
.
P.S. The same applies the other way around: once you have sent any information to the browser (via echo
, print_r
, var_dump
or any other way) you cannot redirect them anymore.
First of all, please always, always, always surround your if
statement blocks with curly braces. You will avoid much pain.
if ($sent) {
header('Location: index.php');
}
Second of all, by changing the header, you are redirecting the user to a different page. Execution of the current script stops, and then index.php
is executed, so your print statement never is never executed.
If you want to show a message, you need to store it in a way that persists across requests. One way to do that is to use the $_SESSION
superglobal (see here for more info) when the form submits in your script, and then display whatever is stored in $_SESSION
in index.php. Something like this may work: (warning: coded in the browser. use at your own risk.)
//in your form processing code
if ($sent) {
$_SESSION['success'] = '<p class="error">Your inquiry has been sent!</p>';
header('Location: index.php');
}
//in index.php
//render your page
if (isset($_SESSION['success']) {
echo($_SESSION['success']);
unset($_SESSION['success']);
}
Try this::
if($sent)
{
header('Location: index.php?e=inquiry_sent');
exit();
}
Remember to show the message after of send the form:
if (isset($_GET['e']))
{
if ($_GET['e'] == "inquiry_sent")
{
print '<p class="error">Your inquiry has been sent!</p>';
}
}
精彩评论