Add get parameters with a select/option value in the url

Asked

Viewed 289 times

0

I have on the page that lists the properties of my site a search form and a select that makes the result ordering.

inserir a descrição da imagem aqui

The problem: Doing a search in the form and passing the values via get Ctr=0&min=20000&max=999999&suites=3 etc. and right after choosing an sorting option the url I lost these values because the page is updated and makes an ordering of the general result. When you select a sort option, I want to sort the result that appears on the page and so I need the previous filter parameter to remain in the url and add the sort parameter.

I am sending the action to sort this way and so I miss the values that are in the url:

$("select.ordenacao").change(function() {
        $("select.ordenacao").each(function() {
          str = $( this ).val();
          //alert(str);
            //$("#ordenar").submit();
            location.href = '?ordenacao=' + str;
        });
    });

anyone has any suggestions?

  • Why don’t you do that in ajax, or restful? So you wouldn’t need to refresh the page.

  • If you show me an example of how to do this with Ajax I am grateful, still can not understand very well how to make requests with Ajax in php files and show the result in a return.

  • 1

    ajax works asynchronously, through an anonymous function using callback, you have a return executed, within an element that will receive this action, without having to give a refresh again, it does so in a masked way, in a php script.

1 answer

1

I suggest doing this in ajax:

$("select.ordenacao").change(function() {
        $("select.ordenacao").each(function() {
          str = $( this ).val();
              ordenar(str);
            //$("#ordenar").submit();
        });
});
var ordenar = function(str) {
       var saida = null;
             $.get('?ordenacao=' + str, function(e){
              saida = jQuery.parseJSON(e);       
             });
       return saida;
 };

Another option is to capture the URL values and give your refresh again:

   $("select.ordenacao").change(function() {
            $("select.ordenacao").each(function() {
              str = $( this ).val();
              //alert(str);
                //$("#ordenar").submit();
                 location.href='?ctr='+getValueQS("ctr")+'&min='+getValueQS("min")+'&max='+getValueQS("max")+'&suites='+getValueQS("suites")+'&ordenar='+str;
            });
        });


    function getValueQS(key) {  
      return unescape(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + escape(key).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));  
    }  
  • It worked with the second option, but the values of the parameter this path passed empty: list? ctgr=&city=&s=&d=&v=&vmin=&Vmax=&id=&Submit=&sort=recent. Doubt: how can I set a default value for each parameter?

  • I tried so but did not succeed: Location.href='list? ctgr='+getValueQS(0)+'&city='+getValueQS(0)+'&s='+getValueQS(0)+'&d='+getValueQS(0)+'&v='+getValueQS(0)+'&vmin='+getValueQS(0)+'&vmax='+getValueQS(99999999999999)+'&submit='+getValueQS("submit")+'&ordenacao='+str;

  • I didn’t understand, how so it worked, and it didn’t work?

  • The method works... if you run it on the console giving the output on console.log() it is returning the string in the query. I don’t understand this zero.

  • You should think about doing this with ajax, you would suffer less.

  • It worked, only a few parameters the values are empty:

  • list? ctgr=&city=&s=&d=&v=&vmin=&Vmax=&id=&Submit=&sort=recentes---------------- Of the above parameters these are passed empty: &s=&d=&v=&vmin=&Vmax=----------------.

  • in the latter case just do this: list? ctgr=<? php echo $_GET['ctgr']; ? >&city=<? php echo $_GET['city'];?>...

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.