2010-11-30 89 views
0

我在我的网站(www.chrisanstey.co.uk)上运行AJAX联系表单,该表单不仅仅在Firefox中提交,而且在其他所有浏览器中都绝对正常。形式使用以下PHP文件:AJAX表单提交(仅适用于Firefox)

<?php 

$to = "[email protected]"; //This is the email address you want to send the email to 

if(!isset($_GET['action'])) 
{ 
die("You must not access this page directly!"); //Just to stop people from visiting contact.php normally 
} 

/* Now lets trim up the input before sending it */ 

$name = trim($_GET['name']); //The senders name 
$email = trim($_GET['email']); //The senders email address 
$subject = "A message sent from " . $name . " on Chris Anstey's portfolio"; //The senders subject 
$message = trim($_GET['msg']); //The senders message 

mail($to,$subject,$message,"From: ".$email.""); //a very simple send 

echo 'contactarea|<p>Thank you '.$name.' for your message, I will reply to you as soon as I can.</p>'; //now lets update the "contactarea" div on the contact.html page. The contactarea| tell's the javascript which div to update. 
?> 

,并以下JavaScript文件:

function createRequestObject() { 
    var ro; 
    var browser = navigator.appName; 
    if(browser == "Microsoft Internet Explorer"){ 
     ro = new ActiveXObject("Microsoft.XMLHTTP"); 
    }else{ 
     ro = new XMLHttpRequest(); 
    } 
    return ro; 
} 

var http = createRequestObject(); 

function sendemail() { 
var msg = document.contactform.msg.value; 
var name = document.contactform.name.value; 
var email = document.contactform.email.value; 
document.contactform.send.disabled=true; 
document.contactform.send.value='Sending....'; 

    http.open('get', 'contact.php?msg='+msg+'&name='+name+'&email='+email+'&action=send'); 
    http.onreadystatechange = handleResponse; 
    http.send(null); 
} 

function handleResponse() { 
    if(http.readyState == 4){ 
     var response = http.responseText; 
     var update = new Array(); 

     if(response.indexOf('|' != -1)) { 
      update = response.split('|'); 
      document.getElementById(update[0]).innerHTML = update[1]; 

     } 
    } 
} 

形式是一个WordPress页面中,被称为通过下面的HTML模板:

<div id="contactarea"> 
<form name="contactform" id="contactform"> 
<p>Full Name:<br /> 
    <span class="wpcf7-form-control-wrap your-name"><input type="text" name="name"></span></p> 
<p>Email:<br /> 
    <span class="wpcf7-form-control-wrap your-email"><input type="text" name="email"></span></p> 
<p>Message:<br /> 
    <span class="wpcf7-form-control-wrap your-message"><textarea name="msg" rows="6" id="textarea"></textarea></span></p> 
<p><input type="submit" value="Send Email" name="send" id="submitbutton" onClick="sendemail();"></p> 
</form> 
</div> 

如果任何人有任何想法或遇到类似的问题与AJAX不工作在Firefox中,请你能回复。任何帮助将非常感激!

回答

1

那么,你的查询可以被缓存,一个。它是调用的PHP,或不?该代码适用于我在Firefox中,但一旦它工作后,它应该缓存它,所以它不会再次调用它。

+0

当表单提交时,应该向我发送一封电子邮件,内容是用户输入到表单中,然后给他们一个小信息,让他们知道表单已经通过。当我测试它时,URL更改(它不应该)以显示php查询并且不发送电子邮件。有任何想法吗? – 2010-11-30 09:12:02

0

我已经在Firefox测试它与Ajax调用工作正常...我不认为这是问题..尝试在Firefox安装Firebug的插件,所以我们可以看看有什么错误消息

编辑: 如果我从您的评论理解正确,页面改变,当你点击提交按钮,你不想说......如果可以的话,那么就改变你的提交按钮的onClick属性此

onClick="sendemail();return false;" 

或更改您的输入提交到输入BU tton ...这是因为当你点击一个提交按钮时,即使你有一个onClick事件,表单也会被发送,所以return false应该停止它...当你想阻止默认事件时链接也是一样的

祝你好运