可以';t获取php-mailform提交按钮以仅刷新当前DIV层


Can't get php mailform submit button to refresh current DIV layer only

我可以让PHP邮件表单正常工作。问题是,我希望验证后的提交按钮不刷新整个页面,而只刷新内容div层。我研究过各种解决方案,但我对PHP还很陌生,所以我可能输入的代码不正确。工作页面可以在www.kaimeramedia.com/derek/Website上找到,然后点击联系链接。实际联系人页面代码为:

 <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>

mailform.php文件如下:

 <?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;
}
}
?>

我已将接收电子邮件替换为text@text.com在这个页面上,但在我的网站上,这是我真正的电子邮件。另一位用户在让这个脚本正常运行方面帮了我很多忙,但我想知道是否可以让thankyou.php刷新到当前div层,而不刷新整个页面。我想编辑的行是:include 'thankyou.php';。如有任何帮助,不胜感激。

这在php中是不可能的。您必须使用JavaScript才能实现这一点。

您可以在不刷新页面的情况下发送电子邮件,并在div中加载文件。

使用,例如jQuery$.ajax函数,它看起来像这样:

var msg; // defines variable "msg"
$('#button_ID').click(function(e){
e.preventDefault(); // this prevents the page from reloading (or default behavior, so the form submit)
msg = $('#input_field_ID').val(); // gets the value of the input field and saves it in variable "msg"
$.ajax({
    type: 'POST',
    url: '/sendmail.php',
    data: {message : msg}, // posts variable "msg" as "message"
    cache: false,
    success: function(data) {
        $("#yourdivID").load('/thankyou.php'); // loads a file in a div
    },
    error: function(xhr, ajaxOptions, thrownError){
        alert('there was an error');
    }
});
});

在本例中,您可以使用$_POST["message"] 在sendmail.php中获取消息

或者使用$.post并序列化整个表单:

$.post("/sendmail.php", $("#formID").serialize());

只是你可以看到如何处理这样的事情。