Does anyone know of a good tutorial that shows you how to make a contact form that checks to make sure all the fields have been provided and that sets the error message to a variable?
Contact Form Tutorial?
Search this forum, I remember a few threads ...
Not exactly a tutorial, but it is well commented.
#### RETURN EMAIL ADDRESS ####
if($_POST['from']==''){$log .= 'Must provide a return email address.';
}elseif (!ereg("^[^@]{1,64}@[^@]{1,255}$", $_POST['from'])){
$log .= 'The return email address is not a valid pattern.';
};
#### FROM NAME ####
if($_POST['fromName']==''){$log .= 'Please provide your name.';}else{
$name=explode(' ',$_POST['fromName']);
if(count($name) < '2'){$log .= 'Please provide your given name <span style="text-decoration:underline;font-weight:bolder;">and</span> surname.';};
};
#### SUBJECT ####
if(trim($_POST['subject'])==''){
$log .= 'No subject.';
};
if(trim(strlen($_POST['subject']))>255){
$log .= 'Subject exceeds maximum length of 255 characters. Please use the form provided.';
};
#### CONTENT ####
if(trim($_POST['content'])==''){$log .= 'No content!';};
$length = strlen($_POST['content']);
if($length>4000){$log .= 'Your content is too long: '.$length.' characters!!';};
#### MULTIPLE SUBMISSIONS ####
if(!prevent_multi_submit('mailForm','')){
$log.='You cannot submit the same form twice!';
}
. . . and to prevent multiple submissions, have this function on the global scope:
function prevent_multi_submit($name,$excl = "validator") {
$string = "";
foreach ($_POST as $key => $val) {
// this test is new in version 1.01 to exclude a single variable
if ($key != $excl) {
$string .= $val;
}
};
$i='0';
while(isset($_SESSION[$name.$i])){
if ($_SESSION[$name.$i] === md5($string)) {
return false;
} else {
$i++;
};
};//end while
$_SESSION[$name.$i] = md5($string);
return true;
}
Finally, if there were errors while checking, you need to display those errors and return the form.
if($log!=''){
$content = '<div class="error">There were some errors:
'.$log.'
</div>
'.$form;
}else{
$content=$form;
};
$pageBuilder->addContent($content);
echo $pageBuilder->assemble();
Remember to initialise $content=''; at the start of the script, if register_globals=1 in php.ini