views:

208

answers:

4

Hi,

I have a form to 'create a tag'. Using the jQuery code below.

$("#createtag").submit(function() { //same as above, but for form submit instead of button click
   var newtag     = $('#newtag').attr('value');
   var type_id     = $('#type_id').attr('value'); 
   var company_id     = $('#company_id').attr('value'); 
         $('#createtag').load("../contacts/action_createtag.php?newtag="+ newtag + "&type_id=" + type_id + "&company_id=" + company_id).append('#createtags');
   return false;
  });

But i have just realised, if the 'newtag' variable includes a space, thats where it will end. Watching it through firebug, if theres no space the parameters appear like this:

company_id 5495
newtag test
type_id 2

But when a space is entered, it appears like this:

newtag test

Does anybody know why this could be happening? Why it is not passing the proper variables to the loaded page?

Thanks in advance!

Ryan

+1  A: 

I think you must encode your space as a %20 in the URL because if you pass it as a space it will mark the end of the URL. I don't think that jQuery load does any kind of special character escaping by itself.

RaYell
the space is the problem in this example, but what if someone put an ampersand in the field?
nickf
+6  A: 

Use encodeURIComponent() on the values:

$('#createtag').load("../contacts/action_createtag.php?newtag="+
  encodeURIComponent(newtag) + "&type_id=" + encodeURIComponent(type_id) +
  "&company_id=" + encodeURIComponent(company_id)).append('#createtags');
cletus
+3  A: 

You need to encode your variables.

Use encodeURIComponent()

nickf
+1  A: 

Encode your newtag value with encodeURI()

$('#createtag').load("../contacts/action_createtag.php?newtag="+ encodeURI(newtag) + "&type_id=" + encodeURI(type_id) + "&company_id=" + encodeURI(company_id)).append('#createtags');
Joel