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

Posted on 2007-08-08
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
    LVL 3

    Accepted Solution

    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

    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 ?>' >

    LVL 3

    Expert Comment

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


    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


    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

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Suggested Solutions

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now