I've done a table of spells on phpmyadmin and i'm trying to access it via ajax request and store it entirely inside a javascript array.
Load_hero_spell.php :
<?php
include("../php/connect_database.php");
$sth = $bdd->query("SELECT * FROM spells_hero LIMIT 4");
$result = $sth->fetchAll(PDO::FETCH_UNIQUE|PDO::FETCH_ASSOC);
$php_array = array();
foreach($result as $row)
{
echo json_encode($result);
}
?>
Javacript file:
var spells_array = [];
$(document).ready(function()
{
$.ajax(
{
type: "POST",
url: "../php/load_hero_spell.php",
success: function(data)
{
spells_array = data;
alert(data);
},
error: function()
{
alert("Request failure");
}
});
alert(spells_array[1]);
});
Alert(data) display something like :
Array
(
[0] => Array
(
[nick] => Spell 1
[description] => Description 1
[icon_dir] => ../../images/icons/spells/AbolishMagic.png
[ownerID] => 1
)
[1] => Array
(
[nick] => Spell 2
[description] => description 2
[icon_dir] => ../../images/icons/spells/AbominationExplosion.png
[ownerID] => 1
)
)
but alert(spells_array); display undefined.
I'd like to be able to pass those value to an object so i can do something like
$(".Spell_icon").attr("src", Hero[1].spell[3].description);