Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7011983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:11:48+00:00 2026-05-27T22:11:48+00:00

I have been trying everything I can to get this script to work and

  • 0

I have been trying everything I can to get this script to work and I just can’t figure out what I’m doing wrong. I’m very new to PHP and Javascript and I am trying to modify a script I found on the internet to get cascading drop downs on my site. This is a fairly simple script that unfortunately doesn’t use AJAX so it requires a page refresh but if Im having a hard time with this I don’t think I should move on to AJAX quite yet. The problem I’m having is the script is supposed to make my selection from menu one stick when the page refreshes but it always resets back to SELECT ONE, the default. It works in the original script but Ive made quite a few modifications so I can’t figure out why mine is not sticking. The original script is:

<?php
//***************************************
// This is downloaded from www.plus2net.com //
/// You can distribute this code with the link to www.plus2net.com ///
//  Please don't  remove the link to www.plus2net.com ///
// This is for your learning only not for commercial use. ///////
//The author is not responsible for any type of loss or problem or damage on using this script.//
/// You can use it at your own risk. /////
//*****************************************

$dbservertype='mysql';
$servername='localhost';
// username and password to log onto db server
$dbusername='';
$dbpassword='';
// name of database
$dbname='dd';

////////////////////////////////////////
////// DONOT EDIT BELOW  /////////
///////////////////////////////////////
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
//////// 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>
<?

/*
If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special care.
To read more on register_global visit.
  http://www.plus2net.com/php_tutorial/register-globals.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;
}


//@$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("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=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"); 
}else{$quer=mysql_query("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>";
while($noticia2 = mysql_fetch_array($quer2)) { 
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>";
while($noticia = mysql_fetch_array($quer)) { 
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>";
?>
<center><a href='http://www.plus2net.com'>PHP SQL HTML free tutorials and scripts</a></center> 
</body>

</html>

And my script is:

<?php
$dbservertype='mysql';
$servername='localhost';
$dbusername='newuser';
$dbpassword='';
$dbname='supplydb';
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword){
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());}
?>
<!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.category.options[form.category.options.selectedIndex].text;
self.location='dd.php?cat=' + val ;
}
function reload2(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].text; 
var val2=form.subcat.options[form.subcat.options.selectedIndex].text; 
self.location='dd.php?cat=' + val + '&cat2=' + val2 ;
console.log(val2);
}
</script>
</head>
<body>
<?

//////  Grab Get after page refresh
$cat=$_GET['cat'];
$command="SELECT * FROM product_group WHERE type = 1";
$quer2 = mysql_query($command);
    if(isset($cat) and strlen($cat) > 1){
        $num = explode(' ',$cat);
        $number = $num[0];      
        $strLen = strlen( $number );
            for ( $i = 0; $i < $strLen; $i++ )
                {$arr[] = $number{$i};}
                $arr1 = $arr [0];
                $arr2 = $arr [1];
                $arr3 = $arr1.$arr2;                
$command1="SELECT * FROM `product_group` WHERE `group` LIKE '".$arr3."%' AND type = '2'";
$quer = mysql_query($command1);
        }       
echo "<form method=post name=category action='dd-check.php'>";

    /////////////   Create First Drop Down Box
    echo "<select name='category' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
        while($query = mysql_fetch_assoc($quer2)){
            if($query['group']==$cat){
                echo "<option selected value='$query[group]'>$query[group]</option>"."<BR>";}
                else {echo "<option value='$query[group]'>$query[group]</option>";}
                }
    echo "</select>";
    //////////////  Create Second Drop Down Box 
    echo "<select name='subcat' onchange=\"reload2(this.form)\"><option value=''>Select one</option>";
        while($query2 = mysql_fetch_array($quer)){      
        if($query2['group']==$cat2)
            {echo "<option selected value='$query2[group]'>$query2[group]</option>"."<BR>";}
        else {echo  "<option value='$query2[group]'>$query2[group]</option>";}
        }





    echo "</select>";

echo "</form>";
print_r ($cat);
?>
</body>
</html>

Any help in pointing out where my bonehead mistake is will be helpful. Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:11:50+00:00Added an answer on May 27, 2026 at 10:11 pm

    Figured it out. Sorry for the formatting problems. I’ll run all code through a formatter from now on. In the original code they are calling for an ID field that I didn’t even have setup in my database. Once I setup the field and corrected the code it worked perfectly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get the following code to work(everything is defined in
I have been trying to replace everything in xcode with stuff I can get
I have been trying to figure this little section of my larger project out
I have been trying to get around this error for a day now and
I have been trying to get SqlCacheDependency working. I think I have everything set
Basically this everything I have been trying to have session variable on same page
OK, I've been Googling for hours and trying everything and can't get anything to
I have been trying out some LINQ query can someone please show how to
I have been searching everywhere on the web and trying everything that I can
I've been trying to get this code to work for hours! All I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.