473,836 Members | 1,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP - Dynamic Drop Down Menu

12 New Member
I've created a dynamic drop down menu that populates itself with data from a MySQL table. What I would like to do is create a non dynamic drop down menu that alters what is shown in the dynamic menu.

For example, the first menu has three cities London, Paris, New York. If I choose London it populates the second menu with people from London.

Here is the code I have for my dynamic menu

Expand|Select|Wrap|Line Numbers
  1. <td valign=top><strong>Name:</strong></td>
  2.     <td>
  3.     <?php
  4.     echo "<select name=\"name\">"; 
  5.     echo "<option size =30 selected>Select</option>";
  6.     if(mysql_num_rows($sql_result)) 
  7.     { 
  8.     while($row = mysql_fetch_assoc($sql_result)) 
  9.     { 
  10.     echo "<option>$row[name]</option>"; 
  11.     } 
  12.  
  13.     } 
  14.     else {
  15.     echo "<option>No Names Present</option>";  
  16.     } 
  17.     ?>
  18.     </td>
  19.     </tr>    
  20.  
So basically, I need a standard menu, i.e:

Expand|Select|Wrap|Line Numbers
  1. <tr>
  2.     <td valign=top><strong>Sites:</strong></td>
  3.     <td> <select name="cities">
  4.     <option selected>Select</option>
  5.     <option>London</option>
  6.     <option>Paris</option>
  7.     <option>New York</option>
  8.     </select>
  9.     </tr>
  10.     </td>
  11.  
I think I need to hold the value of cities when selected in a variable and create an if statement with various sql query results to be run depending on what option is chosen.
Mar 6 '06 #1
19 259009
Banfa
9,065 Recognized Expert Moderator Expert
I think you may have forgotten or not understood that PHP runs of the server and the implications of this.

PHP runs on the server as you request a page returning the required output. This output may be obtained from a MySql database or any other sourse that PHP can access.

What this means though is that if you want to generate source using PHP you have to make a server request of some sort.

In practive this means for a simple page either reloading the current or a new page or loading a page in an IFRAME.

However I get the impression that you want to have a single page with 2 selection boxes on it and fill one depending on what the selection in the other is.

It is possible (I have never done it) using XML requests to reload content in a div without reloading the rest of the page using code that looks something like

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3. /***********************************************
  4. * Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
  5. * This notice MUST stay intact for legal use
  6. * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
  7. ***********************************************/
  8.  
  9. var loadedobjects=""
  10. var rootdomain="http://"+window.location.hostname
  11.  
  12. function ajaxpage(url, containerid){
  13.   var page_request = false
  14.  
  15.   if (window.XMLHttpRequest) // if Mozilla, Safari etc
  16.     page_request = new XMLHttpRequest()
  17.   else if (window.ActiveXObject){ // if IE
  18.     try {
  19.       page_request = new ActiveXObject("Msxml2.XMLHTTP")
  20.     } 
  21.     catch (e){
  22.       try{
  23.         page_request = new ActiveXObject("Microsoft.XMLHTTP")
  24.       }
  25.       catch (e){}
  26.     }
  27.   }
  28.   else
  29.     return false
  30.  
  31.   page_request.onreadystatechange=function(){
  32.     loadpage(page_request, containerid)
  33.   }
  34.  
  35.   page_request.open('GET', url, true)
  36.   page_request.send(null)
  37. }
  38.  
  39. function loadpage(page_request, containerid){
  40.   if (page_request.readyState == 4 
  41.   && (page_request.status==200 || window.location.href.indexOf("http")==-1))
  42.     document.getElementById(containerid).innerHTML=page_request.responseText
  43. }
  44.  
  45. function loadobjs(){
  46.   if (!document.getElementById)
  47.     return
  48.  
  49.   for (i=0; i<arguments.length; i++){
  50.     var file=arguments[i]
  51.     var fileref=""
  52.  
  53.     if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
  54.       if (file.indexOf(".js")!=-1){ //If object is a js file
  55.         fileref=document.createElement('script')
  56.         fileref.setAttribute("type","text/javascript");
  57.         fileref.setAttribute("src", file);
  58.       }
  59.       else if (file.indexOf(".css")!=-1){ //If object is a css file
  60.         fileref=document.createElement("link")
  61.         fileref.setAttribute("rel", "stylesheet");
  62.         fileref.setAttribute("type", "text/css");
  63.         fileref.setAttribute("href", file);
  64.       }
  65.     }
  66.  
  67.     if (fileref!=""){
  68.       document.getElementsByTagName("head").item(0).appendChild(fileref)
  69.       loadedobjects+=file+" " //Remember this object as being already added to page
  70.     }
  71.   }
  72. }
  73.  
  74. </script>
  75.  
As you can probably telll from the header I got this code from http://www.dynamicdrive.com/dynamici...jaxcontent.htm


An alternitive method to use if the is only a small amount of data involved is to use PHP to write some Javascript that defines variables containing all the data (a 2 dimensional array of cities and names would do it). However I would only recomend doing this for a reasonable small amount of data.
Mar 7 '06 #2
aman
4 New Member
I have to create a 4drop down menus in starting only first drop down menu is having list rest three are blank then after i select first drop down value then next drop down menu have there corresponding values then third drop down will have there values then fourth ..
How will i do this i need in PHP or Javascript.
aman
Aug 28 '06 #3
aman
4 New Member
Dynamic Drop Down Menu
Aug 28 '06 #4
Banfa
9,065 Recognized Expert Moderator Expert
Well you only need PHP if the data is being sourced from a database or some other server based location (a file).

When I list selection is made you will either need to reload the page with the new information or have already got all the information in the page and only display the relevent options. This will require javascript.
Aug 28 '06 #5
RamseySkywalker
2 New Member
Hi,

I'm fairly new to PHP and I've created a dynamic drop down menu that populates itself with data from a MySQL table. What I would like to do is create a non dynamic drop down menu that alters what is shown in the dynamic menu.

For example, the first menu has three cities London, Paris, New York. If I choose London it populates the second menu with people from London.

Here is the code I have for my dynamic menu

Expand|Select|Wrap|Line Numbers
  1. <td valign=top><strong>Name:</strong></td>
  2.     <td>
  3.     <?php
  4.     echo "<select name=\"name\">"; 
  5.     echo "<option size =30 selected>Select</option>";
  6.     if(mysql_num_rows($sql_result)) 
  7.     { 
  8.     while($row = mysql_fetch_assoc($sql_result)) 
  9.     { 
  10.     echo "<option>$row[name]</option>"; 
  11.     } 
  12.  
  13.     } 
  14.     else {
  15.     echo "<option>No Names Present</option>";  
  16.     } 
  17.     ?>
  18.     </td>
  19.     </tr>    
  20.  
So basically, I need a standard menu, i.e:

Expand|Select|Wrap|Line Numbers
  1. <tr>
  2.     <td valign=top><strong>Sites:</strong></td>
  3.     <td> <select name="cities">
  4.     <option selected>Select</option>
  5.     <option>London</option>
  6.     <option>Paris</option>
  7.     <option>New York</option>
  8.     </select>
  9.     </tr>
  10.     </td>
  11.  
I think I need to hold the value of cities when selected in a variable and create an if statement with various sql query results to be run depending on what option is chosen.

Any help would be highly appreciated!

Thanks,
Mart


Hey Mart,

I've recently done the same thing. What ever you name your select box will the name of the POSTED value of the drop down. i.e.
<select name="city">
<option>Londo n</option>
<option>Paris </option>
<option>New York</option>
</select>

You would have a table for each city which would contain a list of users you would then submit that to some PHP code that would include something to the effect of ...

<?php $city=$_POST['city'];
//construct lookup statementins

$sql="select people from $city where city= '$city'";

$result=mysql_q uery($sql,$conn ) or die (mysql_error()) ;

if(mysql_num_ro ws($result)) {
while($row = mysql_fetch_row ($result))
{

print("<option value=\"$row[0]\">$row[0]</option>");
}
}
else {
print("<option value=\"\">No courses created yet</option>");
}

?>

That'll work of coruse you don't mind reloading the page. This also will not take care of duplicate names that you may have in the DB.
Jul 30 '07 #6
nathj
938 Recognized Expert Contributor
Hi,

It is possible to achieve exactly what uuo are after using a combination of HTML, PHP, MySQL and JavaScript.

First the data structure - as this will be vital.

From what I ahev read on this thread for this idea you need three tables:
1) The Cities
2) The people
3) The link/association between the two

This is a standarad structure for potential many-to many data relationships. It works perfectly and is properly normalised.

Second the HTML - this is just a form, the combo box of cities, which can be populated from the database, should have an onchange that calls a javascript.

Third the JavaScript - this uses XMLHTTPREQUEST to get the data from the database via the Fourth and final item - the PHP.

This structure works a treat, I'm using on a site that is testing at the moment.

Have a read of the PHP AJAX tutorial from W3Schools and have a go.

If you get stuck post the problem back here.

I hope this points you in the right direction. But it is definitley possible to have the page refresh as you are on it.

Cheers
nathj
Jul 30 '07 #7
erp23
26 New Member
Hi,

It is possible to achieve exactly what uuo are after using a combination of HTML, PHP, MySQL and JavaScript.

First the data structure - as this will be vital.

From what I ahev read on this thread for this idea you need three tables:
1) The Cities
2) The people
3) The link/association between the two

This is a standarad structure for potential many-to many data relationships. It works perfectly and is properly normalised.

Second the HTML - this is just a form, the combo box of cities, which can be populated from the database, should have an onchange that calls a javascript.

Third the JavaScript - this uses XMLHTTPREQUEST to get the data from the database via the Fourth and final item - the PHP.

This structure works a treat, I'm using on a site that is testing at the moment.

Have a read of the PHP AJAX tutorial from W3Schools and have a go.

If you get stuck post the problem back here.

I hope this points you in the right direction. But it is definitley possible to have the page refresh as you are on it.

Cheers
nathj
Thanks for this - I have had a similar problem in populating a second select menu from the first, where both lists are generated from mySQL. Your suggestion works in FF, Safari and Opera very well, but in IE the select menu which is meant to be repopulated remains blank.

I have tested the database, php page and js file, and all of these seem to work. The problem is in populating the <select> tag using the .innerHTML command (it repopulates a div tag without any problem). My code is as follows:

HTML file:
Expand|Select|Wrap|Line Numbers
  1. <select name="supervisor" id="supervisor">
  2.             <option value="none">please select...</option>
  3.           </select> 
  4.  
js command:
Expand|Select|Wrap|Line Numbers
  1. function stateChanged() 
  2. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  3.  { 
  4.  document.getElementById("supervisor").innerHTML=xmlHttp.responseText 
  5.  } 
  6. }
  7.  
php:
Expand|Select|Wrap|Line Numbers
  1. do { 
  2.  
  3.        echo  "<option value=\"";
  4.         echo $row_rs_subsup['firstName']; echo " "; echo $row_rs_subsup['lastName']; 
  5.         echo "\">"; echo $row_rs_subsup['firstName']; echo " "; echo $row_rs_subsup['lastName']; 
  6.         echo "</option>\n"; 
  7.  
  8.           }  while ($row_rs_subsup = mysql_fetch_assoc($rs_subsup));
  9.  
I have tried a number of ways to get around this (e.g. playing with the js command), but I haven't really got anywhere - any suggestions would be greatly appreciated.
Aug 2 '07 #8
nathj
938 Recognized Expert Contributor
If IE will re-populate a div tag no problem then how about doing that. So when the seonc dropdown is populated you actually re-create it?

It's a bit crazy I know but it should still work on the other browsers as well.

Cheers
nathj
Aug 3 '07 #9
erp23
26 New Member
Yes, it's now working, finally ending a long struggle - thanks a lot!!



The whole select menu is wrapped in a div tag, and with the php code amended as follows:

Expand|Select|Wrap|Line Numbers
  1. echo "<select name=\"supervisor\" id=\"supervisor\">\n";
  2. echo "<option value=\"none\">please select...</option>\n";
  3.  
  4. do { 
  5.  
  6.  
  7.       echo  "<option value=\"";
  8.         echo $row_rs_subsup['firstName']; echo " "; echo $row_rs_subsup['lastName']; 
  9.         echo "\">"; echo $row_rs_subsup['firstName']; echo " "; echo $row_rs_subsup['lastName']; 
  10.         echo "</option>\n"; 
  11.  
  12.           }  while ($row_rs_subsup = mysql_fetch_assoc($rs_subsup));
  13.  
  14.  echo "</select>"; 
Aug 3 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
3446
by: jorntk | last post by:
How can i make a drop down menu that are dynamiclly generated base on the value selected in another drop down menu? thanks nad regards Jorn
6
15426
by: Greg Scharlemann | last post by:
I am attempting to populate a drop down menu based on the selection of a different drop down menu. However, it is not working correctly, I cannot figure out for the life of me what exactly happens because I am not getting any errors on the page. <html> <script language="javascript"> var phaseArray = new phaseArray(4); var phaseTypeId = new phaseTypeId(4); var phaseId = new phaseId(4);
1
4308
by: tribal boy | last post by:
Guys, I am using a dynamic menu which uses xml,xsl a css file and javascript. This works fine when there are no server controls around or underneath it. The problem is whenever the menu encounters a server control, it closes itself.
3
3064
by: scaredemz | last post by:
hi, so i'm creating a dynamic drop-down menu. the menu and the text show up fine in IE but only the drop-down shows in Firefox without the menu text. Below is the fxn code. help pls. function DropDownHelper(menuArray, top, left, height) { var currItem = new String(); var item; var idStr;
6
8315
by: mcgrew.michael | last post by:
I hope this is the right group. I am very new to ASP so this is probably a stupid question. I have some vbscript that query's AD and populates a recordset. I know the recorset contains the information I want by doing a Response.write. I am having problems dynamically creating a drop down list from the data in the recordset. The drop down is created but it is empty. Any help would be greatly appreciated. A sample of the code: <% 'On...
4
9302
by: TycoonUK | last post by:
Hi, As I do not have IE7 on my computer, I was wondering if there is a fault in my CSS Menu when using IE7. Please can someone look at my site - http://www.worldofmonopoly.co.uk and tell me if it works, and if it does not, tell me why it does not work. Thanks.
1
3056
by: azeemqaiser | last post by:
Hi All, I have a form with two drop down controls. I want to change the values of Drop Down Menu 2 if i select some value in Drop Down Menu 1. Like If i select country then all the states of this country should get populated in the second menu. Will appreciate your help.
2
4344
by: vinceboy | last post by:
Hi anybody. I am newbie here and would like to know that how can I validate both drop down menu and radio button from a dynamic display form.Something went wrong with my script.The radio button is dynamic,however drop down menu is static. <script language="JavaScript" type="text/JavaScript"> function formSubmit(obj) { var returnStatus = 1; if (obj.rNumber.selectedIndex == 0 || !(movieList.time;?>].checked)) {
10
57052
by: mart2006 | last post by:
Hi, I'm fairly new to PHP and I've created a dynamic drop down menu that populates itself with data from a MySQL table. What I would like to do is create a non dynamic drop down menu that alters what is shown in the dynamic menu. For example, the first menu has three cities London, Paris, New York. If I choose London it populates the second menu with people from London. Here is the code I have for my dynamic menu <td...
0
9813
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10829
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10582
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6976
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5645
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4446
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4005
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3106
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.