-1

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);
?>
Harald
  • 849
  • 3
  • 11
  • 15
  • 2
    Try to remove the `JSON.parse()`: `var availableTags = ;`. If you echo JSON like this, it will already be interpreted as JavaScript object and you cannot parse it. – Felix Kling Nov 04 '11 at 11:05
  • 2
    Is your HTML file actually a PHP file (saved as .php)? – apnerve Nov 04 '11 at 11:05
  • 1
    But is it served through PHP? It has to be... as I said in your previous question, you have to have a look at the resulting HTML, the one the browser sees. If it still contains the PHP directives, then the file is not parsed by PHP. You cannot execute PHP code with JavaScript (if that is what you want to do). – Felix Kling Nov 04 '11 at 11:07
  • What do you mean in with PHP directives? You mean I should look at my source code at "runtime"? What should I look for? – Harald Nov 04 '11 at 11:10
  • 2
    Open the site in your browser and inspect the source. If you see `` in that line instead of `{"Data":...`, then your file is not processed by PHP. It has to be processed by PHP if you want to make this work or use other ways to retrieve the data, like an Ajax call. – Felix Kling Nov 04 '11 at 11:12
  • 1
    possible duplicate of [Javascript function not called](http://stackoverflow.com/questions/8007600/javascript-function-not-called) – Quentin Nov 04 '11 at 11:15
  • Ok, thanks for all that. Now I know, that i have JSON data in it. The problem is my for-loop: for(var i=0;iavailableTags["+i+"] is =>"+availableTags[i]+"
    "); } The browser doesn´t access it!
    – Harald Nov 04 '11 at 11:38
  • It is hard to tell **because you *still* haven't shown us the output of the PHP**, but it looks like `availableTags` is not an array. – Quentin Nov 04 '11 at 12:08
  • The output of the PHP is exactly the JSON, what I posted above! It seems that availableTags can´t save the data. Even if i write var availableTags = new Array(); availableTags[i] = ; How can I manage this? – Harald Nov 04 '11 at 12:14
  • Then it isn't an array, and if you wrap it in an array then it is an array with one entry in it (assuming `i` is an integer), and you'll still need to drill into it. – Quentin Nov 04 '11 at 12:34
  • And you shouldn't be running a JavaScript object through JSON.parse – Quentin Nov 04 '11 at 12:35
  • Drill into it? Sorry but how do you mean that? – Harald Nov 04 '11 at 12:36
  • I suggest you take a step back and learn the basics of JS. You don't appear to understand JS data structures at all, and they are fundamental to this. The [W3C have a good guide](http://www.w3.org/wiki/Web_Standards_Curriculum#JavaScript_core_skills) and [The Good Parts](http://shop.oreilly.com/product/9780596517748.do) is worth reading. – Quentin Nov 04 '11 at 12:39
  • Sorry but that doesn´t help me very much. I know that the way I tried doesn´t work and I´m searching for another way to split the JSON string and insert each in the array... Do you know a way? – Harald Nov 04 '11 at 13:16

1 Answers1

1

You don't need to do a JSON.parse you can simply do

var availableTags = <?php include("/php/search_new.php"); ?>;

And name your php file as somename.php and make sure it is on some local or hosted server.

and you can see you data as,

alert(a['Data']["Recipes"]["Recipe_5"]["ID"]);
Nasaralla
  • 1,839
  • 13
  • 11