I am passing variables to submit file via AJAX but I have problem when I’m passing variable that contains “&” character. I know why, because it’s messing the string like id=1&name=blabla&something=q&a.
But how can I make it work?
Someone wanted code, here it is
$("#btnCatEditSubmit").click(function()
{
$.ajax(
{
type: "POST",
url: "catEditSubmit.php",
data: "catID=<?= $catID ?>" + "&catName=<?= $catName ?>",
success: function(data)
{
alert(data);
}
});
});
catName might be “Something & Something” and then I have problem.
You have to encode / escape them.
In PHP, you can do that with the
urlencode()function:So in your code, do that:
Or even better:
(jQuery allows you to pass a hash of key-value pairs directly, so jQuery can take care of escaping for you. Here
json_encodeformats your array in a way understandable by javascript).In javascript, you can do that with the encodeURIComponent() function:
But as you are using jQuery, you can also pass variables directly with a hash.
Instead for doing this:
Do this:
And jQuery will escape everything for you.