0

I am making a program to submit a form with Ajax By combining all values in variable datastring and sending it with ajax method..

suppose

var varname="ram";
var varage=18;

and var datastring='name='+varname+'&age='+varage;

This works well but

var varname="ram & shyam";
var varage=18;

and var datastring='name='+varname+'&age='+varage;

When a variable already contain & in it then value received by php code as $_POST['name'] has value only 'ram' not 'ram & shyam'

Please tell a solution for this problem

2 Answers2

0

try to replace

 var varname="ram & shyam";
 var sendData = varname.replace("&","%26");

another useful links

How can I send the "&" (ampersand) character via AJAX?

http://www.w3schools.com/tags/ref_urlencode.asp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

Community
  • 1
  • 1
marti_
  • 130
  • 5
  • 14
0

Replace & with %26 while sending the data

var varname="ram & shyam";
var varage=18;

var newvarname=varname.replace("&","%26");
var datastring='name='+newvarname+'&age='+varage;
John
  • 107
  • 9