开发者

Pass special characters in PHP

开发者 https://www.devze.com 2023-03-17 23:40 出处:网络
I have a text field where where the user can pass wild cards - more specific to the question they can use \'%\' character.

I have a text field where where the user can pass wild cards - more specific to the question they can use '%' character.

I am using ajax to get the value and send it to a PHP file. If I enter '%BA' in the text file and retrieve the value using

document.getElementById('textfield').value

This actually gets '%BA'. I am using POST method to send it to a PHP file. But the variable displays as "�" in the web browser and inserts " ° - degree s开发者_如何学编程mall o" in the database.

I am sure there are other cases that I am not aware of as well. Is there a function in PHP to escape the special characters or any other way to get the exact string?

Edit: This may be a guess but doing escape(document.getElementById('textfield').value) to send the value and using urldecode($values[3]) to retrieve the value doesn't work. Maybe it's a js to PHP problem.

Update: urldecode will not work. Read the first comment in urldecode. Used the function there. Solved.


while passing the value using ajax , you just encode the value with encodeURIComponent() function and use urldecode() function to decode it in the php file. This might solve the issue.


You could encode the characters with urlencode (and maybe htmlspecialchars too) before storing it in the database, and use urldecode ( and maybe htmlspecialchars_decode) to decode them before displaying to the user.


You can use escape in javascript i.e.

escape(document.getElementById('val'))
0

精彩评论

暂无评论...
验证码 换一张
取 消