skip to Main Content

I am trying to pass a string through ajax, however the string is a query string taken from a search and looks something like this:

search=&site=0&salesperson=0&referral=0&product=0&estimate=0&sort=date&open=on&filter_sbmt=Filter+Prospect&limit=30

So when I pass it through ajax as the variable url=search=&site=0... it sets the $_POST['url']="search=", and then separates &site as a new post, rather than keeping the entire string in one.

$.ajax({
    type    : 'POST',
    url     : '//'+base_url+'/ajax2/customer-search.php',
    data    : 'url='+url,
    success : function(data) {
        $('#customers_table').html(data);
    }
});

I have tried decoding it with json both on the php side and javascript side. This did not help either. I am out of ideas.

How can I get this string "search=&site=0&salesperson=0&referral=0&product=0&estimate=0&sort=date&open=on&filter_sbmt=Filter+Prospect&limit=30" on to ajax/customer-search.php

2

Answers


  1. You can use parse_str()

    $params = array();
    parse_str($_POST['url'], $params);
    
    print $params["salesperson"];
    
    Login or Signup to reply.
  2. jQuery will encode the data for you if you pass an object and not a string.

    data    : { url }
    

    Alternatively, modern browsers have a URLSearchParams object that will encode the data for you:

    var searchParams = new URLSearchParams();
    searchParams.append("url", url);
    var data = searchParams.toString(); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search