Jump to content

How to display DB values in textbox


Go to solution Solved by cyberRobot,

Recommended Posts

Hi,

 

i'm trying to display db(name,email,telephone no) values in textbox.please help me.

 

::HTML part::

 

 

<form class="form-signin" role="form" id="form1" action="post_user.php">
      
       <label class="">Name</label>
        <input type="text" class="form-control" placeholder="Your Name" value="" required name="yname" id="yname" autofocus ><br/>
         <label class="">Email</label>
                <input type="text" class="form-control" name="email" id="email" placeholder="Email address" required > <br/>
                   
            
    
    <label class="">Telephone Number</label>
       <input type="text" class="form-control" pattern="\d+" placeholder="Telephone" name="telephone" required><br/>
 
<div class="col-md-4 col-md-offset-4">
        <button class="btn btn-lg btn-primary btn-block" type="submit">Submit Ad</button>
        </div>
</form>
 
 
 
::post_user.php::
 
 
<?php
 
$con = mysql_connect("localhost","root","");
if(!$con)
{
die('Could not connect:'.mysql_error());
}
mysql_select_db("biz",$con);
 
$mail=$_POST['email'];
 
 
$query = mysql_query("SELECT yname,mail,tp FROM reg WHERE mail = '$mail' ");
 
while($row = mysql_fetch_array($query))
    {
   
 echo $row['yname'];     
  echo $row['mail'];   
   echo $row['tp'];                
    } 
mysql_close($con);
?>
 
Link to comment
https://forums.phpfreaks.com/topic/289648-how-to-display-db-values-in-textbox/
Share on other sites

Let's say that you have code that will hold what you want to display in a variable, like this:

$username = 'Mark';

And then, you need to display it in an input box, you would do this:

<input type="text" value="<?php echo $username; ?>">

So, in your code, instead of this line:

echo $row['yname'];     

you would set a variable (instead of outputting it right on the screen)

$yname = $row['yname']; 

Then, later on, in your input field:

 <input type="text" class="form-control" placeholder="Your Name" value="<?php echo $yname; ?>" required name="yname" id="yname" autofocus >
  • Solution

Do you currently have the HTML and PHP parts in different scripts? If so, they'll need to be merged so you can use the PHP variables in form as mogosselin suggested. Your code might look something like the following:

<?php
//...
 
$query = mysql_query("SELECT yname,mail,tp FROM reg WHERE mail = '$mail' LIMIT 1");
$row = mysql_fetch_assoc($query);
mysql_close($con);
?>
<form class="form-signin" role="form" id="form1" action="post_user.php">
    <label>Name <input type="text" class="form-control" placeholder="Your Name"<?php if($row['yname'] != '') { echo ' value="' . $row['yname'] . '"'; } ?> required name="yname" id="yname" autofocus /></label><br />
    
...
 
</form>
 
Note that I added the LIMIT clause to the query since you're probably only looking for one result. If that's a correct assumption, you don't need the loop. I also modified the <label> tag so that it's attached to the corresponding input field.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.