0

I am using jQuery, JavaScript and PHP. My Ajax.php file just displays the data. File test.php has assigns the value to a JavaScript function.

My problem is I am not able to get the values that are assigned by test.php, from getList(data).

Is anything wrong with my logic? What do I do to get the values assigned by test.php to get displayed in the 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 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>";
?>

My JavaScript function has:

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

2 Answers 2

1

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 code can parse the response back into a 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>
Sign up to request clarification or add additional context in comments.

Comments

0
$.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?

2 Comments

Yes I get the Alert, But The getList Function Willnot shown the Total lenghth
COuld you please suggest example of JSON for these

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.