pconder
asked on
posting to php through ajax
I am stumpped on posting variables to a php file.
From my html I call 'calledFromHTMLform()'. This file generates an ajax variable and posts to a file called 'myPHPfile.php'.
The php file sends an email. The email is successfully sent but I cannot read any of the posted variables.
In the following code, the $message variable contains nothing.
Any input as to what I am missing?
From my html I call 'calledFromHTMLform()'. This file generates an ajax variable and posts to a file called 'myPHPfile.php'.
The php file sends an email. The email is successfully sent but I cannot read any of the posted variables.
In the following code, the $message variable contains nothing.
Any input as to what I am missing?
--------------------------------------------------------------------------------
function calledFromHTMLform() {
var ajaxVar = getAjaxRequestVariable();
ajaxVar.onreadystatechange = function() {
if(ajaxVar.readyState == 4) {
var myResponse = ajaxVar.responseText;
}
}
ajaxVar.open("POST", "myPHPfile.php", true);
ajaxVar.send('name=myname&username=myusername');
}
--------------------------------------------------------------------------------
function getAjaxRequestVariable() {
try { // Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) { // Internet Explorer Browsers
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) { // Something went wrong
alert("Your browser broke!");
return false;
}
}
}
return ajaxRequest;
}
--------------------------------------------------------------------------------
<?php
$email = 'you@domainname.com';
$subject = 'This is the subject';
$message = $_POST["name"];
$from = "me@domainname.com";
$headers = "From: $from";
if(mail($email,$subject,$message,$headers)) {
echo "Thank you for sending email";
} else {
echo "Can't send email to $email";
}
?>
--------------------------------------------------------------------------------
ASKER
I did that already. The string going to the post looks good (I think). The following is a sample string being sent to the post from the actual code - not the stripped and hard coded code that I posted with the question:
-------------------------- ---------- ---------- --
name=pc&username=myUser
-------------------------- ---------- ---------- --
--------------------------
name=pc&username=myUser
--------------------------
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Someone also recomended that I add:
ajaxVar.setRequestHeader(" Connection ", "close");
after the two lines you indicated. Works great. Thanks.
ajaxVar.setRequestHeader("
after the two lines you indicated. Works great. Thanks.
ajaxVar.send('name=myname&
Dump them to the screen to ensure they have data.