Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping slashes in jQuery for passing paths in an AJAX request

I have a problem with slashes! I have some jQuery for handling generic dialogs on a page. In some cases the fields are passing /-delimited paths...

var fieldValues = [];
// pull values from all the fields belonging to the dialog...
$.each($(this).find('input, textarea, select'), function(n,field) {
  // escape the path fields
  var value = escape($(field).val().replace(/\//g,'__slash__'));
  //alert ($(field).attr('id')+'='+value);
  if(id != '' && value != '') {
    fieldValues.push(id+'='+value);
  }
});

This code works, but I have a manual stripping out of __slash__ when I get to the php end of things. Is there some encoding function I'm missing that would let me drop the clumsy-looking replace?

like image 716
Dycey Avatar asked Aug 07 '09 19:08

Dycey


1 Answers

You can use Javascript's built-in encodeURIComponent() and then PHP's rawurldecode() should decode it in PHP once received.

like image 199
Andrew Marshall Avatar answered Sep 27 '22 18:09

Andrew Marshall