Jump to content

Archived

This topic is now archived and is closed to further replies.

meman1188

Dynamic Mysql Form

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

Share this post


Link to post
Share on other sites
Have a look at [url=http://members.aol.com/barryaandrew/xmlhttp/article.html]AJAX article[/url]

Share this post


Link to post
Share on other sites
Depending on the size of each dropdown, you might be able to retrieve both lists at runtime (e.g. province/country lists); alternatively, IFRAMEs work wonders.

Share this post


Link to post
Share on other sites
I am trying to do pretty much the same thing.  I want to have a drop down list with names from a table, and when the user clicks on a name, the ID number associated with that name will appear in a textbox next to the drop down list.  Can someone help me with this?  thanks

Share this post


Link to post
Share on other sites
Well, if you're already pulling all the names, it's the same amount of work to pull the IDs as well, so there's no need for another round-trip to the server.

Share this post


Link to post
Share on other sites
Ok, i got the id's, now how do I make it so when i click on the name, the ID will appear in the text box?  Will I have to do javascript or something? Thanks

Share this post


Link to post
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.

Share this post


Link to post
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. 

Share this post


Link to post
Share on other sites
This is no longer really a MySQL question anymore, is it?

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Found a great tutorial to do what i was looking for, maybe it will help someone else:
http://www.plus2net.com/php_tutorial/php_drop_down_list.php

Share this post


Link to post
Share on other sites

×

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.