1

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.

7
  • you need to start using mysqli or pdo..also you can use onclick edit on your td tags using ajax.. Commented Nov 22, 2012 at 11:47
  • Collect the row id-s for example on button click and than you will know what has been selected to add to cart. You can create multiple forms for each row and on submit collect the id or you can have checkboxes for each row and one submit button Commented Nov 22, 2012 at 11:51
  • @vodich where and how to collect row id? Commented Nov 22, 2012 at 11:54
  • If you have $row[Link] than I guess that there is also id in same table. Just put $row[id] in <input type='hidden' name='id' value='<?php echo $row[id];?>' /> You can use $_SESSION to store quantity and id-s of products while the user is on ine page and then process data and save it in db if the user is finished with the shopping Commented Nov 22, 2012 at 12:23
  • @vodich yeah i have id in my table. I guess u think i'm working on shopping cart... it isn't right. i'm just making a php form to save links to websites but as per my needs i have to add, edit and delete the records from my database Commented Nov 22, 2012 at 12:34

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

7 Comments

I don't need to edit any value, I just need to get values in my textboxes when i click on tne button.
can you plz explain this line for me? var link Val = document.getElementById('link'+rowID+'').innerHTML.replace(/<\/?[^>]+(>|$)/g, "\n");
it strips out the anchor tag from link td and assigns the rest to var linkVal..in case you won't use it then tb2 will also display the anchor tag along with link td value..
Okay I got it now thanks so much for your help and time. I really Appreciate it. thanks again.
i have one more question that is still unanswered can you please help me for that?
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.