DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   How to send & as data using _POST method using Ajax (http://www.daniweb.com/forums/thread115423.html)

rajivloharuka Mar 24th, 2008 6:16 am
How to send & as data using _POST method using Ajax
 
Hi All,
I am trying to send some data using Ajax and recieve that data in php code using _POST method.

But my data contains "&" and _POST method takes it as a seperating token and data after tht does not get passed. Does anyone has a solution to this.

Code snippet:

params = "q="+str+"&p="+str1;  //here str1 contains data which has &


  xmlHttp.open("POST",url,false);
  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.send(params);

And on server side when I use
$posted = &$_POST ;

it takes & as a seperator which is present as data.

Please someone let me know the workaround or approach to this.

johnsquibb Mar 24th, 2008 10:31 am
Re: How to send & as data using _POST method using Ajax
 
try encoding your str1 with encodeURIComponent

more about this at w3schools

rajivloharuka Mar 25th, 2008 1:37 am
Re: How to send & as data using _POST method using Ajax
 
Quote:

Originally Posted by johnsquibb (Post 567986)
try encoding your str1 with encodeURIComponent

more about this at w3schools



Thanks John for the input. This what I have done. If one wants to encode and decode the URL in javascript then above reference is fine. In my case i needed to encode at javascript and and decode inside php. The function to be used inside php code is

   preg_replace('/%([0-9a-f]{2})/ie', 'chr(hexdec($1))', (string) $p);  

$p is the string which has encoded value. And the first 2 parameters just does enough to decode back whatever [CODE ] encodeURIComponent [/code] has done.

Thanks

sagedavis Mar 27th, 2008 10:56 pm
Re: How to send & as data using _POST method using Ajax
 
just out of curiosity, would str_replace not have worked easier for this?
You have a string (as a variable at least) and you know what other string is inside of it (or could be inside of it), and you know what you want to replace it with.

http://us.php.net/manual/en/function.str-replace.php
Sage


All times are GMT -4. The time now is 1:40 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC