populate text boxes with data from a dynamic mysql pull-down menu

Posted on 2007-08-08
Medium Priority
Last Modified: 2013-12-12
I am trying to populate the text fields once a user selects an entry from the first pull-down menu.  I am using php and have created two files.  The first file (so far) just containts the form, similar to this (left out much detail for brevity):

$query = "SELECT firstname, lastname, role FROM userdb";
$all_users = mysql_query($query,$mysql);
some html
Select a user from the pulldown:
<form method='POST' action='moduser.php'>
<select name='userid'>
     while ($data = mysql_fetch_array($all_users, MYSQL_ASSOC)) {
          echo "<option value='{$data['username']}'>{$data['firstname']}</option>\n";
<input type='text' name='firstname'>
<input type='text' name='username'>
<select name="role">
  <option value="user">User</option>
  <option value="admin">Admin</option>
<input type='submit' value='Modify'>

The second file, moduser.php, is basically just an error checking php script that will update the fields in the database after the user submits the form.

However, I seem to be stuck at this point.  I don't know what steps to take to allow php to update all the text entries (and select entry) in the form, once someone selects a username from the pulldown menu.  I'm also using mysql to populate the first pull-down menu of usernames.

Any help is greatly appreciated...

Question by:con2007

Accepted Solution

AymanAdam earned 2000 total points
ID: 19660462
hi con,
i think to accomplish what you want you have to submit the form when the user select a name. and then collect what the user selected and then use this value to get his details form the db and display them in the text fields thorough assigning the variables to the VALUE=$variable in the text field, that way you can do it.
to submit the form based on user selection you have to use java script, use the following function to do that, insert this function in the <header> tag:
<SCRIPT language="JavaScript">
function submitform()
change (form1 to your form name.

then change  <select name='userid'>
to <select name='userid' onClick="javascript:submitform();">

by using this way when user selects a value the form will be submitted, and u can get this selection easily.
to display the data in the text fields use <input type='text' name='firstname' value=$firstname>
in this case $firstname should be pulled from ur db.

any further help please ask
LVL 19

Expert Comment

by:http:// thevpn.guru
ID: 19660605
You could do the following

$name = $_POST['name'];
$address = $_POST['address'];

//check and sql..
$name = '';
$address = ''

<form action='?' method='POST'>

<input type='text' value='<?php echo $name ?>' >


Expert Comment

ID: 19660971
I think you want the user profile edit functionality , u do the following steps to achieve this ,

1.By using select box u got the userID (i assume that in your case username should be unique and u must have userID as primary key).
2. Based on that username , u will show other information like firstname , lastname by default.
3. If suppose user want to change the firstname , lastname and other information your form will allow.

After submit ,
1.you will update the user information with update statement and using WHERE username != $_POST["username"} and userid != $_POST["user_id"];

so , user can change their information and there is no chance for duplicate entry...

Author Comment

ID: 19663733

I think your response was the closest to what I need to accomplish but I can't seem to get it to work.

 Basically, when I select from the dropdown menu, I need it to populate the appropriate text entries below it.  Whether this be done via php or javascript I don't mind.  But with the code given above, every time I click on the pulldown menu (before I select a name) it refreshes the screen.  So I changed it to onSelect, instead of onClick, but I don't see anything happening.  However, this re-runs the backend php script and it does not populate the fields.

Author Comment

ID: 19664317

I will award you the points, although I am still stuck with this.  I am researching this more and realizing it may need ajax.  I will repost in another section to hopefully get some advice from some ajax experts.


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question