需要在PHP中按钮单击数据库中的文本框中获取值

时间:2012-11-22 11:45:46

标签: php javascript html onclick

我需要使用PHP和Javascript在按钮单击的文本框中获取数据库的值。例如,我从数据库表中获取HTML表中的值。当用户点击add0按钮时,我需要在文本框中获取相应的值。

这是我的代码:

<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);
?>

按钮点击我需要更多代码。

2 个答案:

答案 0 :(得分:2)

imho你可以使用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>

答案 1 :(得分:1)

使用从数据库中获取的默认值重新创建表单。

<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);
?>

你只需要根据某些东西的反击来改变对象的名称......