I use jQuery to send data from the client to the server and to store data in my database. Now I use the $.post-function from jQuery. I use it like the following
var queryUpd = "UPDATE settings SET round_duration='10'"
$.post("../../server/test.php", {func: "manipulate", query: queryUpd}, function(json, textStatus)
{
alert(json); //Outputs UPDATE settings SET round_duration =\\\'10\\\'
});
The php funcion "test.php" is simple:
<?php
echo json_encode($_POST["query"]); //send values to the client
?>
As you can see, the php-function gets an invalid query, since it adds two backslashes in front of the \'. So why does this happen and how can I solve this problem?
if it's php, then try somewhere in the bootstrap of your application
<?php
if (get_magic_quotes_gpc()) {
function stripslashes_gpc(&$value)
{
$value = stripslashes($value);
}
array_walk_recursive($_GET, 'stripslashes_gpc');
array_walk_recursive($_POST, 'stripslashes_gpc');
array_walk_recursive($_COOKIE, 'stripslashes_gpc');
array_walk_recursive($_REQUEST, 'stripslashes_gpc');
}
?>
You may need to specify that what you're getting back is JSON. Otherwise, jQuery guesses, and sometimes it's wrong:
$.post("../../server/test.php", {func: "manipulate", query: queryUpd}, function(json, textStatus)
{
alert(json); //Outputs UPDATE settings SET round_duration =\\\'10\\\'
},"json");
The comments on your original post are correct, however. You shouldn't send queries through ajax. You should send form data, and create queries based on that data, after checking it to make sure it's valid.