简体   繁体   中英

pass multiple ids through ajax to get the results from database

I want to pass multiple values through ajax request like

var ids = 10 ,15,30;

is it possible to do this if not how do i pass the ids in the request?

 $.ajax({
                 type: "POST",
                 url: $('#baseurl').val()+"/ajax_requests/testing.php",
                 data:{row :ids,action:"get_values"},
                 async :false,
                 cache: false,
                 success: function(result){
                        if(result)
                        {
                            alert("success")
                        }
                    }
                });

Once i pass these values how do i go about in fetching the results in test.php

Actually, there are a lot of possibilities how to send several values. The solution depends only on how you would like to parse them in PHP. Here are some possibilities that gets on my mind:

  1. Just make a string with comma-separated values. Like:

    var ids = "10,15,30"; After that in PHP you will have to split these values by ",".

  2. You can make JSON object and pass it. In PHP you will have to parse JSON object.
  3. You can pass just like an array and treat it like an array in PHP. These are the easiest possibilities I see.

Note: there's very good function in jQuery that allows to pack easily all needed information from the page: .serialize()

You can pass this data as string

data:{row :"10,15,30",action:"get_values"},

or as array

data:{row[]:10, row[]:15, row[]:30,action:"get_values"},

depending what your php expect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM