In a database, a number of items are stored. When the user loads the page, the items are listed as rows of a table. To enable that the user can remove elements (rows), each row also provides a "Remove item" button with an onclick event attached to it. Here's the PHP part that generates the table.
for ($i=0; $i<$num_books; $i++)
{
$book = mysql_fetch_row($classics);
$shop .= "<tr>\n";
$shop .= "<td>".$book[0]."</td>\n";
$shop .= "<td><input type='button' onclick='remove_item()' value='Remove' /></td>\n";
$shop .= "</tr>\n";
}
The remove_item() function is defined externally in JQuery (see below).
However, now clicking the buttons results in an error: ReferenceError: Can't find variable: remove_item. I believe it's because the remove_item()functions which are returned by PHP are not known to the DOM.
How can this be corrected?
The complete markup is here
<html>
<head>
<script type='text/javascript'
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js">
</script>
<script type="text/javascript"
src="../behaviour/interactions.js">
</script>
</head>
<body>
<h1>Publications database</h1>
<div id="items"></div>
</body>
</html>
The complete interactions.js script is here
$('document').ready(function()
{
// Path to MySQL configuration.
var server = '../server/'
var data = '../data/'
function mysql_connection_setup()
{
$.get(data + 'mysql_connection_setup.php');
}
function populate()
{
$.get(server + 'shop.php', cb);
}
function remove_item()
{
console.log("Remove item.");
}
// Generic AJAX callback.
function cb(response)
{
$('#items').html(response);
}
mysql_connection_setup();
populate();
}); // End ready
onclick='javascript: remove_item()'. If theremove_item()function is defined in a page that is included on the current page, then this should work.remove_item()is defined in the Javascript that loads with the page.Inspect elementwith Chrome, click onConsolethen type inremove_item()it should be triggered successfully. If it is not, then it's not properly included on the page.remove_item()in the console and it's not recognized (Can't find variable: remove_item).$(document).ready()function. You should place the function outside of this so it is globally accessible to the page.