1

当我测试它时,我收到一个弹出窗口说它正在提交,但我也从 ajax 部分收到一条消息,说有一个错误,即使所有内容都通过电子邮件成功发送给我。

我认为一个问题是来自 PHP 和 AJAX 的两种不同的提交消息。那么需要PHP吗?非常感谢您的帮助。

更新:

我收到的错误消息是提交后来自 ajax 的错误响应,说“出错了”。我还弹出一个消息,说它已正确提交,是来自 PHP 的回声。

更新* * 实时版本: http: //pieceofmedesigns.com/test/contact.html

这是 AJAX:

$(function(){

$("#Submit").click(function(){

$.ajax({type:'POST', url: './php/mailer.php', data:$('#frmContact').serialize(), success: function(response) {
$("#spanMessage").html('Please Wait...');


 if(parseInt(response)>1)
   {
     $("#spanMessage").html('<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">&times;</button><strong>Well done!</strong> Your message has been sent.</div>');
   }
   else{
     alert(response);
     $("#spanMessage").html('<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button><strong>Error! </strong> Somthing Wrong</div>');
   }


}});


});

这是 PHP 邮件程序:

<?php session_start();
if(isset($_POST['Submit'])) {
$youremail = 'myemail@gmail.com';
$fromsubject = 'Test Email';
$txtName = $_POST['txtName'];
$txtEmail = $_POST['txtEmail'];
$txtSubject = $_POST['txtSubject']; 
$txtText = $_POST['txtText']; 
$to = $youremail; 
$mailsubject = 'Masage recived from'.$fromsubject.' Contact Page';
$body = $fromsubject.'

The person that contacted you is  '.$txtName.'
 E-mail: '.$txtEmail'

 Message: 
 '.$txtText.'

|---------END MESSAGE----------|'; 
echo "Thank you fo your feedback. I will contact you shortly if needed.<br/>Go to <a       href='/index.php'>Home Page</a>"; 
                            mail($to, $subject, $body);
 } else { 
echo "You must write a message. </br> Please go to <a href='/contact.php'>Contact Page</a>"; 
}
?> 

这是表格:

<form action="" method="post" id="frmContact">
<h5>Whats your Name?</h5>
<input name="txtName" type="text" class="txbx" value="Name" /><br />
<h5>Whats your Email?</h5>
<input name="txtEmail" type="text" class="txbx" value="Email" /><br />
<h5>Email Subject?</h5>
<input name="txtSubject" type="text" class="txbx" value="Subject" /><br />
<div class="erabox">
<h5>Message to us</h5>
<textarea name="txtText" cols="" rows="" class="txbx era" value="Message" ></textarea>          <br />
<input name="" type="button" class="sendbtn" value="Send Message" align="left" id="btnSend"/>
</form>
4

2 回答 2

0

您忘记命名按钮并将其 id 设置为Submit

<input name="Submit" type="button" class="sendbtn" value="Send Message" align="left" id="Submit"/>
             ^ // here was the mistake.                                                   ^ here also

问题就在这里

if(parseInt(response)>1)
{
 $("#spanMessage").html('<div class="alert alert-success"><button type="button" class="close" data-dismiss="alert">&times;</button><strong>Well done!</strong> Your message has been sent.</div>');
}
else{
 alert(response);
 $("#spanMessage").html('<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button><strong>Error! </strong> Somthing Wrong</div>');
}

由于您使用的是条件,因此条件总是会落入其他部分。而是parseInt(response)>1使用true false概念php并签true/false入javascript。

于 2013-02-22T09:30:19.277 回答
0
$("#Submit").click(function(){}

而不是这条线,

尝试

$("#frmContact").submit(function(){}

请参阅jQuery 提交 API了解更多信息。有一个类似的例子。

于 2013-02-22T09:30:35.020 回答