Retrieving data from database and displaying in textbox when the button click

newphpcoder

New Member
I search about the code for retrieving data from database when the retrieve button was click. But I can’t find any codes that similar in my situation.

Here is the flow of my system. I have plt_no and a retrieve button. The retrieve button is to check if the plt_no is already exist or not if it is already exist the data from the database will appear in the text area/textbox and if the plt_no did not exist there’s a message that the id number did not exist and I can input data in the textboxes.


I have an idea of using this code:
This code is to check if the Retrieve button is click and the plt_no is the data which the retrieve depend on. Like for example the user put plt_no 111 and when the user click the Retrieve button, if the 111 is already exist the data will appear in the textboxes but if not there’s a message appear saying the plt no did not exist.

Code:
if(isset($_POST['Retrieve']))
$plt_no = $_POST['plt_no'];


And I have search code for retrieving data:

PHP:
<?php 
 // Connects to your Database 
 mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); 
 mysql_select_db("Database_Name") or die(mysql_error()); 
 $data = mysql_query("SELECT * FROM friends WHERE pet='Cat'") 
 or die(mysql_error()); 
 Print "<table border cellpadding=3>"; 
 while($info = mysql_fetch_array( $data )) 
 { 
 Print "<tr>"; 
 Print "<th>Name:</th> <td>".$info['name'] . "</td> "; 
 Print "<th>Color:</th> <td>".$info['fav_color'] . "</td> "; 
 Print "<th>Food:</th> <td>".$info['fav_food'] . "</td> "; 
 Print "<th>Pet:</th> <td>".$info['pet'] . " </td></tr>"; 
 } 
 Print "</table>"; 
 ?>

I attach my full code.

My problem is how can I used that code and if that code is the code I need to used for retrieving data.



I have an idea but i am luck of knowledge in coding

Any help is highly appreciated I ma willing to learn and share

Thank you
 

Attachments

  • retrieve.txt
    12.8 KB · Views: 44

Denno

New Member
To display information from a database in a form field once the button is pressed, you will need to refresh the page and run a new query to get the information (this is the easy way)
The other way is to use AJAX. I haven't had much experience myself with that, but I know that it can work by checking the database in the background (or asynchronously), so that the server database information can be display to the client.

To do it the easy way, you give the form no action, so it just reloads the same page, and in your if statement, you can check if a form was POSTed (therefore the button has just been pressed), if so, run the query and the appropriate php code to display the information on the page inside the text field.

Denno
 

Mr_John

New Member
To display information from a database in a form field once the button is pressed, you will need to refresh the page and run a new query to get the information (this is the easy way)
The other way is to use AJAX. I haven't had much experience myself with that, but I know that it can work by checking the database in the background (or asynchronously), so that the server database information can be display to the client.

To do it the easy way, you give the form no action, so it just reloads the same page, and in your if statement, you can check if a form was POSTed (therefore the button has just been pressed), if so, run the query and the appropriate php code to display the information on the page inside the text field.

Denno


Yes, that's true, you can check info from database like every 0.1 second refreshing only a part of page. It looks very nice, but I recomend refresh page.
 

jalicia18

New Member
Code:
if(isset($_POST['Retrieve']))
$plt_no = $_POST['plt_no'];

Search Form
PHP:
<form method="POST" action="">
Enter Plt No.<br>
<input type="text" name="plt_no"><br>
<input type="submit" name="Retrieve" value="Retrieve">
</form>

PHP Scripts
PHP:
<?php 
 // Connects to your Database 
 mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()); 
 mysql_select_db("Database_Name") or die(mysql_error()); 
 $data = mysql_query("SELECT * FROM friends WHERE pet='Cat'") 
 or die(mysql_error()); 
 if(isset($_POST['Retrieve']))
{
  $plt_no = $_POST['plt_no'];
  $sql = "SELECT * FROM tablename WHERE plt_no='$plt_no'";
  $result = mysql_query($sql);
  if(mysql_num_rows($result)<=0)
    $error = "the plt no did not exist" . "<br>";
  else
  {
     $info = mysql_fetch_array($result);
     $name = $info[name];
     $country = $info[country];
   }
}

Display Results
PHP:
<?php echo $error; ?>
Name <input type="text" value="<?php echo $name; ?>"><br>
Country <input type="text" value="<?php echo $country; ?>"><br>
 
Last edited:
Top