Jump to content
Updating IPB tonight Read more... ×
shams

Two dropdown menu in php?

Recommended Posts

Posted (edited)

My site will have nearly 2000 html pages, they are grouped in 114 categories each category have many pages, i want to have two dropdown menu in first menu select the category and in the second menu the page to open,  i got this code from thesitewizard.com it is working just for one dropdown menu:


 
<form action="chgoto.php" method="get">
<select name="url" onchange="menu_goto(this.form)">
<option value="001_001.html">page 1</option>
<option value="001_003.html">page 3</option>
<option value="001_005.html">page 5</option>

</select>
<input type="submit" value="Go!" />

 

this is chgoto.php:

<?php

$baseurl = "/" ;
$url    = $_REQUEST['url'] ;

header( "Location: " . $baseurl . $url );
exit ;

?>

 

 

Edited by shams
inserting codes tags

Share this post


Link to post
Share on other sites

What is your database structure to control the grouping of pages within category?

Share this post


Link to post
Share on other sites

I didn't created any database for it, guide me to a link i can do, or if can be possible without any database?

Share this post


Link to post
Share on other sites

What is wrong with the code you posted already?  It has only one dropdown and that form and the php in the second block seems to handle the input (despite using the wrong global array to grab the input) correctly.  So - what is the problem?  Are you simply looking for us to give you the code to build the sub-menus with?

What is the JS function supposed to do for you?  Is that where you want the sub menu to be shown and get the user's final choice from?  Sounds like a use for an ajax call to take the main menu choice and retrieve the sub-menu choices via a php script which returns a json value of those sub-menu choices from which your js code can then build the second drop down with.  Is that what you are looking for?  Have you tried googling for "ajax call to get sub-menu"?

When you use a GET form, you should use the $_GET array to retrieve the expected input(s).  When you use a POST form, use the $_POST array for the inputs.  This way you cut down on hackers abilities in a fashion.

Share this post


Link to post
Share on other sites

If you don't have a database, what do you have to map pages to category?

Share this post


Link to post
Share on other sites
Quote

My site will have nearly 2000 html pages, they are grouped in 114 categories each category have many pages,

🚩Red Flag. You need to create a dynamic site. If you do not know how to code, there are may free CMS's available. Wordpress, Drupal, Joomla...

To actually create and maintain thousands of static html pages is just asking for a maintenance nightmare. You cant just copy/paste code from some help site. Your options are learn to code, pay someone to do it, or use one of the CMS's available.

Share this post


Link to post
Share on other sites

Take the Profile pages on this site, for example. Do you think someone has created  separate html pages for each of the 173,000 members?

Share this post


Link to post
Share on other sites

Thanks to all, i got the  multiple drop down list box code from the plus2net.com, arranged  my html pages in a mysql database in table category with columns category id and category name and second table subcategory  with category id and their html pages,  i select the category from first drop down menu and then it's  html page from second drop down menu it prints the category id and it's html page name:

Value of $cat = 2
Value of $subcat = 002_001.html 

how to pass this value to other php code to open this html page?

Share this post


Link to post
Share on other sites

this is code prints the  values:

<?php
require 'config.php';  // Database connection
//////// End of connecting to database ////////
?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Multiple drop down list box from plus2net</title>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val ;
}

</script>
</head>

<body>
<?Php

@$cat=$_GET['cat']; // Use this line or below line if register_global is off
if(strlen($cat) > 0 and !is_numeric($cat)){ // to check if $cat is numeric data or not. 
echo "Data Error";
exit;
}



///////// Getting the data from Mysql table for first list box//////////
$quer2="SELECT DISTINCT category,cat_id FROM category order by category"; 
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
if(isset($cat) and strlen($cat) > 0){
$quer="SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"; 
}else{$quer="SELECT DISTINCT subcategory FROM subcategory order by subcategory"; } 
////////// end of query for second subcategory drop down list box ///////////////////////////

echo "<form method=post name=f1 action='dd-check.php'>";
/// Add your form processing page address to action in above line. Example  action=dd-check.php////
//////////        Starting of first drop downlist /////////
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
foreach ($dbo->query($quer2) as $noticia2) {
if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo  "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
//////////////////  This will end the first drop down list ///////////

//////////        Starting of second drop downlist /////////
echo "<select name='subcat'><option value=''>Select one</option>";
foreach ($dbo->query($quer) as $noticia) {
echo  "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";
//////////////////  This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";
echo "</form>";
?>
<br><br>
<a href=dd.php>Reset and start again</a>
<br><br>
<center><a href='http://www.plus2net.com' rel="nofollow">PHP SQL HTML free tutorials and scripts</a></center> 
</body>

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.