i am having a problem, i have a script that prints a table while getting values from the database, now, my problem is i want the values of each cell clickable, i know how to make that, i added
<a href="sample.php">row from query result is printed here</a>
everytime it prints a value in the cell
now, each cell has different values, and when ever a clicker clicks one of the values i have no idea on how will i get the value of the cell that the user clicked, any suggestions??
<a href="sample.php?value=<?php echo $yourvalue?>">row from query result is printed here</a>
You can pass value as a GET variable.
(edited below)
OR
using jquery [DEMO]
$(".tdclass").click(function(){
alert($(this).text());
//OR
//alert($(this).html());
});
You can easily identify td
using class. Oterwise you can use $(td).click()
.
add a class (say 'click_td') to each cell of the table. and then do this is JS.
$(document).ready(function() {
$('#table_1 .click_td').click(function(){
alert($(this).text());
});
});
I asume you want to treat the data with javascript. So, create a function in javascript:
function handle_db_data(var data){...do whatever you want here...}
And in order to call that function with the database data, you can do this:
<a name="row1" onclick="handle_db_data(ROW_FROM_DATABASE);">ROW_FROM_DATABASE</a>
Using jQuery would be even more easy!
<a href="sample.php?id=<?php echo $row['value']; ?>"><?php echo $row['value']; ?></a>
Your question is not vague but has little information, for instance, where do you want to keep the identifier of each cell? will it be in a hidden input field or will it be in a the link or even a javascript function? Either way this would be how you want to do this:
echo '<table>';
$counter = 1;
/* Assuming the database output is an associative array. */
foreach($db_data as $item) {
echo '<tr>';
echo '<td>'.$counter.'</td>';
/* if the row identifier will be in your link. */
echo '<td><a href="sample.php?rowid='.$item['pk_row_id'].'">row from query result is printed here</a></td>';
/* if the row identifier will be a hidden input field. */
echo '<td><input type="hidden" value="'.$item['pk_row_id'].'" name="rowid" id="rowid" />';
/* if the row identifier will be in a javascript function on click. */
echo '<td><a href="void(0)" onclick="userclickaction('.$item['pk_row_id'].')>Click Me to edit</a></td>`enter code here`';
echo '</tr>';
/* Increment counter. */
$counter++;
}
echo '</table>';