I have php page with a drop down selector, i use jquery, ajax and mysql to produce a table when the selector is changed and drop into a div called .mytable. Some of the data in the table are links to php scripts which effect the data in the table, so I want to refresh the table without a reload.
I know I need to do this by attaching an event listener to the links in the table, but I cant seem to get the right selector to attach the listener to?
Any help would be appreciated
The page the user sees, called user.php is:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="css/table_style.css" rel="stylesheet" type="text/css">
</head>
<body>
<form name="form1" method="post" action="">
<label for="select_data"></label>
<select name="select_data" id="select_data">
<option value=1>"Apples"</option>
<option value=2>"Pears"</option>
<option value=3>"Bananas"</option>
</select>
</form>
<div id="mytable">This is where the table goes</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="get_table.js"></script>
</body>
</html>
The jquery event handler is:
$(document).ready(function (){
//listen for select list to change, pass fruit into create_table.php, pass data into mytable div
$('#select_data').change(function() {
$.post('create_table.php',{fruit: fruit}, function(data) {
$('div#mytable').html(data);});
});
$('.fruit_link').change(function() {
$.post('create_table.php',{fruit: fruit}, function(data) {
$('div#mytable').html(data);});
});
});
The script that the event handler calls which creates the table and returns the html code is:
<?php
require_once('Connections/dbc.php');
$fruit=$_POST['fruit'];
$sql="SELECT * FROM q_fruit WHERE FruitID =".$fruit;
$rec1=mysqli_query($dbc,$sql);
echo '<table class="table" align="center">';
echo '<th class="th" width="80px">Fruit ID</th>';
echo '<th class="th" width="150px">Fruit Name</th>';
echo '<th class="th" width="40px">Fruit Action 1</th>';
echo '<th class="th" width="150px">Fruit Action 2</th>';
while ($row=mysqli_fetch_array($rec1))
{
echo '<tr class="tr">';
echo '<td class="td">'.$row['FruitID']. '</td>';
echo '<td class="td">'.$row['FruitName']. '</td>';
echo '<td class="td"><a class="fruit_link" href="fruitaction1.php">'.$row['FruitID']. '</a></td>';
echo '<td class="td"><a class="fruit_link" href="fruitaction2.php">'.$row['FruitID']. '</a></td>';
echo '</tr>';
}
echo '</table>';
mysqli_free_result($rec1);
mysqli_close($dbc);
?>
So, the select event handler is working to reproduce the table every time a value is changed, but the link handler is not, I'm presuming its because the links are in the html thats returned and not already present in the user file which the java is listening to. Is there a way round this?