/    Sign up×
Community /Pin to ProfileBookmark

Trouble with a PHP mail script and XHttpRequest (aka AJAX)

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’];
}

?>[/CODE]

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*/[/CODE]

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>[/CODE]

If anyone can point out why the ajax returns 0 it would be great!!!!!!!!!!!

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@kristovaherFeb 15.2012 — While the code is a mess, I don't see where you are actually sending those $_POST variables to AJAX. It looks like it makes an empty request without those variables. There's no post variable handling in your call() method from what I can see. You literally call .send(null) there, so the script never gets any of your variables.

Also you have two onsubmit events as well as whitespace problem in <form> from what I can see.

Catch up on reading here.
Copy linkTweet thisAlerts:
@Lawrence_FieldsauthorFeb 16.2012 — Ohhhh, I see. According to that link I see what I did wrong. According to that link I didn't actually send the variables. I was just going off a tutorial and it never mentioned I had to do that!

I need these two tibits here:
[CODE]xmlhttp.open("POST","ajax_test.asp",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("fname=Henry&lname=Ford");[/CODE]


The only problem I have is within .send. According to this it's defining the info you want to pass. I was trying to get the info from the forms themselves. Does this mean I have to pass along the php variables like [CODE]"fname='<?php $_POST['fname'] ?>'"[/CODE]
Copy linkTweet thisAlerts:
@Lawrence_FieldsauthorFeb 16.2012 — Okay, I read up on the XHttprequest on Wc3 and I'm still having the same problem. I added this to the script.


JS
[CODE]
//****in contruction an Object, Methods...and Pylons.

function mailMan(Lname, Fname, Numba, Email){

//Dem Props
this.Lname = Lname;
this.Fname = Fname;
this.Numba = Numba;
this.Email = Email;


//Dem Methods
this.newLname=newLname;
this.newFname=newFname;
this.newNumba=newNumba;
this.newEmail=newEmail;

}

//Define Dem Mehtods my Nig. Naw Meen.
function newLname(datLname){

this.Lname = datLname;

}

function newFname(datFname){

this.Fname = datFname;

}

function newNumba(datNumba){

this.Numba = datNumba;

}

function newEmail(datEmail){

this.Email = datEmail;

}

//End contruction

var mail = new mailMan();

//-------------------------------------//

//Where the info would be sent

ajaxObject.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //define content type
ajaxObject.send("Fname="+mail.Fname+"&Lname="+mail.Lname+"&Numba="+mail.Numba+"&Email="+mail.Email); // close the query and send the info.

[/CODE]



And this to the HTML

[CODE]<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?" onkeyup="mail.newLName(this.value)">
</li>
<li>
<p class="list">First Name.</p>
<input class="entry" type="text" name="FName" size="50" maxlength="250" value="First Name?" onkeyup="mail.newFName(this.value)">
</li>
<li>
<p class="list">Phone Number.</p>
<input class="entry" type="text" name="Numba" size="18" maxlength="18" value="Can we have it?" onkeyup="mail.newNumba(this.value)">
</li>
<li>
<p class="list">Email.</p>
<input class="entry" type="text" name="Email" size="25" maxlength="250" value="Better than Snail Mail!" onkeyup="mail.newEmail(this.value)">
</li>
<li>
<input class="Button" type="submit" name="submit_info" value="Done." >
</li>
</ul>
</form>[/CODE]


And I'm still getting a reply of 0 sent back and only two of the input boxes returning errors. I figured grabbing the info via an object would help me send the info but that's not working either.
Copy linkTweet thisAlerts:
@Lawrence_FieldsauthorFeb 17.2012 — Nevermind, I just figured out what's wrong with the post variables.

function was named newLname

I had newL[B]N[/B]ame when passing a value to the .js, which would in turn pass it to the PHP. That's why I wasn't getting the info from the inputs fields. However I STILL am getting a zero returned. Don't know why though.

EDIT

nevermind again, same problem as before but in PHP

I had Lname there istead of LName.

However after finding those minor errors I finally got a 1 back!!!!!!!!

Yeah boi!
Copy linkTweet thisAlerts:
@kristovaherFeb 17.2012 — Nevermind, I just figured out what's wrong with the post variables.

function was named newLname

I had newL[B]N[/B]ame when passing a value to the .js, which would in turn pass it to the PHP. That's why I wasn't getting the info from the inputs fields. However I STILL am getting a zero returned. Don't know why though.

EDIT

nevermind again, same problem as before but in PHP

I had Lname there istead of LName.

However after finding those minor errors I finally got a 1 back!!!!!!!!

Yeah boi![/QUOTE]


These responses have been amusing to read!

But good that you got it working ?
×

Success!

Help @Lawrence_Fields 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,
)...