Problem: – I could not save the ampersand (&) symbol to my database
Symptom: – The text was cut off at the point the ampersand should appear.
When inputting “Dog & Cat” it would appear in the database as just “Dog”
I had a text area which a user was typing in text, and whenever the form was submitted to save, the Jquery would get the text area contents with no problem as I could alert it out as normal.
I then sent this via Ajax to the PHP page, and collected the post data and tried to save it to the database, but it was constantly cut off. So I tried the following things. (Thinking that it was the inputting of the data where the problem was).
1 2 3 4 5 6 |
//NOTE : this is NOT the answer! $keywords = addslashes($keywords); $keywords = htmlspecialchars($keywords); $keywords = htmlentities($keywords); |
I gave up this method, and thought about the sending via ajax.
I eventually found the answer and had to encode the URL using the code below. the window.escape did the trick. It was getting confused as (&) in a url can confuse it. It was not a Mysql escape that was needed but a JavaScript escape!
1 2 3 4 5 |
//ANSWER str = "Dog & Cat"; escapedstring = window.escape(str); |