Jump to content


Photo

Dynamic Mysql Form


  • Please log in to reply
11 replies to this topic

#1 meman1188

meman1188
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 02 July 2006 - 12:36 AM

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


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 02 July 2006 - 07:22 PM

Have a look at AJAX article
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 03 July 2006 - 05:21 AM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 mwq27

mwq27
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 03 July 2006 - 03:34 PM

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

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 03 July 2006 - 03:47 PM

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.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#6 mwq27

mwq27
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 03 July 2006 - 03:50 PM

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

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 03 July 2006 - 07:27 PM

This will do it
<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>

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.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 mwq27

mwq27
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 03 July 2006 - 08:58 PM

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. 

#9 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 03 July 2006 - 09:47 PM

This is no longer really a MySQL question anymore, is it?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 03 July 2006 - 10:47 PM

try

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 meman1188

meman1188
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 05 July 2006 - 04:35 AM

Found a great tutorial to do what i was looking for, maybe it will help someone else:
http://www.plus2net....p_down_list.php

#12 mwq27

mwq27
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 05 July 2006 - 03:48 PM

Thanks for the help, I got it to work. 




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users