sparckyz,
A good method is to not die() but rather re-print the form with all the information filled out. The easiest way to do this is to make the form a function and if errors were found print the errors and then print the form using the $_POST variables.
Also, a good technique to prevent people from refreshing the page is to check for this:
PHP Code:
function pageRefreshed1(){
return($_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0')?true:false;
}
So... as I said above, this is generally considered good logic for once the form is submitted (I used this code recently to help scrappy out) :
PHP Code:
function pageRefreshed1(){
return($_SERVER['HTTP_CACHE_CONTROL'] == 'max-age=0')?true:false;
}
function isEmpty($field) {
return stripslashes(trim($field))==""?true:false;
}
function isValidEmail($to_check) {
return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$to_check)?true:false;
}
if($_GET['do']=="submit") {
if(isEmpty($_POST['name'])||isEmpty($_POST['email'])||isEmpty($_POST['sonicCharacter'])) {
echo "One or more of the fields were left blank. Please make sure you have filled out the form completely.<br /><br />\n";
printForm($_POST);
} elseif(!isValidEmail($_POST['email'])) {
echo "The email address you supplied is invalid. Please supply a valid email address.<br /><br />\n";
printForm($_POST);
} else {
if(pageRefreshed1()) {
echo "Please do not spam our form. <br />";
} else {
sendEmail($_POST);
echo "Thank you for entering our competition. If you are successful, we will notify you via e-mail, and winners will be posted on the forums as soon as all winners have been notified.";
}
}
} else {
printForm();
}
I'll leave functions like sendEmail() and printForm() out because they've already been discussed
Here's a hint though on how to start printForm() :
PHP Code:
function printForm($postVars = null) {
$newsletter_checked = ' checked="checked"';
if(!is_null($postVars)) {
$name = stripslashes($postVars['name']);
$email = stripslashes($postVars['email']);
$newsletter_checked = $postVars['newsletter']=="CHECKED"?' checked="checked"':"";
}
/* ... */
}
Happy coding!
Regards,
Patrick