Multiple Drop Down

This is a discussion on "Multiple Drop Down" within the PHP Forum section. This forum, and the thread "Multiple Drop Down are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Nov 24th, 2005, 13:39
New Member
Join Date: Nov 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Multiple Drop Down

Hello I have created a small php file which is going to be a module in my mambo driven intranet site. The problem I am having is having one of the drop downs do two things on 'onChange'
1) Change the href of the page/content
2) Update the second drop down based on the selection chosen
here is me code...
Code: Select all
<html>
<script language="javascript" type="text/javascript">
function change() {
document.location.href='index.php?option=content&task=category&sectionid=6&id=' + document.form1.options[selectedIndex].value
+ '&Itemid= '+document.form1.options[selectedIndex].id+'';
}
</script>
<body>
<?PHP
$db = mysql_connect();
mysql_select_db("mambo",$db);
$query = 'SELECT id, componentid, name FROM `mos_menu` WHERE parent =53';
$areas = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($areas);
?>
<form name="form1" id="form1" method="post" action="">
<select name="area" id="area" onchange="javascript:document.form1.submit();">
<option value="" selected="selected">Select an area...</option>
<?php
for ($i=0; $i < $num_rows; $i++)
{
$row_area = mysql_fetch_array($areas);
?>
<option value="<?php echo $row_area['componentid'] ?>" id="<?php echo $row_area['id'] ?>"><?php echo $row_area['name'] ?></option>
<?PHP
}//for
?>
</select>
</form>
<br><br>
<select onchange="document.location.href='index.php?option=content&task=view&id=' + this.options[selectedIndex].value + ' &Itemid= '+this.options[selectedIndex].id+'';">
<option selected="selected">Select a document...</option>
<?PHP
// Quick hack to swap around the id and component id for href
$doc_area = $_POST['area'];
$query_area = "SELECT id, name FROM `mos_menu` WHERE parent =53 AND componentid='$doc_area'";
$id_change = mysql_query($query_area) or die(mysql_error());
$row = mysql_fetch_assoc($id_change);
$cat_id = $row['id'];
// Retrieve area id and find all documents related to the selected area
$query_doc = "SELECT id, title, catid FROM `mos_content` WHERE catid ='$doc_area'";
$area_docs = mysql_query($query_doc) or die(mysql_error());
$doc_rows = mysql_num_rows($area_docs);
for ($i=0; $i < $doc_rows; $i++)
{
$row_docs = mysql_fetch_array($area_docs);
?>
<option value="<?php echo $row_docs['id'] ?>" id="<?php echo "$cat_id" ?>"><?php echo $row_docs['title'] ?></option>
<?PHP
}//for
?>
</select>
<?PHP
$temp = $_POST['area'];
if ($temp != "")
{
echo "<script language='javascript' type='text/javascript'>change();</script>";
}
?>
</body>
</html>
As you can see, i know onchange cant do two things so I have written a Javascript function which recognizes when something has been posted and it needs to change the href...problem only is it doesnt work!!
Any help/input would be much appreciated.
Thanks Guys/Gals

Last edited by benbacardi; Nov 24th, 2005 at 16:19. Reason: Code Tags
Reply With Quote

  #2 (permalink)  
Old Nov 25th, 2005, 16:16
New Member
Join Date: Nov 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Multiple Drop Down

Because a lack of response I tried to look at it myself and decided session variables were the way forward. Now it all works but the second drop down box does not populate dynamically according to the first.

Again any ideas/help will be welcomed

PHP: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>
<?PHP
    
//connection to the db stuff
    
$db mysql_connect("HIDDEN FOR FORUM PURPOSES");
    
mysql_select_db("mambo",$db);
    
    
//query to fetch areas and get the number of rows 
    
$query 'SELECT id, componentid, name FROM `mos_menu` WHERE parent =53';
    
$areas mysql_query($query) or die(mysql_error());
    
$num_rows mysql_num_rows($areas);
    if(!empty(
$_POST['area']) AND ($_POST['area'] != "Select an area..."))
    {
        
//register sessions
        
session_register('area');
        
$_SESSION['area'] = $_POST['area'];
        
//query for the second select box (docs)
        
$queryd "SELECT id, title, catid FROM `mos_content` WHERE catid ='" $_SESSION['area'] . "'";
        
$areasd mysql_query($queryd) or die(mysql_error());
        
$num_rowsd mysql_num_rows($areasd);
        if((!empty(
$_POST['area'])) AND ($_POST['area'] != "Select a document..."))
        {
            
session_register('aread');
            
$_SESSION['aread'] = $_POST['aread'];
echo 
"<script language='javascript' type='text/javascript'>document.location.href='index.php?option=content&task=view&id=' + this.options[selectedIndex].value + ' &Itemid= '+this.options[selectedIndex].id+'';'</script>";
        }
        
//do some re-arranging business
        
$queryN 'SELECT id, name FROM `mos_menu` WHERE parent=53 AND componentid=' $_SESSION['area'];
        
$resultN mysql_query($queryN) or die(mysql_error());
        
$name mysql_result($resultN,0,"name");
        
$id mysql_result($resultN,0,"id");
    }
?>
<body>
<form name="form1" method="post" action="">
    <SELECT name="area" onchange="javascript:document.form1.submit()">
      <?PHP
        
//has the user posted anything?
        
if(!empty($_SESSION['area']))
        {
            echo (
"<option value= " $_SESSION['area'] . ">" $name "</option>");
            for (
$i=0$i $num_rows$i++)
            {
                
$row_area mysql_fetch_array($areas);
echo (
"<option value=" $row_area['componentid'] . " id=" $row_area['id'] . ">" $row_area['name'] . "</option>");
            }
        }
        
//if not get a list of all the locations
        
else
        {
            echo (
"<option>Select an area...</option>");
            for (
$i=0$i $num_rows$i++)
            {
                
$row_area mysql_fetch_array($areas);
echo (
"<option value=" $row_area['componentid'] . " id=" $row_area['id'] . ">" $row_area['name'] . "</option>");
            }
        }
    
?>
    </SELECT>
    <BR>
<SELECT name="aread" onchange="document.location.href='../index.php?option=content&task=view&id=' + this.options[selectedIndex].value + ' &Itemid= '+this.options[selectedIndex].id+'';">
       <?PHP
        
//has the user posted anything?
        
if(!empty($_SESSION['aread']))
        {
            echo (
"<option value>Please Select a document...</option>");
            for (
$i=0$i $num_rowsd$i++)
            {
                
$row_aread mysql_fetch_array($areasd);
                echo (
"<option value=" $row_aread['id'] . " id=" $id ">" $row_aread['title'] . "</option>");
            }
        }
        
//if not get a list of all the documents
        
else
        {
            echo (
"<option value>Select a document Now...</option>");
            for (
$i=0$i $num_rowsd$i++)
            {
                
$row_aread mysql_fetch_array($areasd);
                echo (
"<option value=" $row_aread['id'] . " id=" $id ">" $row_aread['title'] . "</option>");
            }
        }
    
?>
    </SELECT>
</form>
</body>
</html>
Cheers

Steve
Reply With Quote
  #3 (permalink)  
Old Nov 25th, 2005, 17:52
Highly Reputable Member
Join Date: May 2005
Location: U.K
Age: 21
Posts: 739
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to benbramz
Re: Multiple Drop Down

dont know why nobodyhas replied....
anyway:

Seeing as you want to use php to populate these options from the database, you would unfortunatly ave to refresh, unless you used javascript to store all the options for the second box, which isnt to smart, as you can switch javascript off.

the way i would go about it is in the first dropdown, use the value to send the data and get the options for the second box....

<option value="countrys">Countrys</option>

then use sql to select all from the database that is under countrys, then loop the options for the next drop down...if that made sense.
unfortunatly there is no easy way of doing this dynamicly...
Reply With Quote
Reply

Tags
multiple, drop, down

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple drop-down menus ktsirig JavaScript Forum 2 May 28th, 2008 22:54
multiple rollover images change multiple images joshlindem Web Page Design 4 Apr 18th, 2008 09:11
Multiple drop down menus D4CUE Web Page Design 3 Mar 6th, 2008 08:06
Drop down menus drop behind flash header theGAME71135 Flash & Multimedia Forum 3 Jan 10th, 2008 09:42
Multiple Drop Down Validation Monie JavaScript Forum 1 Nov 6th, 2004 07:41


All times are GMT. The time now is 05:45.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43