-1

I am working with Zend Framework. I have a form with some fields, all of them type="text", including an input called "Telephone". When submit, I am using Ajax to send data to Controller. Well here is my problem, if I type a + symbol, for example: +34-666666666 the data I receive is 34-666666666. The + symbol turns into a whitespace. This problem only happens with +, I have tried with all the symbols and theres no problem. I am going mad and I didn't found any solution in Google.

Juicy Scripter
  • 25,778
  • 6
  • 72
  • 93
Carlos
  • 105
  • 1
  • 2
  • 8

2 Answers2

4

The + symbol is used in URL's to represent whitespace. Your ajax submit is probably performing a GET request and the + in the URL string is getting transformed. Sanitize your input via javascript before submitting the ajax request with encodeURIComponent().

You might also find this question useful: Plus character in URL transformed to space on a linux box.

This question discusses encodeURIComponent: How to encode a URL in Javascript?

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
3

You need to encode your GET/POST data as described here. The JavaScript encodeURIComponent function can be used with a little twist*:

For application/x-www-form-urlencoded (POST), per specs, spaces are to be replaced by '+', so one may wish to follow a encodeURIComponent replacement with an additional replacement of "%20" with "+".

"&phone=" + encodeURIComponent("+34-666666666").replace(/%20/, "+");      // "&phone=%2B34-666666666"
"&phone=" + encodeURIComponent("+34-666666666 x123").replace(/%20/, "+"); // "&phone=%2B34-666666666+x123"

*Note: treatment of + character in a URL varies depending on whether it is used inside a path component or query string:

  1. http://example.com/page+1 -- file name page+1
  2. http://example.com/page%201 -- file name page 1
  3. http://example.com/?file=page+1 -- query string parameter file=page 1
  4. http://example.com/?file=page%201 -- query string parameter file=page 1
Salman A
  • 262,204
  • 82
  • 430
  • 521