1

I have a table with the first column made up of checkboxes. I want to collect the values of the checked rows with jquery and transmit these to a aphp function for processing .

So far I have:

var searchIDs = $('input:checked').map(function(){
                  return $(this).val();
                });

which returns an object containing the checked values.

I can get a list of these using :

searchIDs.get()

of the form :

1,2,3 etc

Now I need to send this to php. I tried:

                   $.ajax({
                   type: "POST",
                   url: "update/updateR",
                   contentType: 'application/json',
                   data: searchIDs.get() , 
                   contentType: 'application/json',
                   dataType: 'json',
                   success: function(){
                       alert("OK");
                   }
               });

However in my php function which consists of:

 $json = $_POST['json'];
 var_dump($json);
 exit;

I get the following error:

<p>Message:  Undefined index: json</p>

What am I doing wrong?

addendum:

I changed the ajax to

                   $.ajax({
                   type: "POST",
                   url: "update/updateR",
                   contentType: 'application/json',
                   data:{ searchIDs : searchIDs.get() },                        
                   dataType: 'json',
                   success: function(){
                       alert("OK");
                   }
               });

but I'm getting the same error and the var_dump in the php method gives NULL

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • Firstly, `var_dump($_POST)` to see what is actually being sent. If you want to specify the `json` attribute to be sent, you'll need to send an object from jQuery: `data: { json: searchIDs.get() }` – scrowler Aug 11 '14 at 21:31
  • PHP requires you to send data in a `key=value` format for it to populate $_POST/$_GET with with. You're essentially sending only `value`, so PHP has nothing to process. try `data: {foo: searchIDs.get()}` and then `$_POST['foo']` – Marc B Aug 11 '14 at 21:36

1 Answers1

5

You were using json as a key to the data being passed. In this example, searchIDs is used as the key:

      $.ajax({
           type: "POST",
           url: "update/updateR",
           data: { searchIDs: searchIDs.get() }, 
           dataType: 'json',
           success: function(){
               alert("OK");
           }
       });

Then you can access it using the key searchIDs:

$json = $_POST['searchIDs'];
var_dump($json);
exit;
mccannf
  • 16,619
  • 3
  • 51
  • 63