My database contains records with included HTML scripting tags. I have read many different options on how to handle this scenario while also using json_encode/AJAX.
Should I use a JS function to escape special characters client side or might there be a PHP solution I'm missing?
Edit Assumption: The user does not want to strip/remove the html tags, just wants a way or a suggestion in to encoding them either on the server or client side!
PHP (process.php):
$records = array();
if($results = $db->query("SELECT * FROM cust_tbl")) {
if($results->num_rows) {
while($row = $results->fetch_object()) {
$records[] = $row;
}
echo json_encode($records);
$results->free();
}
}
AJAX:
function show() {
clear();
$.ajax({
type: "POST",
dataType: "json",
url: "process.php",
data: "action=show",
success: function(data) {
$.each(data, function(index, data) {
$('#tablebody').append('<tr>');
$('#tablebody').append('<td>' + data.JL_JOB_DATE + '</td>');
$('#tablebody').append('<td>' + data.JL_YR + '</td>');
$('#tablebody').append('</tr>');
});
}
});
}