2012-09-26 78 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>';