473,378 Members | 1,142 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Dynamic Drop Down Menu

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
Mar 6 '06 #1
10 56996
Banfa
9,065 Expert Mod 8TB
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
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
Dynamic Drop Down Menu
Aug 28 '06 #4
Banfa
9,065 Expert Mod 8TB
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
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>London</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_query($sql,$conn) or die (mysql_error());

if(mysql_num_rows($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 Expert 512MB
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
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 Expert 512MB
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
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
nathj
938 Expert 512MB
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>"; 
I'm glad it's got sorted.

All the best with the rest of the project.

Cheers
nathj
Aug 3 '07 #11

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

Similar topics

7
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
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...
1
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...
3
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...
19
by: mart2006 | last post by:
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....
6
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...
4
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...
1
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...
2
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.