1

I'm working with JavaScript and PHP using arrays, the first step is create the array, here

var ListaA=[];
var i = 0, len = options.length;
while (i < len){
            var tmp = {
                        'proyecto':'test',
                        'pendientes1':0,
                        'pendientes2':0,
                        'terminadas1':0,
                        'terminadas2':0,
                        'solucion':0
                };
                ListaA.push(tmp);
                i++;
            }

Then i send it to my PHP file like this

var laLista = JSON.stringify(ListaA);

$.get("php/operation.php?test="+ {'test' : laLista }, function( data ){
                var tmp = {
                        'proyecto':""+value['proyecto']+"",
                        'pendientes1':""+value['pendientes1']+"",
                        'pendientes2':""+value['pendientes2']+"",
                        'terminadas1':""+value['terminadas1']+"",
                        'terminadas2':""+value['terminadas2']+"",
                        'solucion':""+value['solucion']+""
                    };
                    ListaA.push(tmp);
            });

As you can see above i have ready the code to get the data which represents the array sent by the PHP file, so i got covered that part, my issue here is in my PHP file, here.

$arrayWork = json_decode($_POST['test']);

Then i want to loop, this time, just for testing i'm just taking one of the values and incresing it to watch the result, like this

foreach($arrayWork as $value){
     $value['pendientes1']++; // this is not working for me
}

I got the following: "invalid argument supplied in foreach". So, what's wrong with my code? and which is the properly way to loop it and return it to my JavaScript?

I hope you can help me out with this issue. Thank you for your time and attention, good night.

1
  • 1
    In php code $arrayWork or $_POST['test'] it should be empty because in javascript you are using get request and in php you try to get data from a post. Commented Dec 22, 2016 at 5:22

2 Answers 2

2

Using this code

$arrayWork = json_decode($_POST['test']);

your json isn't really converted into an associated array, look at below

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )

assoc When TRUE, returned objects will be converted into associative arrays.

To convert json object into an array just add true to the second parameter to it

$arrayWork = json_decode($_POST['test'], true);**strong text**

To increment an index value in an array

foreach($arrayWork $key => as $value){
    $arrayWork['pendientes1']++;
}

Edited.

also since you are using $_POST method change your ajax from $.get to $.post

$.post("php/operation.php?test="+ {'test' : laLista }, function( data ){
      var result = JSON.parse(data); // parse json string into json object
       ...
    });
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer, i get it, but how about my loop sentence in my PHP file? or also i have to modify it to make it work properly?
Awesome, but you don't get me, i mean, the way that i'm looping in PHP foreach($arrayWork as $value){...} is it correct? or also i have to modify it?
your goal in that line is to increment $value['pendientes1']++ alone
Yeah, only that just for test if the foreach sentece is working correctly.
@Gutierrez check my updated answer, I added the increment
|
2

If you want to read $_POST, you have to make a POST request:

$.ajax({
  url:'php/operation.php',
  type:"POST",
  data: { test: ListaA },
  contentType:"application/json; charset=utf-8",
  dataType:"json",
  success: function(){
    ...
  }
})

You can't use $.post, because you have to set the contentType to JSON.

Important: you don't need to run JSON.stringifyyourself, jQuery will take care of it for you - so pass the original ListaA array.

1 Comment

Thank you for your answer and how about my foreach sentence? is it correct? or also i have to modify it? foreach($arrayWork as $value){...}

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.