I´m really frustrated about a problem with my website. From a php File, I get this list of JSON:
{"Data":{"Recipes":{"Recipe_5":{"ID":"5","TITLE":"Spaghetti Bolognese"},"Recipe_7":{"ID":"7","TITLE":"Wurstel"},"Recipe_9":{"ID":"9","TITLE":"Schnitzel"},"Recipe_10":{"ID":"10","TITLE":null},"Recipe_19":{"ID":"19","TITLE":null},"Recipe_20":{"ID":"20","TITLE":"Hundefutter"},"Recipe_26":{"ID":"26","TITLE":"Apfelstrudel"},"Recipe_37":{"ID":"37","TITLE":null},"Recipe_38":{"ID":"38","TITLE":"AENDERUNG"},"Recipe_39":{"ID":"39","TITLE":null},"Recipe_40":{"ID":"40","TITLE":"Schnitzel"},"Recipe_42":{"ID":"42","TITLE":"Release-Test"},"Recipe_43":{"ID":"43","TITLE":"Wurstel2"}}},"Message":null,"Code":200}
In my html file, I´ve got a JS function that parses this JSON data and save it to a array.
<script type="text/javascript">
function test() {
//var availableTags = new Array(400);
//availableTags[0] = "Test";
alert("misstake");
var availableTags = JSON.parse(<?php include("/php/search_new.php"); ?>);
alert("misstake");
//var availableTags = JSON.parse(<?php include("/php/getAllRecipes.php"); ?>);
alert("misstake");
for(var i=0;i<availableTags.length;i++){
alert("<b>availableTags["+i+"] is </b>=>"+availableTags[i]+"<br>");
}
alert("Hallo");
}
</script>
I´m sure, the function is called because I tried it just with a alert in it. Here´s my HTML:
<body>
<form action="search.html" onsubmit="test()">
<input type="text" class="searchinput" style="margin-left: 850px; margin-top: 0px; width:170px; background: #fff url(images/search_icon.png) no-repeat 100%;" placeholder="Suchen..."></input>
<input type="submit" value="" width: 5px></input>
</form>
</body>
So there must be a mistake at
var availableTags = JSON.parse(<?php include("/php/search_new.php"); ?>);
What is the problem? How can I figure out that?
If you need the php:
<?php
include 'db_connect.php';
session_start();
if (isset($_SESSION['last_activity']) && (time() - $_SESSION['last_activity'] > 1200)) {
session_destroy();
session_unset();
}
else
{
$_SESSION['last_activity'] = time();
}
$arr = array('Data' => null,'Message' => null,'Code' => null);
$sql = "SELECT * FROM RECIPES";
$result = mysql_query($sql,$db) or exit("QUERY FAILED!");
while($row = mysql_fetch_array($result))
{
$arr['Data']['Recipes']['Recipe_'.$row['recipes_id']]['ID'] = $row['recipes_id'];
$arr['Data']['Recipes']['Recipe_'.$row['recipes_id']]['TITLE'] = $row['title'];
}
if($arr['Data'] == null)
{
$arr['Message']= "nothing found";
$arr['Code'] = 404;
}
else
{
$arr['Code'] = 200;
}
mysql_close($db);
echo json_encode($arr);
?>
"); } The browser doesn´t access it!