Tje Posted July 10, 2014 Share Posted July 10, 2014 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/289648-how-to-display-db-values-in-textbox/ Share on other sites More sharing options...
mogosselin Posted July 10, 2014 Share Posted July 10, 2014 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 > Quote Link to comment https://forums.phpfreaks.com/topic/289648-how-to-display-db-values-in-textbox/#findComment-1484507 Share on other sites More sharing options...
Solution cyberRobot Posted July 10, 2014 Solution Share Posted July 10, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/289648-how-to-display-db-values-in-textbox/#findComment-1484509 Share on other sites More sharing options...
Tje Posted July 11, 2014 Author Share Posted July 11, 2014 Thanks...It's working Quote Link to comment https://forums.phpfreaks.com/topic/289648-how-to-display-db-values-in-textbox/#findComment-1484637 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.