0

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);
QuasarAG
  • 31
  • 4
  • Do you use JSON to send data back from Ajax call? – Sfili_81 Dec 03 '18 at 11:25
  • replace `print_r($php_array);` with `echo json_encode($php_array);` although you could simply `echo json_encode($result);` as that is an array already. Your `foreach` loop is actually a waste of effort – RiggsFolly Dec 03 '18 at 11:27
  • Where are you placing `alert(spells_array)`? Since it is defined as `var spells_array = [];` , it shouldn't return undefined. – escapeVelocity Dec 03 '18 at 11:28
  • 1
    @escapeVelocity But `spells_array[1]` would be. – George Dec 03 '18 at 11:28
  • 1
    @George, my bad! I just saw the index 1! – escapeVelocity Dec 03 '18 at 11:30
  • i've update the php part to reflect the add of echo json_encode @escapeVelocity `alert(spells_array)` is at the end of `$(document).ready(function() ` – QuasarAG Dec 03 '18 at 11:34
  • @QuasarAG, you need to be careful while typing. `alert(spells_array)` is different from `alert(spells_array[1])`. The former will return the data as a string and the later `undefined`. – escapeVelocity Dec 03 '18 at 11:38

2 Answers2

1

The reason our array looks like that is due to the fact that you use print_r($php_array); in your php script. This format is something javascript simple does not understand. My advice would be to use json_encode($php_array). This way, your array will be turned into JSON, which javascript will understand.

BattleOn
  • 205
  • 1
  • 3
  • 11
  • 1
    Afterwards, do not forget to parse received JSON to plain JS array on frontend side. You can use JSON.parse(data). – Jakub Hubert Dec 03 '18 at 15:02
0

add this line in php file instead of print_r($php_array);.

  $someJSON = json_encode($php_array);
  echo $someJSON;

you will get a JSON in proper format in javascript.

Negi Rox
  • 3,828
  • 1
  • 11
  • 18