【问题标题】:Contact Form not working [PHP / AJAX]联系表格不起作用 [PHP / AJAX]
【发布时间】:2013-02-22 09:24:41
【问题描述】:

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

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

更新:

我收到的错误消息是提交后来自 ajax 的错误响应,说“Error something wrong”。我还弹出一个消息,说它已正确提交,是来自 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>

【问题讨论】:

  • 我在您的表单中没有看到“提交”
  • 您能否在您的问题中添加确切的错误消息?谢谢
  • 您也可以将按钮放在表单之外。这样你就确定不会有表单提交。

标签: php ajax phpmailer contact mailer


【解决方案1】:
$("#Submit").click(function(){}

而不是这一行,

试试

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

请参阅jQuery Submit Api 了解更多信息。有一个类似的例子。

【讨论】:

    【解决方案2】:

    您忘记命名按钮并将其 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>');
    }
    

    条件总是属于 else 部分,因为您使用的是 parseInt(response)&gt;1 条件。而不是在 php 中使用 true false 概念并在 javascript 中检查 true/false

    【讨论】:

    • 最初,我在里面有提交,但还是失败了,所以我把它省略了,因为它似乎无关紧要。
    • @Warren 您还没有将按钮的 ID 设置为 Submit。并调用Submit's点击事件。更改它并尝试。
    • 该脚本随模板一起提供。您提到的内容超出了我的专业知识,哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多