I was trying to make a dynamic email script that would send the email script off in the background while updating the page. The only problem I have is that the php backend is returning a response of 0 rather than 1. I know the PHP is correct because I did it the old fashion way and it mails it off just fine.
First the PHP backend:
[CODE]<?php
if ( isset($_POST[‘LName’]) && isset($_POST[‘FName’]) && isset($_POST[‘Numba’]) && isset($_POST[‘Email’]) !=” ){
$text = ‘ ‘;
foreach( htmlspecialchars($_POST) as $name => $value) {
if (is_array($value)) {
$text .= sprintf(“%s: %sn”, $name, join(‘ ‘, $value));
} else {
$text .= sprintf(“%s: %sn”, $name, $value);
}
}
mail(‘test2@localhost’, ‘Form data’, $text);
echo ‘1’;
}
else
{
echo ‘0’;
echo $_POST[‘LName’]; //For testing, oddly enough the values aren’t returned
echo $_POST[‘FName’];
echo $_POST[‘Numba’];
echo $_POST[‘Email’];
}
?>
The javascript file
[CODE]function call(url){
ajaxObject = false;
if (window.XMLHttpRequest) { // if we’re on Gecko (Firefox etc.), KHTML/WebKit (Safari/Konqueror) and IE7
ajaxObject = new XMLHttpRequest();
if (ajaxObject.overrideMimeType) { // older Mozilla-based browsers
ajaxObject.overrideMimeType(‘text/xml’);
}
}
else if (window.ActiveXObject) { // and now for IE6
try {// IE6 has two methods of calling the object, typical!
ajaxObject = new ActiveXObject(“Msxml2.XMLHTTP”);
// create the ActiveX control
} catch (e) { // catch the error if creation fails
try { // try something else
ajaxObject = new ActiveXObject(“Microsoft.XMLHTTP”);
// create the ActiveX control (using older XML library)
} catch (e) {} // catch the error if creation fails
}
}
if (!ajaxObject) { // if the object doesn’t work
// for some reason it hasn’t worked, so show an error
alert(‘Sorry, your browser seems to not support this functionality.’);
return false; // exit out of this function
}
ajaxObject.onreadystatechange = ajaxResponse;
// DO NOT ADD THE () AT THE END, NO PARAMETERS ALLOWED!
ajaxObject.open(‘POST’, url, true); // open the query to the server
ajaxObject.send(null); // close the query
// and now we wait until the readystate changes, at which point
// ajaxResponse(); is executed
return true;
} // end function doAjaxQuery
function ajaxResponse() { // this function will handle the processing
// N.B. – in making your own functions like this, please note
// that you cannot have ANY PARAMETERS for this type of function!!
if (ajaxObject.readyState == 4) { // if ready state is 4 (the page is finished loading)
if (ajaxObject.status == 200) { // if the status code is 200 (everything’s OK)
// here is where we will do the processing
if(ajaxObject.responseText == ‘1’) { // if the result is 1
var p, np, text;
p = docuemnt.getElementById(‘flavor’);
np = document.createElement(‘p’);
text = document.createTextNode(“Congrats, you just sent an email to the Mobile Center!”);
np.appendChild(text);
p.appendChild(np);
}
/*else { // otherwise
alert(‘Not enough cash, Stranger!’);
}*/
} // end if
else { // if the status code is anything else (bad news)
alert(‘There was an error. HTTP error code ‘ + ajaxObject.status.toString() + ‘.’);
return; // exit
}
} // end if
// if the ready state isn’t 4, we don’t do anything, just
// wait until it is…
} // end function ajaxResponse
/*End Ajax*/
The HTML page
[CODE]<html>
<head>
<title>*Title*</title>
<link rel=”stylesheet” type=”text/css” href=”paint.css”>
<script type=”text/javascript” src=”blink.js”></script>
</head>
<body>
<div id=”the_new_chicken_wrap”>
<ul id=”nav”>
<li>
<a href=”#”><img src=”What.png” alt=”” onclick=clickTabs(‘what_android’)></a>
</li>
<li>
<a href=”#”><img src=”Home.png” alt=”” onclick=clickTabs(‘body’)></a>
</li>
<li>
<a href=”#”><img src=”Classes.png” alt=”” onclick=clickTabs(‘dem_classes’)></a>
</li>
</ul>
<div id=”body”>
<p id=”flavor”>
Please use the form on the right hand side to sign up for [removed]!
</p>
<img src=”droid_head.png” alt=”dat android be peepin'” id=”droid_head”>
<form action=”javascript:;” onsubmit=”call(‘submit.php’)”enctype=”application/x-www-form-urlencoded” method=”post” onsubmit=”return true”>
<ul>
<li>
<p class=”list”>Last Name.</p>
<input class=”entry” type=”text” name=”LName” size=”50″ maxlength=”250″ value=”Last Name?”>
</li>
<li>
<p class=”list”>First Name.</p>
<input class=”entry” type=”text” name=”FName” size=”50″ maxlength=”250″ value=”First Name?”>
</li>
<li>
<p class=”list”>Phone Number.</p>
<input class=”entry” type=”text” name=”Numba” size=”18″ maxlength=”18″ value=”Can we have it?”>
</li>
<li>
<p class=”list”>Email.</p>
<input class=”entry” type=”text” name=”Email” size=”25″ maxlength=”250″ value=”Better than Snail Mail!”>
</li>
<li>
<input class=”Button” type=”submit” name=”submit_info” value=”Done.” >
</li>
</ul>
</form>
</div>
</body>
</html>
If anyone can point out why the ajax returns 0 it would be great!!!!!!!!!!!