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

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):

<?php
session_start();
...
$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'>
<?php
     while ($data = mysql_fetch_array($all_users, MYSQL_ASSOC)) {
          echo "<option value='{$data['username']}'>{$data['firstname']}</option>\n";
     }
?>
</select>
Firstname:
<input type='text' name='firstname'>
Username:
<input type='text' name='username'>
Role:
<select name="role">
  <option value="user">User</option>
  <option value="admin">Admin</option>
</select>
<input type='submit' value='Modify'>
</form>
...
</html>

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

Thanks!
con2007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
AymanAdamCommented:
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()
{
document.form1.submit();
}
</SCRIPT>
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
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
http:// thevpn.guruCommented:
You could do the following

if(isset($_POST['submit']))
{
$name = $_POST['name'];
$address = $_POST['address'];

//check and sql..
}
else
{
$name = '';
$address = ''
}


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

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

0
 
skumara_samyCommented:
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...
0
 
con2007Author Commented:
AymanAdam:

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.
0
 
con2007Author Commented:
aymanadam,

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.

Thanks...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.