当我测试它时,我收到一个弹出窗口说它正在提交,但我也从 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 邮件程序:
<?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>