When I pass a variable into a PHP script I firstly check if it isset
, is not blank, check that it's a string and that the length is greater than 1 and less than 255 (the DB varchar field limit) then I run it through this:
$f_name= stripslashes(htmlentities($_POST["f_name"], ENT_QUOTES));
I then use PDO prepare to update the f_name
field. When I then fetch it to put it on the page I decode it like:
html_entity_decode($row["f_name"], ENT_QUOTES);
Return it in a JSON array to the AJAX call that initiated the fetch and then parse it into an input field.
If the f_name field was saved with an apostrophe in it, everything works right up to the point I try and put it in the input field with JavaScript. It just cuts off the apostrophe and anything after it. How can I stop this from happening?
var response = "<input type='text' id='f_name' value='"+rdata.patient[0].f_name+"' onkeydown='onedit(1)' maxlength='255' />";