Can';t获取php邮件表单提交按钮以仅刷新当前DIV层

Can';t获取php邮件表单提交按钮以仅刷新当前DIV层,php,forms,html,form-submit,Php,Forms,Html,Form Submit,我可以让PHP邮件格式正常工作。问题是,我希望验证后的submit按钮不刷新整个页面,而只刷新ContentDiv层。我已经研究了各种解决方案,但是我对PHP还是相当陌生的,所以我可能输入的代码不正确。工作页面可在www.kaimeramedia.com/derek/Website上找到,然后单击联系人链接。实际联系人页面代码为: <form method="post" id="captcha_form" name="form1" action="mailform.php">

我可以让PHP邮件格式正常工作。问题是,我希望验证后的submit按钮不刷新整个页面,而只刷新ContentDiv层。我已经研究了各种解决方案,但是我对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中加载文件

使用例如和功能,它看起来像这样:

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');
    }
});

});
在本例中,您可以使用
$\u POST[“message”]

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

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

您可以看到如何处理类似的内容。

因此,为了清楚起见,我可以将此javascript与我当前的代码结合使用,以替换
中的include'thankyou.php'
$.post[“contact.php”],$(“#mailform”).serialize()?我是否在contact.php页面上调用javascript?然后是$.post(..);放入我的邮件格式文件。只是对上面脚本的执行有点困惑。这不是它的工作方式。我刚才给了你一个例子,说明如果你用jQuery解决它,会是什么样子,因为用php是不可能的。您必须包括jQuery库。Javascript文件包含在
中,或与
警报一起执行(“这是一个Javascript警报”)。也许您想阅读jQuery文档来开始,自己尝试一下,如果您还有其他问题,请返回: