If I want to put a value between single quotes in Javascript how can I sanitize/encode it so that any quotes in the value doesn't cause issues?
I also want to then use this value in a query string which I will then be passing to PHP.
Whatever is used I need to be able to decode it back to it's normal value with PHP.
Example:
$foo = "Hey, what's up!?"; // PHP
getGrades('<?=$foo?>'); // JS Function
function getGrades(var) {
// Set file to get results from..
var loadUrl = "ajax_files/get_grades.php";
// Set data string
var dataString = 'grade=' + var;
// Run the AJAX request
runAjax(loadUrl, dataString);
}
function runAjax(loadUrl, dataString) {
jQuery.ajax({
type: 'GET',
url: loadUrl,
data: dataString,
dataType: 'html',
error: ajaxError,
success: function(response) {
someFunction(response);
}
});
}
// get_grades.php file
$grade = $_GET['grade']; // We now want this value to be it's normal value of "Hey, what's up!?";