Jump to content


Photo

retrieving variables in php using $_POST from ajax


  • Please log in to reply
2 replies to this topic

#1 tinks

tinks

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 09 March 2011 - 10:55 PM

hi, i cannot seem to retrieve using $_POST in php the variables that I passed using ajax.

i have something like:

{
temp1 = "root";
temp2 = "false";
var url = "sample.php";
var arg1 = "arg1="+ temp1;
var arg2 = "arg2="+ temp2;
var paramPost = arg1+"&"+arg2;

...
ajax.postIt(paramPost,temp2);
}



function postIT(param, temp2) {
// make a HTTP GET request to the URL asynchronously
...
request.open("POST",url, true);
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.setRequestHeader("Content-length",param.length);
request.setRequestHeader("Connection", "close");
request.send("param="+param);

}



in my file.php, i have:

...

if (isset($_POST['param']))
{
$args1 = $_POST['arg1']; //gives out blank
$args2 = $_POST['arg2']; //gives out false value (just right)

}


i also tried:

if (isset($_POST['param']))
{
$args1 = $_POST['param']; //gives out "arg1=root"
$args2 = $_POST['arg2']; //gives out false value (just right)

}


am i doing it right? how can i retrieve just the value of $arg1 insteas of the
'name-value' pair?

help! thanks a lot!



#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 09 March 2011 - 11:12 PM

Expand out the following lines:
var arg1 = "arg1="+ temp1;
var arg2 = "arg2="+ temp2;
var paramPost = arg1+"&"+arg2;

// param = paramPost

request.send("param="+param);
You'll notice the post you're sending back then is:
param=arg1=root&arg2=false
Do you see the problem now?

#3 tinks

tinks

    Young Padawan

  • Members
  • Pip
  • 8 posts

Posted 10 March 2011 - 01:33 AM

yey! got it now! cant believe i appended that "param" !

thanks a loooooooot!








0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users