我在我的网站(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中,请你能回复。任何帮助将非常感激!
当表单提交时,应该向我发送一封电子邮件,内容是用户输入到表单中,然后给他们一个小信息,让他们知道表单已经通过。当我测试它时,URL更改(它不应该)以显示php查询并且不发送电子邮件。有任何想法吗? – 2010-11-30 09:12:02