943,879 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 3020
  • PHP RSS
Mar 24th, 2008
0

How to send & as data using _POST method using Ajax

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  1. params = "q="+str+"&p="+str1; //here str1 contains data which has &
  2.  
  3.  
  4. xmlHttp.open("POST",url,false);
  5. xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  6. 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.
Last edited by peter_budo; Mar 24th, 2008 at 7:20 am. Reason: Keep It Organized - please use [code] tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rajivloharuka is offline Offline
2 posts
since Mar 2008
Mar 24th, 2008
0

Re: How to send & as data using _POST method using Ajax

try encoding your str1 with encodeURIComponent

more about this at w3schools
Last edited by johnsquibb; Mar 24th, 2008 at 11:33 am. Reason: typo
Reputation Points: 14
Solved Threads: 14
Junior Poster in Training
johnsquibb is offline Offline
84 posts
since Nov 2007
Mar 25th, 2008
0

Re: How to send & as data using _POST method using Ajax

Click to Expand / Collapse  Quote originally posted by johnsquibb ...
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
Last edited by rajivloharuka; Mar 25th, 2008 at 2:38 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rajivloharuka is offline Offline
2 posts
since Mar 2008
Mar 27th, 2008
0

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
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
sagedavis is offline Offline
86 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to arrange a Profile.
Next Thread in PHP Forum Timeline: send e-mail every new entry on table on db





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC