2012-11-22 23 views
1

我需要使用PHP和Javascript在按鈕單擊文本框中獲取數據庫的值。例如,我從數據庫表中獲取HTML表格中的值。當用戶點擊add0按鈕時,我需要在文本框中獲取相應的值。需要在數據庫中獲取文本框中的值按下PHP中的按鈕點擊

這裏是我的代碼:

<form method="post" action=""> 
    <input type="text" name="tb1" /> 
    <input type="text" name="tb2" /> 
    <input type="submit" name="btn" value="Find" /> 
</form> 
<?php 
    include "conn.php"; 
    $show = "SELECT * FROM data"; 
    $rs = mysql_query($show) or die(mysql_error()); 
    $add_to_textbox = "<input type='button' name='btn' value='add0' />"; 
    #****results in Grid**** 
    echo "<table width='360px' border='1' cellpadding='2'>"; 
    while($row = mysql_fetch_array($rs)) { 
    echo "<tr>"; 
    echo "<td width='130px'>$row[Name]</td>"; 
    echo "<td width='230px'><a href = '$row[Link]'>$row[Link]</a></td>"; 
    echo "<td width='130px'>$add_to_textbox</td>"; 
    echo "</tr>"; 
    } 
    echo "</table>"; 
    #********************** 
    mysql_free_result($rs); 
?> 

我需要按一下按鈕進一步代碼。

+0

你需要開始使用庫MySQLi或pdo..also可以使用的onclick使用您的TD標籤編輯來改變對象的名稱ajax .. –

+0

收集行id -s例如在按鈕點擊和比你會知道什麼已被選中添加到購物車。你可以爲每一行創建多個表單,並提交收集id,或者你可以有每個行的複選框和一個提交按鈕 – vodich

+0

@vodich在哪裏以及如何收集行ID? – Bkay

回答

2

恕我直言,你可以使用 Inline edit using Ajax in Jquery

下面是它的demo

它可以讓你在表本身編輯您顯示的內容..

更新:

<form method="post" action=""> 
    <input type="text" name="tb1" id="tb1" /> 
    <input type="text" name="tb2" id ="tb2" /> 
    <input type="submit" name="btn" value="Find" /> 
</form> 
<?php 
    include "conn.php"; 
    $show = "SELECT * FROM data"; 
    $rs = mysql_query($show) or die(mysql_error()); 
    $add_to_textbox = "<input type='button' name='btn' value='add0' />"; 
    #****results in Grid**** 
    echo "<table width='360px' border='1' cellpadding='2'>"; 
    $rowID=1; 
    while($row = mysql_fetch_array($rs)) { 
    echo "<tr>"; 
    echo "<td width='130px' id='name".$rowID."'>$row[Name]</td>"; 
    echo "<td width='230px' id='link".$rowID."'><a href = '$row[Link]'>$row[Link]</a></td>"; 
    echo "<td width='130px' onclick='txtValDisp($rowID);'>$add_to_textbox</td>"; 
    echo "</tr>"; 
    $rowID++; 
    } 
    echo "</table>"; 
    #********************** 
    mysql_free_result($rs); 
?> 
<script type="text/javascript"> 
function txtValDisp(rowID){ 
    var linkVal = document.getElementById('link'+rowID+'').innerHTML.replace(/<\/?[^>]+(>|$)/g, "\n"); 
    document.getElementById("tb1").value = document.getElementById('name'+rowID+'').innerHTML; 
    document.getElementById("tb2").value = linkVal; 
    } 
</script> 
+0

做到這一點我不需要編輯任何值,我只需要在我的文本框中獲取值,當我點擊按鈕。 – Bkay

+0

你可以檢查我的更新的答案。 –

+0

它按照我的要求工作...謝謝這麼糊@Rishi .. – Bkay

1

用d重新創建表單從數據庫中提取默認值。

<form method="post" action=""> 
<input type="text" name="tb1" /> 
<input type="text" name="tb2" /> 
<input type="submit" name="btn" value="Find" /> 
</form> 
<?php 
include "conn.php"; 
$show = "SELECT * FROM data"; 
$rs = mysql_query($show) or die(mysql_error()); 
$add_to_textbox = "<input type='button' name='btn' value='add0' />"; 
    #****results in Grid**** 
echo "<table width='360px' border='1' cellpadding='2'>"; 
while($row = mysql_fetch_array($rs)) { 
echo "<tr>"; 
echo "<td><input name ='INSERT_HERE' type=text value='"$row[Name]"'></td>"; 
echo "</tr>"; 
} 
echo "</table>"; 
#********************** 
mysql_free_result($rs); 
?> 

你只需要根據的東西無論反...

相關問題