Cannot get data with spaces in database from AJAX POST request

I have a simple simple form with a text box and a button, and my goal is for the asynchronous request (jQuery: $ .ajax) to send the text to the server (PHP / mysql a la Joomla) so that it can be added to the base table data.

Here's the javascript that sends data from the client:

var value= $('#myvalue').val(); $.ajax( { type: "POST", url: "/administrator/index.php", data: { option: "com_mycomponent", task: "enterValue", thevalue: value, format: "raw"}, dataType: "text", success: reportSavedValue } ); 

The problem occurs when the user enters text with a space in it. The $ _POST variable that I get has all the spaces, so if the user enters " This line has spaces ", the server gets the value " Thisstringhasspaces ".

I work in search engines and have found many links that I need to use encodeURIComponent. So I tried, but now the value I get from $ _POST is " This20string20has20spaces ".

Thus, he seems to encode it as I would expect, only to exclude percent signs instead of spaces and leave hexadecimal numbers.

I'm really confused. It seems that this kind of question is being asked and answered everywhere on the Internet, and everywhere encodeURIComponent is welcomed as a silver bullet. But apparently, I’m fighting another breed of lycanthrope. Anyone have any suggestions?

+4
source share
2 answers

It turns out that there was additional filtering, I did not understand that I was performing. Since all this was done through Joomla, I used JRequest::getCmd('thevalue') instead of $_POST['theValue] . It turns out that this function filters out all these nasty characters, such as "%".

So, the final solution is to use encodeURIComponent on the client, as is customary on the Internet:

 var value = encodeURIComponent($('#myvalue').val()); 

And on the server to trade getCmd() for getVar() , which allows more control over filtering in combination with urldecode() :

 $value = urldecode(JRequest::getvar('thevalue', JREQUEST_ALLOWHTML)); 

Thanks again to Karim79 and Cesar. I give you Harker and Helsing - my heroes for the day! :)

+1
source

Could I try you please?

 var value = escape($('#myvalue').val()); 
0
source

Source: https://habr.com/ru/post/1308572/


All Articles