Forum Moderators: open

Message Too Old, No Replies

JavaScript and AJAX issue with firefox

JavaScript, Ajax Firefox

         

kahunaschooner

4:33 am on Apr 20, 2009 (gmt 0)

10+ Year Member



Hi,

I have a simple contact form which I downloaded the code for from a site. I have integrated it into a page, styled it etc. For some reason it is sending in IE but not FF (In FF when you press send the button refreshes quickly, no email send, no sent message displayed). Also the subject is not being included in the message in IE. Here is the contact page -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ben Morrow - Contact - Project Management, Web Development, Supply Chain Solutions</title>
<link href="css/main_layout.css" rel="stylesheet" type="text/css" media="screen, projection" />

<script language="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;
var subject = document.contactform.subject.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];

}
}
}
function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}

function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}

</script>

</head>

<body>

(I have deleted the irrelevant content from here)

<div id="contactarea">
<form name="contactform" class="hFontField" id="contactform">
<div id="messagearea">

<input type="text" name="name" id="name" class= "fieldColour" value="Name" onclick="clickclear(this, 'Name')" onblur="clickrecall(this,'Name')" />
<br /><br />
<input type="text" name="email" id="email" value="Email" class= "fieldColour" onclick="clickclear(this, 'Email')" onblur="clickrecall(this,'Email')" /><br /><br />
<input type="text" name="subject" id="subject" value="Subject" class= "fieldColour" onclick="clickclear(this, 'Subject')" onblur="clickrecall(this,'Subject')" /><br /><br />

</div>
<div id="messagearea2">
<textarea name="msg" id="message" rows="10" class= "fieldColourMessage" onclick="clickclear(this, 'Type your message')" onblur="clickrecall(this,'Type your message')">Type your message</textarea><br /><br />
<br /><br /><br /><br /><br /><br />
<input type="submit" value="Send" name="send" onClick="sendemail();" class= "fieldColour2" id="send"/>

<div id="formresponse"></div>
</div>
</form>
</div>
<div id="footer">

</div>
</div>
</div>

</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-#*$!#*$!#*$!");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>

and the PHP file which it uses -

<?php
/*

Author: Andrew Walsh
Date: 30/05/2006
Codewalkers_Username: Andrew

This script is a basic contact form which uses AJAX to pass the information to php, thus making the page appear to work without any refreshing or page loading time.

*/

$to = "benjamin.o.morrow@gmail.com"; //This is the email address you want to send the email to
$subject_prefix = "Message from website: "; //Use this if you want to have a prefix before the subject

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 = trim($_GET['subject']); //The senders subject
$message = trim($_GET['msg']); //The senders message

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

echo 'formresponse¦Thank you '.$name.', your email has been sent.'; //now lets update the "contactarea" div on the contact.html page. The contactarea¦ tell's the javascript which div to update.
?>

holyhttp

7:42 pm on Apr 22, 2009 (gmt 0)

10+ Year Member



Try using the third parameter "false" in the HTTPRequest funcion open(method, url, false);

Try this to ensure the HTTPRequest object is created:

try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
try { return new XMLHttpRequest(); } catch(e) {}
alert("XMLHttpRequest not supported");

Third use the FF error console tool to see what error it displays