-1

This has been asked before but mine is different. I cant rub my head around this. I have a wysiwyg form that is being fetched by jquery and saved by ajax

var content=$('.inputfield').val();

fields='front='+fcarddetails;
 $.ajax({
  method:'POST',
  url:actionpage,
  data:fields,
   beforeSend:function()
   {
 $("#processing").show();
   },

  complete:function ()
   {
  $("#processing").hide();  
   },
  success: function(feedback)
    {
    }    etc.

When '&' is added to the field, the whole input is messed up. I a have handled all the html escapes, filters and special characters. But the code gets broken even before it reaches php action page. I cant convert '&' to & because it still contains '&'. Please help, problem is in js, not php but you can prove me otherwise. Thanks in advance.

jonah
  • 213
  • 4
  • 16
  • URL encode it. `&` is a key symbol in query strings for separating data. –  Aug 05 '15 at 06:57
  • *"This has been asked before but mine is different."* Almost certainly not. In any case, link to the previous questions you've read, and say **how** you think yours is different. – T.J. Crowder Aug 05 '15 at 06:58
  • &T.J. Crowder, its duplicatate no doubt. My bad, I didn't know its an ajax issue so I was searching wrongly. Thanks – jonah Aug 05 '15 at 07:20

1 Answers1

1

Encode the string using encodeURIComponent.

The encodeURIComponent() method encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).

var fcarddetails = 'Tom&Jerry';

fields = 'front=' + encodeURIComponent(fcarddetails);

document.write(fields);
Tushar
  • 85,780
  • 21
  • 159
  • 179