0

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

8
  • Why would you want to do that when you have post and data. Just do data+="&pg=2&company=motorola,lenovo&pricerange=2" Commented Jan 3, 2017 at 7:13
  • You have the URL there and you don't know how to copypaste that to the URL? Or concatenate strings? Commented Jan 3, 2017 at 7:14
  • make a get request although .serialize() is serializing the string for you in the same manner but with ? at the start. Commented Jan 3, 2017 at 7:15
  • I have url to be like that becaue my pagination and sorting is not working Commented Jan 3, 2017 at 7:21
  • Do you want to send the variables as an URL along with the serialized data ? Commented Jan 3, 2017 at 7:21

1 Answer 1

1

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;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Could you elaborate? Do you have some kind of error? With var_dump($_GET) in your PHP file do you see anything?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.