Bkay
Bkay

Reputation: 177

Need to get values in textbox from database on button click in PHP

I need to get values of database in text boxes on button click using PHP and Javascript. For instance, I get values in an HTML table from a database table. I need to get the respective values in the text boxes when the user clicks on the add0 button.

Here is my code:

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

I need further code on button click.

Upvotes: 1

Views: 4458

Answers (2)

000
000

Reputation: 3950

imho you can use Inline edit using Ajax in Jquery

Here is it's demo

It will let you edit your displayed contents in the table itself..

Update:

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

Upvotes: 1

BioSP
BioSP

Reputation: 518

Recreate your form with default values taken from the database.

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

You just need to change the name of the object based on whatever counter of something...

Upvotes: 1

Related Questions