2012-09-26 143 views
0

我遇到问题,我有一个脚本,在从数据库获取值时打印表,现在,我的问题是我想让每个单元格的值可点击,我知道如何使我加从表中获取单元格的值

<a href="sample.php">row from query result is printed here</a> 

每次它打印在单元格的值

现在,每个单元都有不同的价值观,并当过一个唱首歌点击我有我将如何让不知道的一个值的用户点击单元格的值,有什么建议?

+0

如果你打开了jQuery,我可以为你提供你想要被发送的值传送到服务器端 – asprin

+1

做或你所需要的解决方案他们在客户端? –

+0

@KonstantinDinev无论哪种方式将是伟大的 –

回答

4
<a href="sample.php?value=<?php echo $yourvalue?>">row from query result is printed here</a> 

您可以通过值作为GET变量。使用jquery [DEMO]

$(".tdclass").click(function(){ 
    alert($(this).text()); 
    //OR 
    //alert($(this).html()); 
});​ 

可以使用类容易地识别td

(下面编辑)

OR

。此外,您可以使用$(td).click()

+0

+1不错。没想到这一点 – asprin

0

向表格的每个单元格添加一个类(例如'click_td')。然后做这个是JS。

 $(document).ready(function() { 
     $('#table_1 .click_td').click(function(){ 
    alert($(this).text()); 
    }); 

});​ 
+0

'.val()'作为'td'作品?我怀疑它 – asprin

+1

你想使用'.on()'并附加到表 –

+1

这将是.text(),而不是.val() – MLeFevre

0

我asume你想用JavaScript处理数据。因此,在JavaScript中创建一个函数:

function handle_db_data(var data){...do whatever you want here...} 

而且为了调用与数据库数据的功能,你可以这样做:

<a name="row1" onclick="handle_db_data(ROW_FROM_DATABASE);">ROW_FROM_DATABASE</a> 

使用jQuery会更加容易!

0
<a href="sample.php?id=<?php echo $row['value']; ?>"><?php echo $row['value']; ?></a> 
0

您的问题并不清楚,但信息很少,例如,您想在哪里保留每个单元格的标识符?它会在隐藏的输入字段中,或者它会在链接中,甚至是JavaScript函数中?无论哪种方式,这将是你怎么想这样做:

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>';