Guido
Guido

Reputation: 21

Jquery load in with parameters

I have a question because i'm trying to write a load method and to pass through it parameters. I have to populate a select:

$("#select").load("page.php?parameter1="$("#parameter").val());

All looks fine, but when I pick some choice in my select, Firebug gives me an error because the parameter that I send has a space like this: for example if I want to send "New York", Firebug gives me an error that cannot recognize this parameter because of the separation between New and York.

Can anybody help me?

Thanks.

Upvotes: 2

Views: 798

Answers (1)

adeneo
adeneo

Reputation: 318182

You'll have to URL encode strings used for query parameters if they contain certain characters, like spaces

var query = encodeURIComponent( $("#parameter").val() );

$("#select").load("page.php?parameter1=" + query);

As a sidenote, jQuery has something built in for this as well, which makes it easier to construct querystrings

var query = $.param({
                     parameter1: $("#parameter").val()
                   });

$("#select").load("page.php?" + query);

One could any numbers of key/value pairs in $.param and get a valid querystring back.

Or, one could pass the data directly to load() , which in turn calls $.param internally, this is probably the best and easiest way to handle this sort of thing

$("#select").load("page.php", {parameter1 : $("#parameter").val()});

Upvotes: 2

Related Questions