cleary1981 Posted June 23, 2008 Share Posted June 23, 2008 Hi All, I have been trawling the net for a decent example of this but I can't find one. What I am looking for is an example (html, js and php files)of how to populate a dropdown box on page load with an item from a table in a mysql db where a description of the item selected is also displayed. i know this can be done but need a guru to show me the way without losing me. Quote Link to comment https://forums.phpfreaks.com/topic/111492-solved-populate-dropdown-box-php-ajax-amp-mysql/ Share on other sites More sharing options...
MadTechie Posted June 23, 2008 Share Posted June 23, 2008 i wrote a example awhile back try here Quote Link to comment https://forums.phpfreaks.com/topic/111492-solved-populate-dropdown-box-php-ajax-amp-mysql/#findComment-572201 Share on other sites More sharing options...
cleary1981 Posted June 23, 2008 Author Share Posted June 23, 2008 Yeah I seen your example and im sure it works fine for those who understand it. Any chance you can write a simple example for us beginners. Im getting confused about where you have emulated the database and what i need to change Quote Link to comment https://forums.phpfreaks.com/topic/111492-solved-populate-dropdown-box-php-ajax-amp-mysql/#findComment-572212 Share on other sites More sharing options...
MadTechie Posted June 23, 2008 Share Posted June 23, 2008 OKay read the comments.. this is an NON-AJAX version i hope it helps See comments with the ** at the start <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Simple Dymanic Drop Down</title> </head> <body> <!-- OK a basic form--> <form method="post" enctype="multipart/form-data" name="myForm" target="_self"> <table border="0"> <tr> <td> <select name="list1"> <option value=''></option> <?php //**(BELOW) CHANGE HOST, USERNAME AND PASSWORD $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); $selected = mysql_select_db("first_test",$dbh) //<--**CHANGE DATABASE NAME (from first_test) if (!mysql_query("SELECT * FROM table")) //<--**CHANGE TABLE NAME (from table) { echo "Database is down"; } while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { //**(Below update ID to the UniqueID or Name ALSO update the name (these are fields)) (from ID and Name) echo "<option value='".$row['ID']."'>".$row['Name']."</option>\n"; } mysql_close($dbh); ?> </select> </td> </tr> </table> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> In the Subject you asked for AJAX, which you don't need if your getting the drop down populated from the load.. if you need to add another dropdown populated depending the on the selection from the first one then you can review my first example, i'll try to help with any questions.. but i think i may write a real tutorial. when time permits Quote Link to comment https://forums.phpfreaks.com/topic/111492-solved-populate-dropdown-box-php-ajax-amp-mysql/#findComment-572219 Share on other sites More sharing options...
cleary1981 Posted June 23, 2008 Author Share Posted June 23, 2008 Hey sorry again I tried your code and it didnt work. I created a new table called ex_table with only the two fields ID and Name. Below is the code i used <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Simple Dymanic Drop Down</title> </head> <body> <!-- OK a basic form--> <form method="post" enctype="multipart/form-data" name="myForm" target="_self"> <table border="0"> <tr> <td> <select name="list1"> <option value=''></option> <?php //**(BELOW) CHANGE HOST, USERNAME AND PASSWORD $dbh = mysql_connect('localhost', 'admin', 'password') or die("Unable to connect to MySQL"); $selected = mysql_select_db("tes",$dbh) //<--**CHANGE DATABASE NAME (from first_test) if (!mysql_query("SELECT * FROM ex_table")) //<--**CHANGE TABLE NAME (from table) { echo "Database is down"; } while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { //**(Below update ID to the UniqueID or Name ALSO update the name (these are fields)) (from ID and Name) echo "<option value='".$row['ID']."'>".$row['Name']."</option>\n"; } mysql_close($dbh); ?> </select> </td> </tr> </table> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/111492-solved-populate-dropdown-box-php-ajax-amp-mysql/#findComment-572256 Share on other sites More sharing options...
MadTechie Posted June 23, 2008 Share Posted June 23, 2008 Okay i added some debugging, try this and report back any errors <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Simple Dymanic Drop Down</title> </head> <body> <?php //**(BELOW) CHANGE HOST, USERNAME AND PASSWORD $dbh = mysql_connect('localhost', 'admin', 'password') or die("Unable to connect to MySQL"); $selected = mysql_select_db("tes",$dbh) //<--**CHANGE DATABASE NAME (from first_test) $result = mysql_query("SELECT * FROM ex_table") or die(mysql_error()); if (!$result) //<--**CHANGE TABLE NAME (from table) { echo "Database is down"; } ?> <!-- OK a basic form--> <form method="post" enctype="multipart/form-data" name="myForm" target="_self"> <table border="0"> <tr> <td> <select name="list1"> <option value=''></option> <?php while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { //**(Below update ID to the UniqueID or Name ALSO update the name (these are fields)) (from ID and Name) echo "<option value='".$row['ID']."'>".$row['Name']."</option>\n"; } ?> </select> </td> </tr> </table> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/111492-solved-populate-dropdown-box-php-ajax-amp-mysql/#findComment-572268 Share on other sites More sharing options...
cleary1981 Posted June 23, 2008 Author Share Posted June 23, 2008 I have overwritten what i had with your new version. I still get the empty drop down box in the corner. My connection is definately working as I have used it in other pages. Quote Link to comment https://forums.phpfreaks.com/topic/111492-solved-populate-dropdown-box-php-ajax-amp-mysql/#findComment-572279 Share on other sites More sharing options...
cleary1981 Posted June 24, 2008 Author Share Posted June 24, 2008 Can anyone else help me with this? Quote Link to comment https://forums.phpfreaks.com/topic/111492-solved-populate-dropdown-box-php-ajax-amp-mysql/#findComment-573082 Share on other sites More sharing options...
MadTechie Posted July 1, 2008 Share Posted July 1, 2008 is the page live (so i can look)? try this test <?php //existing code $dbh = mysql_connect('localhost', 'admin', 'password') or die("Unable to connect to MySQL"); $selected = mysql_select_db("tes",$dbh) //<--**CHANGE DATABASE NAME (from first_test) $result = mysql_query("SELECT * FROM ex_table") or die(mysql_error()); if (!$result) //<--**CHANGE TABLE NAME (from table) { echo "Database is down"; } //Add test code echo "Found: ".mysql_num_rows($result)."<br>"; while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { print_r($row); echo "<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/111492-solved-populate-dropdown-box-php-ajax-amp-mysql/#findComment-579007 Share on other sites More sharing options...
cleary1981 Posted July 1, 2008 Author Share Posted July 1, 2008 Sorry i should have closed this thread because I worked it out. I will post my code here for whoever wants it. this is the snippet of code from my html file <FORM name="drop_list"> <label for="type">Type</label> <div class="div_texbox"> <SELECT NAME="Category" onChange="SelectSubCat();" > <Option value="">Select Type</option> </SELECT> </div> <label for="mod_named">Model</label> <div class="div_texbox"> <SELECT id="model" NAME="SubCat" onChange="get_description();"> <Option value="">Select Model</option> </SELECT> </div> heres the php & javascript <?php require "config.php"; // database connection details echo " function fillCategory(){ // this function is used to fill the module type list on load "; $q1=mysql_query("select * from module_type"); echo mysql_error(); while($nt1=mysql_fetch_array($q1)){ echo "addOption(document.drop_list.Category, '$nt1[type]', '$nt1[type]');"; }// end of while ?> } // end of JS function function SelectSubCat(){ // ON or after selection of type this function will work removeAllOptions(document.drop_list.SubCat); addOption(document.drop_list.SubCat, "", "Select Model", ""); // Collect all element of model for various type <?php // let us collect all type and then collect all module_name for each type $q2=mysql_query("select distinct(type) from module"); while($nt2=mysql_fetch_array($q2)){ echo "if(document.drop_list.Category.value == '$nt2[type]'){"; $q3=mysql_query("select module_name from module where type='$nt2[type]'"); while($nt3=mysql_fetch_array($q3)){ echo "addOption(document.drop_list.SubCat,'$nt3[module_name]', '$nt3[module_name]');"; } // end of while loop echo "}"; // end of JS if condition } ?> } ////////////////// function removeAllOptions(selectbox) { var i; for(i=selectbox.options.length-1;i>=0;i--) { //selectbox.options.remove(i); selectbox.remove(i); } } function addOption(selectbox, value, text ) { var optn = document.createElement("OPTION"); optn.text = text; optn.value = value; selectbox.options.add(optn); } Quote Link to comment https://forums.phpfreaks.com/topic/111492-solved-populate-dropdown-box-php-ajax-amp-mysql/#findComment-579015 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.