I have the following code:
PHP
<?php include("db.php"); ?>
<html>
<head>
<title>Title</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
</head>
<body>
<div align="center">
<table cellpadding="0" cellspacing="0" width="500px">
<?php $sql = "SELECT * FROM items ORDER BY ID DESC";
$items= mysql_query($sql);
while ($item= mysql_fetch_array($items)){
$id = $item[ID]; ?>
<script type="text/javascript">
$(function() {
$(".<?= $id ?>").click(function() {
var element = $(this);
var boxval = $("#<?= $id ?>").val();
var dataString = 'not='+ boxval;
$("#flash").show();
$("#flash").fadeIn(200).html('');
$.ajax({
type: "POST",
url: "update_data.php",
data: dataString,
cache: false,
success: function(html){
$("ol#update").prepend(html);
$("ol#update li:first").slideDown("slow"); document.getElementById('content').value='';$("#flash").hide();
}
});
return false;
});
});
</script>
<tr style="border: solid 4px red;">
<td>
<div class="<?= $id ?>">
<button type="submit" id="<?= $id ?>" name="not" value="<?= $id ?>">BUTTON <?= $id ?></button>
</div>
</td>
</tr>
<?php } ?>
</table>
<div id="flash" align="left" ></div>
<ol id="update" class="timeline"></ol>
<div id="old_updates"></div>
</div>
</body>
</html>
This code works fine and allows me to insert data in sql without refresh.
But, how can I put the javascript as external script and the variables passing coretly for each item?
I want to reorganize like this:
PHP
<?php include("db.php"); ?>
<html>
<head>
<title>Title</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
</head>
<body>
<div align="center">
<table cellpadding="0" cellspacing="0" width="500px">
<?php $sql = "SELECT * FROM items ORDER BY ID DESC";
$items= mysql_query($sql);
while ($item= mysql_fetch_array($items)){
$id = $item[ID]; ?>
<tr style="border: solid 4px red;">
<td>
<div class="<?= $id ?>">
<button type="submit" id="<?= $id ?>" name="not" value="<?= $id ?>">BUTTON <?= $id ?></button>
</div>
</td>
</tr>
<?php } ?>
</table>
<div id="flash" align="left" ></div>
<ol id="update" class="timeline"></ol>
<div id="old_updates"></div>
</div>
<script src="script.js"></script>
</body>
</html>
When script.js is this
<script type="text/javascript">
$(function() {
$(".<?= $id ?>").click(function() {
var element = $(this);
var boxval = $("#<?= $id ?>").val();
var dataString = 'not='+ boxval;
$("#flash").show();
$("#flash").fadeIn(200).html('');
$.ajax({
type: "POST",
url: "update_data.php",
data: dataString,
cache: false,
success: function(html){
$("ol#update").prepend(html);
$("ol#update li:first").slideDown("slow");
document.getElementById('content').value='';$("#flash").hide();
}
});
return false;
});
});
</script>