I have a php file where I am pulling data from a database and I want to access its contents in javascript. When I try to access the array with data[0].card_id I get "undefined".
Here is my javascript
$(document).ready(function() {
var userId = 1;
var updateUrl;
$.ajax({
type: "POST",
url: "url",
data: {userId: userId},
success: function(data) {
alert(data[0].card_id);
var suffix = ".html";
fb.start('../Animations/' + updateUrl[0].card_id + suffix);
}
});
}
Here is my php file
<?php
include('connect.php');
$user_id = $_POST['userId'];
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL";
}
$select = "SELECT card_id FROM decks WHERE id=$user_id ORDER BY order_num";
$result = mysqli_query($db, $select);
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$animation[] = array(
'card_id' => $row['card_id'],
);
}
json_encode($animation);
echo $animation;
mysqli_close($db);
?>
The array contains the following data
Array ( [0] => eating [1] => mummy
etc.. )