views:

389

answers:

2

Dear All I am using JQuery & javascript PHP, My Ajax.php just displays the data ,test.php has assigns the value to javascript function. My Problem is I not able to get the values that assigned by test.php, from getList(data). is Any wrong in my logic ?.What to do to get the values assigned by test.php will be displayed in getList() function?

$.ajax({
      type:'POST',
      url: 'ajax.php',
      data:'id='+id  ,
      success: function(data){
                               $("#response").html(data);
                                       if(flag!=0){
                                                 flag=0;
                                                  $.get("test.php", function(data){
                                                    alert("Data Loaded: " + data);
                                                    getList(data);
                                                 });


                                        }                                                                                                              

      }//success
          }); //ajax

And test.php has following

          <?php

          print "<script language='javascript'>";
          print " temp[temp.length]=new Array('STA-VES','East',4500);"; 
          print " temp[temp.length]=new Array('STA-CRF','West',5400);"; 
          print "</script>";  

          ?>

My Javascript function has

          var temp=new Array();
           getList(){
             alert(temp.length);
             for(var i=0; i<temp.length; i++){
      var val = temp[i];
              }

          }

Any Idea?

A: 
$.get("test.php", function(data){
 alert("Data Loaded: " + data);
 getList(data);
});

Do you get an alert box with the output of the following?

      <?php

      print "<script language='javascript'>";
      print " temp[temp.length]=new Array('STA-VES','East',4500);"; 
      print " temp[temp.length]=new Array('STA-CRF','West',5400);"; 
      print "</script>";  

      ?>

Note that you should be getting HTML (since that's what you're generating in your PHP file. This is not the way to pass data from PHP to JavaScript, unless you're loading up this PHP in an iframe to directly manipulate the parent window context.

You should change test.php to return JSON. What is ajax.PHP returning anyway?

Ates Goral
Yes I get the Alert, But The getList Function Willnot shown the Total lenghth
venkatachalam
Yes My ajax.php returns fine
venkatachalam
COuld you please suggest example of JSON for these
venkatachalam
+1  A: 

The below code will output the values you require into json that when reconstructed will resemble your array:

<?php
 echo json_encode(array(
  array(
   'STA-VES',
   'East',
   4500
  ),
  array(
   'STA-CRF',
   'West',
   5400
  )
 ));
?>

then your jquery can parse the response back into an javascript object.

<?php
 json_encode(array(
  array(
   'STA-VES',
   'East',
   4500
  ),
  array(
   'STA-CRF',
   'West',
   5400
  )
 ));
?>

<script type="text/javascript">
 $.getJSON("test.php", function(json){
  // access object
  for(var i = 0; i < json.length; i++) {
   alert(json[i][0]);
   alert(json[i][1]);
   alert(json[i][2]);
  }
 });
</script>

hth

Gavin

Gavin