My head's exploding with this one, I'm looking for a concise step-by-step for escaping data submitted by a user before outputting to other users' browsers.
The process: 1) User 1 submits an ajax form (jquery) with a text field, sent as JSON. 2) PHP escapes the string and puts it in a DB using mysqli_real_escape_string(). 3) User 2 loads a page which requests the data via a jquery ajax request, receiving as JSON. The string is presented as an option in a form's select box.
I want to make sure that user 1 can't submit malicious javascript or html - in other words, I want all characters to be properly escaped. I'd like guidance on the steps to achieve this, what would need to be changed in the code below?
On submitting the form: (no escaping)
$.ajax({
url: '/ajax/insert.php', dataType: 'json',
data: {str: $("input").val()}, success: function(){}
)};
PHP insert into DB: (escaped)
mysqli_query(
"insert into tbl (str) values ('"
.mysqli_real_escape_string($link, $_REQUEST['str'])
."')");
JQuery get string, put into drop down (simplified) (put escaping here?):
$.ajax({
url: '/ajax/get.php', dataType: 'json',
data: {},
success: function(json){
$("select").html("<option>" + json.str + "</option>");
}
});
PHP for retrieving from DB (put escaping here?):
$res = mysqli_query($link, "select str from tbl where X");
echo json_encode(mysqli_fetch_assoc($res));
Thanks