I wrote a php and jquery-ajax script that works good. But the following scripts work slow and I found that this type of script is vulnerable to XSS attack, I found something regarding it in this link's first answer please see the first answer. Here is my jquery code:
<script>
$(".question").keyup(function(){
var question = document.forms["myForm"]["question"].value;
$.ajax({
type: "POST",
url: "http://www.example.com/view_result.php",
data: { question: question }
})
.done(function( msg ) {
$("#show_typing_question").html( msg );
});
});
</script>
Here is the view_result.php script:
<?php
function html_converter($val)
{
$turned = array('`<b>','`</b>','`<mark>','`</mark>');
$turn_back = array('<b>','</b>','<mark style="background-color:#999;">','</mark>');
$val = str_replace( $turned, $turn_back, $val );
$val = preg_replace('/(?:(?:\r\n|\r|\n)\s*){2}/s', "\n\n", $val);
return $val;
}
$question = $_POST['question'];
$question = htmlspecialchars($question, ENT_QUOTES, 'UTF-8');
echo html_converter($question);
?>
How can I write the whole script only in jquery function without using ajax. Is this script safe?