wispas Posted January 5, 2010 Share Posted January 5, 2010 Hi All, I currently have a drop dpwn menu that works and retrieves options from my MySQL database. when i select a option from the drop down menu it currently has a selection of the hotel names. (hotel_name) what i want it to do is when i have selected a hotel from the lists i want it to show the hotel name and its description underneath. (hotel_desc) Is this possible to do without having the page reload onto a new page? Quote Link to comment https://forums.phpfreaks.com/topic/187247-display-results-upon-drop-down-selection-phpmysql/ Share on other sites More sharing options...
wispas Posted January 5, 2010 Author Share Posted January 5, 2010 Heres my code so far: <?php // Connect database include("includes/connectdb.php"); $sql="SELECT hotel_id, hotel_name FROM hotel ORDER BY hotel_name ASC"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $id=$row["hotel_id"]; $hotel_name=$row["hotel_name"]; $options.="<OPTION VALUE=\"$id\">".$hotel_name; } ?> <!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=utf-8" /> <title>PHP Dynamic Drop Down Menu</title> </head> <body> <form id="form1" name="form1" method="post" action="selected.php"> <SELECT NAME=id> <OPTION VALUE=0>Choose <?=$options.="<OPTION VALUE=\"$id\">".$hotel_name.'</option>';?> </SELECT> <label> <input type="submit" name="submit" id="submit" value="Submit" /> </label> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/187247-display-results-upon-drop-down-selection-phpmysql/#findComment-988811 Share on other sites More sharing options...
kickstart Posted January 5, 2010 Share Posted January 5, 2010 Hi Simplest way would be to use Javascript, and save an array of hotel descriptions when the page is created. Use an onchange event on the SELECT to trigger a function to find the appropriate description from the array and put it into a div to display it. You could also do something similar using AJAX. However that would mean more hits on the server. Depends on how many hotels there are and how big the descriptions are. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/187247-display-results-upon-drop-down-selection-phpmysql/#findComment-988817 Share on other sites More sharing options...
wispas Posted January 5, 2010 Author Share Posted January 5, 2010 I got an alert to work now.... when i select from the lists it will give me an alert box telling me of the selected item... does anyone know how to get that text to display in a div instead of an alert box. <?php // Connect database include("includes/connectdb.php"); $sql="SELECT hotel_id, hotel_name, hotel_location FROM hotel ORDER BY hotel_name ASC"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $id=$row["hotel_id"]; $hotel_name=$row["hotel_name"]; $hotel_location=$row["hotel_location"]; $options.="<OPTION VALUE=\"$id\">".$hotel_name." (".$hotel_location.")"; } ?> <!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=utf-8" /> <script language="javascript"> function disp_text() { var w = document.myform.mylist.selectedIndex; var selected_text = document.myform.mylist.options[w].text; alert(selected_text); } </script> <title>PHP Dynamic Drop Down Menu</title> </head> <body> <form id="myform" name="myform" method="post" action="selected.php"> <SELECT NAME="mylist" onChange="disp_text()"> <OPTION VALUE=0>Choose <?=$options.="<OPTION VALUE=\"$id\">".$hotel_name.'</option>';?> </SELECT> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/187247-display-results-upon-drop-down-selection-phpmysql/#findComment-988830 Share on other sites More sharing options...
kickstart Posted January 5, 2010 Share Posted January 5, 2010 Hi Something like this would do it:- <?php // Connect database include("includes/connectdb.php"); $sql="SELECT hotel_id, hotel_name, hotel_location FROM hotel ORDER BY hotel_name ASC"; $result=mysql_query($sql); $options=""; while ($row=mysql_fetch_array($result)) { $id=$row["hotel_id"]; $hotel_name=$row["hotel_name"]; $hotel_location=$row["hotel_location"]; $options.="<OPTION VALUE=\"$id\">".$hotel_name." (".$hotel_location.")"; } ?> <!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=utf-8" /> <script language="javascript"> function disp_text() { var w = document.myform.mylist.selectedIndex; var selected_text = document.myform.mylist.options[w].text; document.getElementById('').innerHTML = selected_text; alert(selected_text); } </script> <title>PHP Dynamic Drop Down Menu</title> </head> <body> <form id="myform" name="myform" method="post" action="selected.php"> <SELECT NAME="mylist" onChange="disp_text()"> <OPTION VALUE=0>Choose <?=$options.="<OPTION VALUE=\"$id\">".$hotel_name.'</option>';?> </SELECT> </form> <div id='someDivName'>Nowt Selected Yet</div> </body> </html> All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/187247-display-results-upon-drop-down-selection-phpmysql/#findComment-988883 Share on other sites More sharing options...
wispas Posted January 5, 2010 Author Share Posted January 5, 2010 thanks so much 'kickstart'. worked well!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/187247-display-results-upon-drop-down-selection-phpmysql/#findComment-988999 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.