I want to the arr1 value is changed to arr2 value(test.php) when click event is triggered. However, there is error on the following code.
Is there anyone can help me out? Thanks!
$(function(){
var arr1 = ["apple","banana","orange"];
$("input").click(function(){
$.ajax({
type:"post",
url:"test.php",
cache:false,
success: function(data) {
arr1 = data;
}
});
});
});
——-Test.php—————————————-
<?php
echo $arr2 =array("Saab","Volvo","BMW","Toyota");
?>
You need to use JSON to transfer complex objects to javascript. In php, you’d encode your array using json_encode, like this:
Then you have to tell jquery to talk in json using
dataType: 'json', like this: