0

I have PHP associative array and I use JQuery AJAX to get the result array but my problem is when that result is pass to jquery and use looping to extract each Sequence,Percent and Date then that extracted data will store to new Jquery Array for data manipulation purposes. Please see my sample code so far.

sample code PHP ARRAY:

$Sequence=array(
    array("Seq1","20%"),
    array("Seq2","40%"),
    array("Seq3","60%"),
    array("Seq4","80%"),
    array("Seq5","100%")
);

****For loop here****

$ResultArray[$arrayIndex]=array(
    'Sequence' => $Sequence[$arrayIndex][0],
    'Percent' => $Sequence[$arrayIndex][1],
    'Date' => $row['exactDate']
);


echo json_encode($ResultArray); // then pass result array to jquery

JQUERY :

$(document).ready(function(){

    var ListOfSequence = []
    var ListOfPercentage = [];
    var ListOfDates = [];

    $("#button").click(function(){

        var _WOID = $('#txtWOID').val();

        $.ajax({
            url:'getStatus.php',
            type:'POST',
            data:{id:_WOID},
            dataType:'json',
            success:function(output){
                //here is where the problem begin
                for (var key in output) {
                    if (output.hasOwnProperty(key)) {
                        //here where extracted data will store to designated array
                        ListOfSequence.push(key);//<---store extracted Sequence
                        ListOfPercentage.push(key);//<---store percentage
                        ListOfDates.push(output[key]);//<---store dates                
                    }
                }

                ListOfPercentage.reverse();

                console.log(ListOfPercentage);
                console.log(ListOfDates);
                console.log(ListofSequence);

            }

        });

    });

});

and here's the console.log:

enter image description here

Thank you in advance

2 Answers 2

1

Since you are already using jQuery you could use $.each() :

$(document).ready(function(){

  var ListOfSequence = []
  var ListOfPercentage = [];
  var ListOfDates = [];

  $("#button").click(function(){

    var _WOID = $('#txtWOID').val();

    $.ajax({
      url:'getStatus.php',
      type:'POST',
      data:{id:_WOID},
      dataType:'json',
      success:function(json){
         $.each(json, function(index, object){
             ListOfSequence.push(object.Sequence);
             ListOfPercentage.push(object.Percent);
             ListOfDates.push(object.Date);
         });

      }

    });

  });

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

Comments

1

You should set the json response header before sending the content to the browser like so:

header('Content-type: application/json'); die(json_encode($ResultArray);)

9 Comments

ok sir... if you don't mind, what will happen if I don't set json response header?
@mocanuga never ever tell anyone to use eval() its really bad practice unless you know what you are doing (and OP clearly is not), a better aswer would have been var json = JSON.parse(output);
@mocanuga since OP is using dataType : json it does not need to be eval'ed or w/e, jquery will take care of that
You're right. It's just that I always set the header just to be safe. Also, I didn't recommend anyone to use eval. I just told him that, without the json header, he might need to. JSON.parse might not be available in older browsers.
and that's why I didn't down rate your answer itself because imo that is an improvement and good practice, just the comments aren't (and sorry I explode every time I see someone say eval()), and you are right about older browsers but that must be something like IE6/7 : stackoverflow.com/questions/4908875/… if MS stops supporting IE6/7 why should we still support them ?
|

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.