Snehasish Sarker Snehasish Sarker - 3 months ago 18
PHP Question

Remove '&' sign from string automatically in JavaScript

I am sending a string through JavaScript, but the problem is that it removes the '&' sign automatically. Before the Ajax request I have got the string perfectly, but on the server side I don't get any '&' in the string.

var user = 1; // ex: user_id
var message = "I like cricket & football"; // Example: message
var dataString = "user"=+ user + "&msg=" + message;
alert(message); // It shows my string perfectly: I like cricket & football
$.ajax({
type: "POST",
data:dataString,
url: rootPath()+"user/reply_msg_user",
success: function(data){}
});


After sending the datastring by the Ajax request on the server side it only shows:


I like cricket football

Answer

You can try this:

var message = encodeURIComponent('I like cricket & football');