Jump to content

Recommended Posts

I'm creating a form with html and the values for my drop down menus come from mysql tables.  Got the first one working great, but i need the 2nd one to dynamically update based on the selection of the first (from what i know .. using onChange).  When the 2nd one is updated it needs to call mysql again (accessing a different table if that matters) to get its drop down options based on the value of what was selected in the first.. Only way i know how to do this is javascript and since it doesn't support mysql, my knowledge is very limited on this topic.  Any help is greatly appreciated and if anything didn't make sense please just let me know.

-- Brady
Link to comment
https://forums.phpfreaks.com/topic/13420-dynamic-mysql-form/
Share on other sites

This will do it
[code]<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<SCRIPT language="javascript">
        function setID (id, textid) {
              document.getElementById(textid).value = id;
        }
</SCRIPT>
</head>
<body>
<form>
<select name="user" onchange='setID(this.value,"userid")'>
  <option value=''>- select user -</option>
  <option value="1">User 1</option>
  <option value="2">User 2</option>
  <option value="3">User 3</option>
</select>
<input type="text" name="id" id="userid" size="5">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>[/code]

But it does raise the question "Why do you want to put the id in a text box?"

You should not br editing/changing id values.
Link to comment
https://forums.phpfreaks.com/topic/13420-dynamic-mysql-form/#findComment-52604
Share on other sites

Thanks for the reply, but I still need to be able to have the values of the drop down list to be the data from the MySQL database.  I know I need a query to select the name and the ID from the table, then I need to have a drop down list with the names and a text box (uneditable) for the id's to be displayed.  I think I will still need a javascript function, but I dont know how to create it. 
Link to comment
https://forums.phpfreaks.com/topic/13420-dynamic-mysql-form/#findComment-52655
Share on other sites

try

[code]<html>
<head>
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<SCRIPT language="javascript">
        function setID (id, textid) {
              document.getElementById(textid).value = id;
        }
</SCRIPT>
</head>
<body>
<form>
<select name="user" onchange='setID(this.value,"userid")'>
  <option value=''>- select user -</option>
 
  <?php
            //  connect to mysql server and select db  here
           
          $sql = "SELECT id, name FROM mytablename ORDER BY name";
          $res = mysql_query($sql) or die(mysql_error());
          while (list($id, $name) = mysql_fetch_row($res)) {
            echo "<option value='$id'>$name</option>" ;
          }
  ?>
 
 
</select>
<input type="text" name="id" id="userid" size="5" readonly>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/13420-dynamic-mysql-form/#findComment-52694
Share on other sites

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.