Here's what I do ... best solution ever, probably not - but it works for me when I need to 'pound it out'.
#1 I always call my FORM (html page) from a php script. You'll see why later. In this case you can just have a script called index.php with:
Code: Select all
<?php
session_start();
require('indexform.html');
?>
We'll get to a little tweak in your indexform.html (along with WHY we're doing this.)
#2 Note session_start(); at the top of each script. I almost always put that IMMEDIATELY after <?php ... second nature for me.
#3 for the form handler ...
Code: Select all
<?php
... Do whatever you typically do to validate the data ...
// This is how I typically check something
if (strlen($_POST['firstname'] < 1)
{
$_SESSION['err_msg'] = "Put the error message here.";
}
else
{
... do whatever you typically with the data (personally, I store it to an array in $_SESSION)...
}
... do the same for each field ...
... at the bottom of the script ...
if (isset($_SESSION['err_msg'])
{
header('Location: index.php'); // Send 'em back to fix errors
}
else
{
// Let them go to whatever they should do with good data
header('Location: (whatever is next.php)');
}
?>
So, you have checked for good data and sent them along if all is well.
You've sent them back to the original script if you didn't like the answers they provided.
Now, to show the user the errors of their ways.
#4 The tweak to your html form:
Code: Select all
<html>
<head>
<title>Whatever</title>
<meta>Stuff</meta>
</head>
<body>
<? if(isset($_SESSION['err_msg'])){echo $_SESSION['err_msg'] . "<br />";} ?>
... go on with the rest of your form exactly as it was ...
</body>
</html>
Now your error message shows up at the top of the form. Maybe in bold red letters.
Seems to work well for me. You can get fancier depending on your level of sophistication ... I have all the $_POST data in the $_SESSION array, so I usually make the form refer to the $_SESSION for default values.
Hope this helps ... got more questions? Fire away. The gang here is really friendly & helpful!