AJAX post method: Variables are not passed to target php
I am trying to send two pieces of info to a php. 1-) tent = zuzu 2-) zart = gagi
target php simply echoes what I send so that I can check if it's working. This is the javascript:
function boka ()
{
var mesparam = "tent=zuzu&zart=gagi";
if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
else {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) {document.getElementById("response").innerHTML=xmlhttp.responseText;} }
xmlhttp.open("POST","/mysite/oxifa/oxifat.php?tent=zuzu&zart=gagi",true);
//xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//xmlhttp.setRequestHeader("Content-length", mesparam.length);
//xmlHttp.setRequestHeader("Connection", "close");
xmlhttp.send(mesparam);
}
This is oxifat.php that recieves the request:
<?php
echo " sign1 <br>";
echo next($_POST);
echo next($_POST);
echo next($_POST);
echo next($_POST);
echo next($_POST);
echo $_POST['tent'];
echo $_POST['zart'];
echo $_REQUEST['tent'];
echo $_REQUEST['zart'];
echo "<br> sign2";
?>
As you can see I've included all sorts of things to echo out whatever is in $_POST but apparently there is nothing there and this is the response I get:
sign1
Notice: Undefined index: tent in C:\wamp\www\mysite\oxifa/oxifat.php on line 16
Notice: Undefined index: zart in C:\wamp\www\mysite\oxifa/oxifat.php on line 17
Notice: Undefined index: tent in C:\wamp\www\mysite\oxifa/oxifat.php on line 18
Notice: Undefined index: zart in C:\wamp\www\mysite\oxifa/oxifat.php on line 19
sign2
three lines about the "setRequestHeader" are in comment status. If I include them, I don't even get sign1. No response. What I figure out from this is everything is OK but I don't seem to understand how to use the post method to pass data to php. How do I do this? I have read everything on the net. I 开发者_JAVA技巧only do not know what "setRequestHeader" is for. One more thing: If I put ?tent=zuzu&zart=gagi at the end of target URL, $_REQUEST thing works. But that's GET and not what I'm trying to do. What is the $_POST's deal?
Could you please try to invoke following code
function getXMLObject() {
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");// For Old Microsoft Browsers
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");// For Microsoft IE 6.0+
}
catch (e2) {
xmlHttp = false;// No Browser accepts the XMLHTTP Object then false
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();//For Mozilla, Opera Browsers
}
return xmlHttp;// Mandatory Statement returning the ajax object created
}
var xmlhttp = new getXMLObject();//xmlhttp holds the ajax object
//use this method for asynchronous communication
function doRequest(params, callback) {
if (xmlhttp) {
xmlhttp.open("POST", "your_script.php?" + params, true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
callback(xmlhttp.responseText);
}
else {
alert("Error retrieving information (status = " + xmlhttp.status + ")\n" + response);
}
}
};
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(null);
}
}
and in your PHP script first thing off write
print_r($_POST);
You are passing in the parameters with the URL which is for GET, not the right way for POST.
See: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
Try taking the parameters off in the request here:
xmlhttp.open("POST","/mysite/oxifa/oxifat.php?tent=zuzu&zart=gagi",true);
And then uncommenting these:
//xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //xmlhttp.setRequestHeader("Content-length", mesparam.length); //xmlHttp.setRequestHeader("Connection", "close");
You need to uncomment those 2 lines, otherwise you the php $_POST
is not going to pick it up. also, as stonemonkey77 answered, you are also passing the parameters in the get url.
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", mesparam.length);
精彩评论