1

i am trying to retrieve the value of array via post in php script.

var data = [];
table.rows({ selected: true }).every(function(index){
    // Get and store row ID
    data.push(this.data()[0]);  //create a 1 dimensional array
});

//send data via ajax    
$.ajax({                                      
      url: '/...../...',                  
      type: 'POST',   
      data: {userid:data},                      
      dataType: 'json',                       

In my PHP script so far I am unable to decode the array. Have tried many ways

$myArray = $_REQUEST['userid'];
foreach ($arr as $value) {
    $userid= $value;             //for now just trying to read single item 
}

I have tried print_r($myArray ); this sucessfully prints array contents to screen.

I am trying to retrieve the values for processing! Kindly point me in the right direction

5
  • what do you want to do with the values? Commented Sep 23, 2016 at 21:10
  • 1
    $myArray but foreach($arr )? Are you sure what you do here? Commented Sep 23, 2016 at 21:14
  • Possible duplicate of Get data from php array - AJAX - jQuery Commented Sep 23, 2016 at 21:16
  • Could you show us the result of a var_dump. please Commented Sep 23, 2016 at 21:16
  • Here are the results of 'var_dump' : array(1) { ["userid"]=> string(21) "assssssss,camo,castor" } @mondersky Commented Sep 24, 2016 at 5:38

5 Answers 5

1

I don't think that PHP would recognise the array that you've called "data" as being an array. Couldn't you turn the data from your table rows into values in a JavaScript object, encode it as a JSON string, then post that to your PHP script and use json_decode($_POST["userid"]) on the PHP end to convert it into a PHP array.

Sign up to request clarification or add additional context in comments.

2 Comments

Ive tried encoding the array before posting. This leads to circular reference error
Did you add the values into an object, not an array, first?
1

The object you are posting to PHP isn't in particular a jQuery object. Instead it is an JSON object or rather a JSON string. I guess you can't read that object the way you would read an regular array in PHP.

You might want to try to decode the string with json_decode(). With true as an function argument, it will return an php array as suggested in this stackoverflow answer https://stackoverflow.com/a/6964549/6710876

$phpArray = json_decode($myArray, true);

Documentation of json_decode(): http://php.net/manual/en/function.json-decode.php

Comments

0

simply use:

echo json_encode($myArray);

1 Comment

Would this statement not simply post back the array to the calling script?
0

You're foreach is looping $arr, which doesn't exist. Your array is being set to $myArray, so use that in your for.

$myArray = $_REQUEST['userid'];
foreach ($myArray as $value) {
    $userid= $value;             //for now just trying to read single item 
}

I believe you should also be able to find your values in $_POST

Comments

0

According to your var_dump :

array(1) { ["userid"]=> string(21) "assssssss,camo,castor" }

and if we assume "assssssss,camo,castor" are 3 different usernames. You should use this:

 $userids=explode(",",$myArray->userid);
    foreach($userids as $userid){
        // use $userid
   }

Comments

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.