0

Originally I had the following to produce a menu using PHP:

<ul>
   <?php 
         $items = mysqli_query($con, "SELECT DISTINCT item FROM table");
         while ($row = mysqli_fetch_array($items)) { ?>
             <li><a href='#' onclick='myFunction(<?php echo $row['item'] ?>)'><?php echo $row['item'] ?></a></li><? php 
         } ?>
</ul>

I had this code inside an index.php page surrounded by html. The is to produce a menu from items in a database. In this form it worked fine and called 'myFunction(string)' which is in a seperate global.js file.

function myFunction(string) {
$('#container').load('php/getContent.php', {string: string});
}

getContent.php is similar to the php above. except it uses a where clause in the query which it extracts from the string.


So my problem arose when I went to clean up the code which produces the menu. I added the following function to global.js

$(document).ready(function() {
    $('#menu').load('php/getMenu.php');
});

getMenu.php looks like this:

$items = mysqli_query($con, "SELECT DISTINCT item FROM table");

echo "<ul class='tab'>";
while ($row = mysqli_fetch_array($items) {
    echo "<li><a href='#' onclick='myFunction(".$row['item'].")'>".$row['item']."</a></li>";
}
echo "</ul>"

This works correctly in producing the menu items as before except the onclick function no longer works. Placing an anonymous inline onclick function did work however so I figured it may be the $(document.ready() inside the global.js file causing issues. I moved the $(document).ready() back into index.php in a script tag to check, still not working.

I originally had the script tags as the last elements in the body so I moved them up into the header to check if that changed anything but that also didn't work.

the header tag looks as follows:

<script src='scripts/jquery-3.0.0.js'></script>
<script src='scripts/global.js'></script>
<script>
     $(document).ready(function() {
         $('#menu').load('php/getMenu.php');
     });
</script>

all my php files are in a subfolder called php and all my js files in a subfolder called scripts.


So firslty, does it matter if I have the $(document).ready() in a js file with other functions, does that mess with the other functions?

Secondly any idea on why the function is no longer being called?

Thanks in Advance.

  • change ´myFunction(string)´ to ´function myFunction(string)´. Do you really have a element named ´menu´? Or is it an id name? –  Jun 20 '16 at 22:51
  • the function myFunction() was just a mistake in the text, it's like that in code. 'menu' is the id of a
    , also just a typing mistake transcribing, not in the actual code :)
    –  Jun 20 '16 at 22:57
  • Remember, PHP runs only on the server, never in the browser. Never at the same time as JavaScript. The only way PHP and JavaScript can work together is through the HTTP pipe in the form of links, forms and XHR. jQuery `load` is an XHR call, so you need to use a URL, not a file path, to get through HTTP back to the server and PHP. This answer may help you: [URL not file path](http://stackoverflow.com/a/12524381/2743458) – bloodyKnuckles Jun 20 '16 at 23:04
  • Shouldn't matter if you have `$(document).ready()` in another file. As for why it's not being called anymore, have you checked your dev tools for an error message? I wanted to say that you're printing out variables instead of strings for the parameters in your click callback, perhaps something like this: `echo "
  • ".$row['item']."
  • ";` (notice the backslash double quote before and after the `$row['item']` parameter to indicate it's a string not a variable); but it looks like your first function does that too. –  Jun 20 '16 at 23:08
  • The string `` should be quoted because it is a string in JS, I'd think. – chris85 Jun 20 '16 at 23:08
  • Have you tried using developer tools from Chrome or Firefox, to check if the console is displaying any errors or warnings? Like undefined function or something like that. In Chrome you can bring it up with the shortcut CTRL + SHIFT + i – Tiago Jun 20 '16 at 23:09