Note that there are some explanatory texts on larger screens.

plurals
  1. POArray of objects suddenly empty
    primarykey
    data
    text
    <p>I am getting a json string from my Server via a php file, containing 25 meals. The php is started by a javascript function. I then want the json to be converted into an array of objects.</p> <p>Responce stream has the right values. alert() shows the right values on every single step. Once the full stream is done the array is suddenly empty again. THIS is my issue and i am lost on this one, not even sure what to google.</p> <p>I'll just paste the code from my php and javascript. Please note the 3 Alert() at the end of the javascript code, they show what happens.</p> <p>Here is the php</p> <pre><code>&lt;?php $mysqlHOST = "localhost"; $mysqlUSER = "xxx"; $mysqlPW = "xxx"; $mysqlDB = "xxx"; $mysqlERROR = "Could not connect to database!"; $mysqlERRORDB = "Could not connect to database - DB-Error!"; @mysql_connect($mysqlHOST,$mysqlUSER,$mysqlPW) or die($mysqlERROR); @mysql_select_db($mysqlDB) or die($mysqlERRORDB); @mysql_query('SET CHARACTER SET utf8'); $res = mysql_query("SELECT * FROM Rezepte"); while($Row = mysql_fetch_array($res)) { $jsondata[]= array('f1'=&gt;$Row["id"], 'f2'=&gt;$Row["data"], 'f3'=&gt;$Row["art"],'f4'=&gt;$Row["name"],'f5'=&gt;$Row["zutaten"],'f6'=&gt;$Row["info"],'f7'=&gt;$Row["bild"]); }; echo(json_encode($jsondata)); ?&gt; </code></pre> <p>Here is the Javascript.</p> <pre><code>//Variablen varRezepte = []; //Liste mit den Rezeptobjecten //Konstruktor für die Rezept - Objekte function objRezept(varid, data, art, name, zutaten, info, bild) { this.varid = varid; this.data = data; this.art = art; this.name = name; this.zutaten = zutaten; this.info = info; this.bild = bild; } //wenn Page gepaden ist soll das folgende passieren $(function() { //Datenbank bemühen um die Rezepte herzubekommen $.ajax({ type: "POST", url: "planerDB.php", dataType: "json", success: function (data) { for(var i = 0;i &lt; data.length;i++) { var item = data[i]; varRezepte.push(new objRezept(item.f1, item.f2, item.f3, item.f4, item.f5, item.f6, item.f7)); alert(varRezepte[i].name); //correct output } } }); alert(varRezepte[0].name); //undefined alert(varRezepte.length); //zero }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload