0

i'm new in PHP and JAVASCRIPT.. i need to do a little script who takes the data from data.php with $.getJSON("data.php", function(json).. and push it into 2 arrays in the HTML file, to reuse them.

data.php produce this:

[[47,48,48,48,50,51,48,46,47,45,48,47],[25,23,22,21,19,21,24,25,27,29,31,28]]

at now i do this, but it doesn't run and i don't know how to do.

<!DOCTYPE html>
<html>
<head>
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $.getJSON("data.php", function(json) {
            $row1[] = json[0];
            $row2[]= json[1];           

        }); 
    });

</script>
</head>
<body>

<div></div>

</body>
</html>

thanks to all guys ;)

1
  • I think you are confusing yourself with PHP and JS notations. $array[] means to append data to an array in PHP, not JavaScript. Whereas object[property] is to access an object's property value. However, $row1[] in your code is an invalid syntax as you have not defined any object $row1 nor provided any property within []. Commented Apr 25, 2017 at 0:03

1 Answer 1

3

Your assignment syntax is wrong. You can't put [] at the end of a variable in Javascript.

$(document).ready(function() {
    $.getJSON("data.php", function(json) {
        var row1 = json[0];
        var row2 = json[1];
        // put code that uses row1 and row2 here
    }); 
});
Sign up to request clarification or add additional context in comments.

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.