4

I have a script that makes an ajax request passing 3 different data : eventName / ticketsToWin / totalWinners.

My whole process was working perfectly until I had this case : "SCi+Tec" as the eventName variable. Here is what looks like the data of the request just before sending :

name=Sci+Tec&ticketsToWin=1&totalWinners=2

But then, on the PHP side, if I dump the _GET array, I have this :

array(4) {
  ["name"]=> string(7) "Sci Tec"
  ["ticketsToWin"]=> string(1) "1"
  ["totalWinners"]=> string(1) "2"
  ["_"]=> string(13) "1372359516001"

}

The '+' character is missing in the name, which breaks everything that comes after. Any idea why ?!

Thans!

Rommy
  • 497
  • 1
  • 4
  • 16

4 Answers4

3

encode your string:

name=Sci%2BTec&ticketsToWin=1&totalWinners=2

Or easier:

var str = 'name=Sci+Tec&ticketsToWin=1&totalWinners=2';
var encoded = encodeURIComponent(str);

see the docs or this Question

Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152
0

I'm pretty sure that the plus sign in URLs is used instead of a space, like in a google search, you give the following query:

"How to send an email",

It will show in the URL as:

"How+to+send+an+email".

Try POSTing it.

thephpdev
  • 1,097
  • 10
  • 25
0

In Urls, spaces in query strings are automatically replaced by plus signs. So when the server gets Sci+Tec, it thinks there is supposed to be a space there. You will need to escape it with its url encoding: %2B.

More on Url encoding: http://www.w3schools.com/tags/ref_urlencode.asp

Omada
  • 784
  • 4
  • 11
0

you could either use a java url encode or a javascript encoder

URLEncoder.encode(yourString)
happybuddha
  • 1,271
  • 2
  • 20
  • 40