无法获取php mailform提交按钮仅刷新当前的DIV图层

时间:2012-02-26 16:29:42

标签: php forms html form-submit

我可以让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';任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:2)

使用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());

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