There are a number of ways to do this, but for most, your form is going to have to be a PHP page, not an HTML page. HTML pages. Let's assume your form page is form.php and your processing page is called proc.php.
In proc.php, if there is an error, you could do something like this:
if ($error) {
$err_msg = 'Incorrect password';
include('form.php'); // will process form.php
exit();
}
This would require that your form.php can easily be included from some other page and it would need to be able to display the message. The form might look like this:
<form method="post" action="proc.php">
<?php
if (isset($err_msg)) {
echo '<div>' . $err_msg . '</div>';
}
?>
username:<input type="text" name="username">
password:<input type="password" name="username">
<input type="submit" name="submit" value="submit">
</form>
If you cannot include from.php from inside proc.php, then you could store pass the error message by setting a cookie, using the query string, or using $_SESSION.
To set a cookie in proc.php, use [man]setcookie[/man]. Then in form.php, you can look in $_COOKIE to see if an error message exists. If so, you should display the error message and empty the cookie so the error is gone. NOTE that this approach should work even if form is only an HTML page because you can check cookies with javascript if using javascript is ok for your project. note that people without javascript would not be able to see the message in that situration.
to use session variables, you can set a value in proc.php like this:
if ($error) {
session_start(); // you must call this function before you ever check $_SESSION
$_SESSION['error_message'] = 'Incorrect password';
header('location: form.php'); // redirect to the form page...NOTE you cannot use this if you have output any text at all to the user's browser
exit();
}
in your form.php page, you would need to call [man]session_start[/man] and peek at $_SESSION['error_message'] to see if anything was there. If so, fetch and it and reset the value to NULL.
The last way is to put the error message in the query string when you redirect back to the form page:
if ($error) {
$err_msg = 'Incorrect password';
$url = 'form.php?err=' . urlencode($err_msg);
header('location: ' . $url); // redirect to the form page...NOTE you cannot use this if you have output any text at all to the user's browser
exit();
}
then in form.php you would need to check $_GET['err'];