I can get the PHP mailform to work correctly. The problem is I would like the submit button upon validation to not refresh the entire page but just the content div layer. I have researched various solutions however I am fairly new to PHP so I may be entering the code incorrectly. The working page can be found at http://www.kaimeramedia.com/derek/Website and then by clicking on the contact link. The actual contact page code is:
<form method="post" id="captcha_form" name="form1" action="mailform.php">
<br />
<table width="100%" border="0">
<tr><td rowspan="4" width="125">
</td>
<td>
<div id="Imprint3">Name:</div></td>
<td width="15">
</td><td><div id="Imprint3">All fields are required.</div></td>
<tr>
<td width="150">
<input type="text" id="name" name="name" maxlength="30" value="name" onfocus="if
(this.value==this.defaultValue) this.value='';"/></td>
<td rowspan="5" colspan="2">
<table>
<tr><td width="10"></td><td>
<div id="Imprint3">Message:</div></td></tr>
<tr><td width="10">
</td>
<td width="200"><textarea name="message" id="message"
rows="6" cols="25" value="Enter your message" onfocus="if
(this.value==this.defaultValue) this.value='';"/><?php echo "</tex" . "tarea>"; ?>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input name="submit" type="submit" value="Submit" />
<input type="button" name="reset_form" value="Reset" onclick="this.form.reset();">
</td></tr></table>
</td>
</tr>
<tr>
<td valign="top"><div id="Imprint3">Email:</div></td> </tr>
<td valign="top">
<input type="text" id="email" name="email" maxlength="100" value="you@email.com"
onfocus="if(this.value==this.defaultValue) this.value='';"/></td>
</tr> <tr>
<td width="15"></td>
<td align="center"> <div style="padding-bottom: 1em;">
<img src="captcha.php" /> <br />
<input type="text" name="userpass" value="input the above text here" onfocus="if
(this.value==this.defaultValue) this.value='';"></div></td></tr></table></form>
and the mailform.php file is as follows:
<?PHP
session_start();
try{
$check = new check();
if(!isset($_REQUEST['email']))
throw new exception('You did not enter an email address.');
if(!isset($_REQUEST['message']))
throw new exception('You did not enter a message.');
if(!isset($_REQUEST['name']))
throw new exception('You did not enter a name');
$sender = $_REQUEST['email'];
$message = $_REQUEST['message'];
$name = $_REQUEST['name'];
$recipient = 'text@text.com';
$subject = 'Regarding Your Portfolio';
if($check->captcha('userpass') == FALSE)
throw new exception('Your captcha is incorrect.');
if($check->spam($sender) == FALSE)
throw new exception('Your email field contains spam.');
if($check->spam($name) == FALSE)
throw new exception('Your name field contains spam.');
if($check->length($sender, 10) == FALSE)
throw new exception('Your email field does not satisfy the minimum character
count.');
if($check->length($message, 8) == FALSE)
throw new exception('Your message field does not satisfy the minimum character
count.');
if($check->length($name, 3) == FALSE)
throw new exception('Your name field does not satisfy the minimum character count.');
mail($recipient, $subject, $message, "From: $name <$sender>" );
include'thankyou.php';
}catch (Exception $E){
die($E->getMessage());
}
class check{
function captcha($field){
if(isset($_REQUEST[$field])==FALSE){ return false; }
if($_SESSION['pass'] != $_REQUEST[$field]){ return false; }
return true;
}
function email($email){
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ return false;}
return true;
}
function spam($field){
if(eregi("to:",$field) || eregi("cc:",$field) || eregi("\r",$field) ||
eregi("\n",$field) || eregi("%0A",$field)){ return false; }
return true;
}
function length($field, $min){
if(strlen($field) < $min){ return false; }
return true;
}
}
?>
I have replaced my receiving email to text@text.com on this page, but on my site it is my actual email. Another user helped me a lot with getting this script functioning correctly, but I was wondering if it is possible to get the thankyou.php to refresh into the current div layer and not refresh the entire page. the line that I want to edit I believe is the: include 'thankyou.php'; Any help would be appreciated.
This is not possible with php.
You will have to use JavaScript to realize this.
You would be able to send the email without refreshing the page and to load a file in a div.
With e.g. jQuery and the
$.ajaxfunction it would look something like this:In this example you would be able to get the message in sendmail.php with
$_POST["message"]Or use
$.postand serialize your entire form:Just that you can see how to handle something like this.