/    Sign up×
Community /Pin to ProfileBookmark

Works in IE, doesn’t work in Firefox. AJAX mail form.

After completing in the fields required. It should process the information and send the email. After there, displaying a “Thank you.” at the bottom of the form.

It works in IE like clockwork, but when all the fields are valid and pass in Firefox, it just reloads the page and doesn’t display the “Thank you.” therefore, not sending the email.

If you can spot where the error is, I would much appreciate it.

Thank you for your time.

[URL=http://www.design-ready.com/contact.php]Contact Form using AJAX[/URL]

Here’s the code for those who don’t wish to leave the site (I’ll just post the script instead of the entire file):

[CODE]
<?
if(!isset($rnd) || !isset($name) || !isset($email) || !isset($subject) || !isset($body)) {
showform();
} else {
processform();
}

function processform(){
global $name, $email, $subject, $body;

$email_to1 = “[email protected]”; // Fake just for demo purpose
$email_from1 = $email;
$email_subject = “Contact Form: “.stripslashes($subject);
$email_message = “Please find below a message submitted by ‘”.stripslashes($name);
$email_message .=”‘ on “.date(“d/m/Y”).” at “.date(“H:i”).”nn”;
$email_message .=”Email of sender: “.stripslashes($email).”nn”;
$email_message .=”——— START OF SUBMITTED MESSAGE ———nn”;
$email_message .= stripslashes($body);
$email_message .=”nn——— END OF SUBMITTED MESSAGE ———nn”;

// SEND EMAIL TO email_to1 – message to you!!
$headers = ‘From: ‘.$email_from1.”rn” .
‘Reply-To: ‘.$email_from1.”rn” .
‘X-Mailer: PHP/’ . phpversion();

mail($email_to1, $email_subject, $email_message, $headers);

echo “Thank You.”;
die();
} // end processform()

function showform() {
?>
<html>
<head>
<title>.contact form</title>
<link rel=”stylesheet” type=”text/css” href=”css/form.css”>
<script type=”text/javascript”>
<!–
var http = createRequestObject();
var areal = Math.random() + “”;
var real = areal.substring(2,6);

function createRequestObject() {
var xmlhttp;
try { xmlhttp=new ActiveXObject(“Msxml2.XMLHTTP”); }
catch(e) {
try { xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);}
catch(f) { xmlhttp=null; }
}
if(!xmlhttp&&typeof XMLHttpRequest!=”undefined”) {
xmlhttp=new XMLHttpRequest();
}
return xmlhttp;
}

function sendRequest() {
var rnd = Math.random();
var name = escape(document.getElementById(“name”).value);
var email = escape(document.getElementById(“email”).value);
var subject = escape(document.getElementById(“subject”).value);
var body = escape(document.getElementById(“body”).value);

try{
http.open(‘POST’, ‘contact.php’);
http.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
http.onreadystatechange = handleResponse;
http.send(‘name=’+name+’&email=’+email+’&subject=’+subject+’&body=’+body+’&rnd=’+rnd);
}
catch(e){}
finally{}
}

function check_values() {
var valid = document.getElementById(“valid”).value;
if(real !== valid) {
alert(“Please enter the 4 digits as they appear.”);
return false;
}

var name = document.getElementById(“name”).value;
var email = document.getElementById(“email”).value;
var subject = document.getElementById(“subject”).value;
var body = document.getElementById(“body”).value;
if(trim(name) == “” ||
trim(email) == “” ||
trim(subject) == “” ||
trim(body) == “”) {
alert(“Please complete all fields”);
} else {
if(isEmail(email)) {
document.getElementById(“submit”).disabled=true;
document.getElementById(“submit”).value=’Please Wait..’;
sendRequest();
} else {
alert(“Email appears to be invalid. Please check.”);
document.getElementById(“email”).focus();
document.getElementById(“email”).select();
}
}
}

function handleResponse() {
try{
if((http.readyState == 4)&&(http.status == 200)){
var response = http.responseText;
document.getElementById(“confirmation”).innerHTML = response;
document.getElementById(“confirmation”).style.display =””;
document.getElementById(“form”).style.display = “none”;
}
}
catch(e){}
finally{}
}

function isUndefined(a) {
return typeof a == ‘undefined’;
}

function trim(a) {
return a.replace(/^s*(S*(s+S+)*)s*$/, “$1”);
}

function isEmail(a) {
return (a.indexOf(“.”) > 0) && (a.indexOf(“@”) > 0);
}

// –>
</script>

<?
} // end showform()
?>
[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@ForeverIriseauthorDec 12.2006 — There has to be a reason why this doesn't work. Anyone? Any help please?

Thank you for your time.
×

Success!

Help @ForeverIrise spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...