views:

37

answers:

2

Trying to pass spaces along with ajax call.

'word' is been passed the same as 'word ' i believe so.

On the other hand two words need to be send completely with call.

'word second' but not the same as 'word second '

Should I trim before call or do this on server side script? How can I send spaces as well?

A: 

The simplest way, I think, is to encodeURIComponent string in javascript before sending xmlhttprequest, and then urldecode it in PHP

Michał Kluczka
urldecode is done automtically
zerkms
A: 

To allow a parameter to include spaces, etc. you will want to use the javascript escape() [W3Schools] function.

escape( 'hello world ' ) = 'hello%20world%20';

The handling on the PHP side will automatically decode/unescape the parameter, restoring the spaces (along with any other characters which cannot be part of a parameter's value when sent through AJAX, such as "=" or "&".

Within PHP, if you are wanting to strip off any leading or trailing spaces, you can use the PHP trim() [PHP.net] function.

trim( 'hello world ' ) = 'hello world';
Lucanos