tags:

views:

118

answers:

3

When we type 'a d' (a space d) the .val returns a+d using a plus sign. I need the actual string of 'a d'? How do I do this?

HTML textbox

<input type="text" name="term" value="" id="term" title="term" size="30px"/>

JQuery

function() { $('#results').load('search.aspx', $('#term').val()); }
A: 

.load is just an abstraction of the ajax methods, so the value is being Url encoded as it's sent to search.aspx. Are you noticing the encoding in firebug (or equivalent) or when you're storing the value on search.aspx page?

ScottE
A: 

You may need to decode it on the server. In ASP.NET Server.UrlDecode() will do for you. In PHP there is also a urldecode function:

http://us2.php.net/manual/en/function.urldecode.php

James Wiseman
Thanks to all responses. server.urldecode does the job. The only problem is if someone types + in their search.
That's where the JS encodeURIComponent() comes in handy ;-)
JorenB
If you're handing encoding and decoding of URL on the server and client correctly it shouldn't matter what the user types. Thorough testing of non-alphnumeric fields in HTML inputs is an essential part of the testing of any web application.
James Wiseman
A: 

I'm pretty sure the .val() returns the string perfectly, but that the AJAX module does some URL encoding on your data. I encountered this problem somewhere too... Hold on, I'll try to find the solution I used back then.

Edit:

This is the piece of code that would serialize a form for me:

var data = '';
for(var i = 0; frm.elements[i] != undefined; i++) {
  data += frm.elements[i].name + '=' + encodeURIComponent(frm.elements[i].value);
  if(frm.elements[i + 1] != undefined) data += '&';
}

in which frm is your form, obviously. You'll probably only need the encodeURIComponent()-function if you're only submitting the lone string.

JorenB