-1

I have this jQuery code that adds to an array inside an $each loop:

new_edit = {};
new_edit['file_id'] = $file_id;
new_edit['file_realname'] = $new_file_realname;
new_edit['file_folder'] = $new_file_folder;
new_file_edits.push(JSON.stringify(new_edit));

jQuery new_file_edits array output

{"file_id":"1857","file_realname":"dddd[1].mp4","file_folder":"/"}, 
{"file_id":"1856","file_realname":"aaaa[1].jpg","file_folder":"/"},
{"file_id":"1855","file_realname":"ssss[1].jpg","file_folder":"/"}

and im trying to post it to call.php like this:

$.ajax({
    type: "POST",
    url: "/call.php",
    data: "edit_files="+ arrayVarHere,
    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        success: function(msg){
            alert(+msg);
        }
    });

In the PHP file call.php i have this code:

if(isset($_POST['edit_files'])){
    $edit_array = $_POST['edit_files'];
    $file_array = array();
    foreach($edit_array as $key => $value){
        $file_array[$key] = $value;
    }
print_r($file_array);
    die();
}

but i get this error and i cant figure out how to fix it, been googling it for a while now...

error:

Warning: Invalid argument supplied for foreach() in /home2/dddd/public_html/call.php on line 237

Line 237: foreach($edit_array as $key => $value){

Any help is much appreciated!

-Morten

EDIT 1: I changed $edit_array = $_POST['edit_files']; to $edit_array = array($_POST['edit_files']);

And now it outputs:

{"file_id":"1857","file_realname":"dddd[1].mp4","file_folder":"/"}, 
{"file_id":"1856","file_realname":"aaaa[1].jpg","file_folder":"/"},
{"file_id":"1855","file_realname":"ssss[1].jpg","file_folder":"/"}

How do i go from here with the foreach($edit_array as $key => $value){ part?

Edit 2: i build my arrayVarHere like this:

$.each( $selected_files_array, function( index, value ){
//i get $file_id, $new_file_realname and $new_file_folder with some code here
        new_edit = {};
        new_edit['file_id'] = $file_id;
        new_edit['file_realname'] = $new_file_realname;
        new_edit['file_folder'] = $new_file_folder;
        arrayVarHere.push(JSON.stringify(new_edit));
    });
10
  • Possible duplicate of PHP Warning: Invalid argument supplied for foreach() Commented Jun 8, 2016 at 5:05
  • Is $edit_array an array? Commented Jun 8, 2016 at 5:05
  • Check this too: stackoverflow.com/q/2630013/5447994 Commented Jun 8, 2016 at 5:07
  • data: {edit_files : arrayVarHere}, Commented Jun 8, 2016 at 5:07
  • I updated the post with an edit, any ideas? Commented Jun 8, 2016 at 5:14

5 Answers 5

0

change your code like this:

data: "edit_files="+ arrayVarHere,

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

Comments

0

call json data by json_decode in call.php

if(isset($_POST['edit_files'])){
$edit_array = json_decode($_POST['edit_files'],true); //return array 
$file_array = array();
foreach($edit_array as $key => $value){
    $file_array[$key] = $value;
}
print_r($file_array);
die();
}

in your ajax alert should be

 success: function(msg){
            alert(msg); //remove plus sign ,this can output `NaN`
        }

2 Comments

How can i output file_realname for each file_id in the array?
what will output $key and $value in foreach
0

check edit_array is array print_r($edit_array);

check contentType is used form data then correct otherwise used json type

if form data then check data: edit_files[]: arrayVarHere,

Comments

0

It returns this array

Array ( [0] => [{"file_id":"1857","file_realname":"aq53pop_460sv[1].mp4","file_folder":"/"},{"file_id":"1859","file_realname":"aKqbD8Q_460sv[1].mp4","file_folder":"/"},{"file_id":"1856","file_realname":"aDopymK_700b[1].jpg","file_folder":"/"}] ) 

how can i loop the array and access file_id value etc?

Comments

0

The way you are passing Data to PHP may not be that ideal. Here is what might get you started:

PHP: call.php

    <?php

        $data0      = isset($_POST['data_0']) ? json_decode($_POST['data_0'], true) : null;
        $data1      = isset($_POST['data_1']) ? json_decode($_POST['data_1'], true) : null;
        $data2      = isset($_POST['data_2']) ? json_decode($_POST['data_2'], true) : null;

        $arrData    = array(
            'data0' => $data0,
            'data1' => $data1,
            'data2' => $data2,
        );

        foreach($arrData as $dataKey=>$dataObject){
            $tempFileID     = $dataObject->file_id;
            $tempFileName   = $dataObject->file_realname;
            $tempFileFolder = $dataObject->file_folder;

            // DO SOMETHING WITH THESE DATA AND THEN BUILD UP A RESPONSE FOR AJAX + SEND IT...

            // UNCOMMENT TO SEE THE DATA IN YOUR NETWORKS CONSOLE BUT 
            // IF YOU DO THIS (SINCE YOU EXPECT JSON RESPONSE) YOUR AJAX WILL RESPOND WITH AN ERROR...
            // var_dump($dataObject);
        }

        // RESPOND WITH THE SAME DATA YOU RECEIVED FROM AJAX: JUST FOR TESTING PURPOSES... 
        die( json_encode($arrData));

    ?>

JAVASCRIPT: BUILDING THE DATA FOR PHP

    <script type="text/javascript">
        var dataForPHP              = {};
        var iCount                  = 0;

        $.each( $selected_files_array, function( index, value ){
            var keyName                 = "data_" + iCount;
            new_edit                    = {};
            new_edit['file_id']         = $file_id;
            new_edit['file_realname']   = $new_file_realname;
            new_edit['file_folder']     = $new_file_folder;
            dataForPHP[keyName]         = new_edit;
            iCount++;
        });
    </script>

JAVASCRIPT: AJAX REQUEST

    <script type="text/javascript">
        $.ajax({
            type        : "POST",
            url         : "/call.php",
            datatype    : "JSON",
            data        : dataForPHP,
            contentType: "application/x-www-form-urlencoded;charset=UTF-8",

            success: function(msg){
                if(msg){
                    alert("Ajax Succeeded");
                    console.log(msg);
                }
            }
        });
    </script>

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.