当我测试它时,我得到一个弹出窗口说它提交,但我也收到来自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">×</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">×</button><strong>Error! </strong> Somthing Wrong</div>');
}
}});
});
这是PHP Mailer:
<?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>
答案 0 :(得分: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">×</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">×</button><strong>Error! </strong> Somthing Wrong</div>');
}
因为你正在使用parseInt(response)>1
条件,所以条件将总是落到其他部分。在true
中使用false
php
概念并在javascript中检查true/false
答案 1 :(得分:0)
$("#Submit").click(function(){}
而不是这一行,
试
$("#frmContact").submit(function(){}
有关详细信息,请参阅jQuery Submit Api。有一个类似的例子。