Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the querystring when I submit my GET form using JQuery?

Suppose that I have a simple form in my page like this :

<form action="/properties/search" method="GET" id="form_search">
  <p>
    <label for="price">Min price:</label>
    <input type="text" name="min_price" id="min_price">
  </p>
  <p>
    <label for="price">Max price:</label>
    <input type="text" name="max_price" id="max_price">
  </p>
  <p>
    <input type="submit">
  </p>
</form>

When I submit my form, I have the following url :

http://.../properties/search?min_price=100000&max_price=200000

I want to change this url to have :

http://.../properties/search?price=100000,200000

To do that, I'm using JQuery and the JQuery querystring plugin :

$(document).ready(function() {
    $("#form_search").submit(function() {
        var querystring = rewrite_interval_qstring();
        // querystring equals "?price=100000,200000" -> exactly what I want !

        // ???
    });
});

How can I change (comment "???") the submit url ? I have tested the following instructions separately, but it does not work.

window.location = querystring;
window.location.href = querystring;
window.location.search = querystring;
like image 638
Sandro Munda Avatar asked May 22 '11 11:05

Sandro Munda


People also ask

How can I add or update a query string parameter?

set('PARAM_HERE', VALUE_HERE); history. pushState(null, '', url); This will preserve everything about the URL and only change or add the one query param. You can also use replaceState instead of pushState if you don't want it to create a new browser history entry.

What is request QueryString?

The value of Request. QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request. QueryString(parameter).

How do you parse in QueryString?

The ParseQueryString method uses UTF8 format to parse the query string In the returned NameValueCollection, URL-encoded characters are decoded and multiple occurrences of the same query string parameter are listed as a single entry with a comma separating each value.


1 Answers

You're almost there. Intercept the submit event (as you are doing), extract the min and max values, build your url and set window.location.href

$(document).ready(function() {
    $("#form_search").submit(function(event) {
        event.preventDefault();
        $this = $(this);
        // var url = rewrite_interval_qstring();
        var min_price = $('#min_price').val();
        var max_price = $('#max_price').val();
        var url = $this.attr('action') + '?price=' + min_price + ',' + max_price;
        window.location.href = url;
    });
});
like image 74
Rob Cowie Avatar answered Oct 13 '22 00:10

Rob Cowie