Siddharth Chauhan
Siddharth Chauhan

Reputation: 92

How to append url using javascript in particular pattern

I want to append variable data in js in below code in url

$(document).on('change','.sort_rang',function(){
   var url = "ajax_search.php";
   //console.log($("#search_form").serialize());
   var data = $("#search_form").serialize();
   //data += "&pn="+ <?php echo $_GET['pn']; ?>;
   //console.log(data);
   $.ajax({ 
     type: "POST",
     url: url,
     data: data,
     success: function(response)
     {                  
        $('.ajax_result').html(response);
     }               
   });

  return false;
});

How to append the url in below format,

?pg=2&company=motorola,lenovo&pricerange=2 I want to append url in ajax_search.php

after var_dump($_REQUEST). I'm getting this

array(4) { ["company"]=> array(1) { [0]=> string(6) "Lenovo" } 
["category"]=> array(1) { [0]=> string(6) "mobile" } ["pricerange"]=> 
string(1) "1" ["pricesort"]=> string(1) "1" }

From this i want to append on above format

Upvotes: 0

Views: 89

Answers (1)

Cliff Burton
Cliff Burton

Reputation: 3744

If your service (ajax_search.php) requires the GET method then you can simply change the type parameter of your $.ajax request from type: "POST" to type: "GET" then jQuery does the job, you don't need to append the string to the URL by hand.

$(document).on('change','.sort_rang',function(){
    var url = "ajax_search.php";
    //console.log($("#search_form").serialize());
    var data = $("#search_form").serialize();
    //data += "&pn="+ <?php echo $_GET['pn']; ?>;
    //console.log(data);
    $.ajax({ 
        type: "GET", // <-- Note the change here from POST to GET
        url: url,
        data: data,
        success: function(response) {                  
            $('.ajax_result').html(response);
        }               
    });
    return false;
});

Upvotes: 1

Related Questions