简体   繁体   中英

Getting array values in php from jQuery post

I am trying to send an array from jQuery post to PHP. But I am not getting any values with the below code. Could anyone help ?

jQuery

      $("body").on("click", ".js-form",function(event){

         var arr = [];
         i = 0;

          $('.addcolor').each(function() {

              if( $(this).text()=="done"){

              arr[i++]= $(this).data('request-id');
              }  

          });

          alert(arr);
          $.post("../ajax/save_Request.php", {requestids:arr, action:'save_request' })   

      });

alert(arr)-> prints 11,24,35 (eg)

But I am not getting any values in the following PHP variable.

PHP

$ids = ( isset($_POST['requestids']) ) ? $_POST['requestids'] : 0;

Try with this 'choices[]'

$.post( "test.php", { 'choices[]': [ "Jon", "Susan" ] } );

See more in : jQuery.post and search the key "Pass arrays of data to the server". I think that you missed [] . Try it and return me the result.

Try converting the array to a JSON string first, using

var json = JSON.stringify(arr);

Now that it's a JSON string, you can simply pass it through a hidden field. Then, once you get the string back from the PHP page, you can turn it back into an array using

$array = json_decode($arr, true);

where $arr is the JSON string.

I had a similar problem with trying to pass an array from JQuery to another PHP page and this worked for me.

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