I have two pages apply.php and registration.php.
In registration.php
, i have
if(isset($_POST['submitted'])){
//validation part
$form_error ='';
if(!$fullname开发者_JS百科){
$form_error.= "Enter full name<br />";
header('Location: apply.php');
And in apply.php
, I display an error message if an error happened:
<p><?php if(isset($form_error))echo $form_error?></p>
<form action="registration.php" method="post">
<label for="fullname">Fullname</label>
<input type="text" name="fullname" />
Why I am not getting echoed error message "Enter full name" in apply.php ?
You're not getting it because apply.php doesn't know about $form_error - it was initialised in registration.php, but not in apply.php.
You could do the following:
$_SESSION['form_error'] = "Enter full name<br />";
Then you could access that on apply.php.
<p><?php if(isset($_SESSION['form_error']))echo $_SESSION['form_error']?></p>
<form action="registration.php" method="post">
<label for="fullname">Fullname</label>
<input type="text" name="fullname" />
Alternatively, you could also pass the error through the header (via GET):
$form_error.= "Enter full name<br />";
header('Location: apply.php?form_error=' . urlencode($form_error));
And access it like this in apply.php:
<p><?php if(isset($_GET['form_error']))echo $_GET['form_error']?></p>
<form action="registration.php" method="post">
<label for="fullname">Fullname</label>
<input type="text" name="fullname" />
Do not use header nor different files.
Put everything into one and on error just show the form.
Redirect on success only.
Make it like this:
<?
if ($_SERVER['REQUEST_METHOD']=='POST') {
if(!$fullname) $form_error.= "Enter full name<br />";
// other validations
if (!$form_error) {
//writing to database
Header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
}
?>
<form>
...
See also http://en.wikipedia.org/wiki/Post/Redirect/Get
精彩评论