0

i am using serialize()

Some Event trigger(assume click)

       var querydata= a=1&b=2&c=3  //jquery printing 
     $.ajax({
        url: "script",
        data: querydata,
        method: "POST",
        dataType: "text",
        success: function(data) {
          $("#counts").html(data);
            }

  });

in php do i just use the regular post method

a=htmlspecialchars($_POST["a"]); b=htmlspecialchars($_POST["b"]); and so on

or do i need to use jquery to get the string to variables and then send to data as a object array

if jquery is also an option could you tell me how i would do that im fairly new to jquery and i really want to learn it

3
  • 2
    var_dump($_POST) would show you what php's receiving. and note that jquery will happily accept an array/object for data and do the necessary querystring transforms for you. you DON'T have to manually build the string yourself. data: {"a":1,"b":2,....} works just fine. Commented Aug 3, 2016 at 16:10
  • neat but using ajax how do i see my script page ...im guessing i have to send var_dump as a response back and print it somewhere? @Marc B Commented Aug 3, 2016 at 16:13
  • any output from php would show up in the success handler's data parameter, which you're inserting into your page already anyways. Commented Aug 3, 2016 at 16:19

1 Answer 1

1

Why bother creating a functionality that your browser+PHP provide already?? In your case, if you really have to send a raw string:

var querydata = 'a=1&b=2&c=3';
$.ajax({
    url: "script",
    data: querydata,
    method: "POST",
    dataType: "application/x-www-form-urlencoded",
    success: function(data) {
        $("#counts").html(data);
    }
});

You may also want to simplify:

var querydata = {a: 1, b: 2, c: 3};
$.post('url', querydata, function(data){
    $("#counts").html(data);
});
Sign up to request clarification or add additional context in comments.

6 Comments

thing is the querydata variable will keep changing there are 3(x150 pages) loaded everytimes a succesfull submit happens
and each time query data will change so i dont think using a built array would work form me casue values assigned to a b c will changed based on what the user selects
Of course native arrays will work. Just capture the user input value: { a: $(':input[name="inputName"]').val() }
the pages are all paginated using easyPagination lib so from 1 field set iv captured the values using serialize() thats why im getting a querystiring
|

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.