I have some code I have been working on for a little while to make some processes a little more efficient. I am completely new to the PHP world, but do have some experience with VBA. I am trying to reference a second dropdown based on the selection from the first. I pulled the code I have from the net and have modified it to work (though it doesnt completely yet).
Background on the data. I have one table, with multiple columns. The only ones relevant are “Parent” and “Child” (temp names for posting on here). I would like all of the parents to appear in the first list, and when one is selected, the children that the parent owns. I do not care to see any children when a parent has not been selected (and removed the else statements regarding that). Currently I can see the Parents in their list, but when I select one, the page refreshes and name is tagged in the address bar. The Parent is reset back to the Original value and no change from the Child.
Any and all help is appreciated, and please go easy on me, I know how it is when a new members jump in looking for information.
Thanks
[COLOR=“Blue”]EDIT[/COLOR]
Decided to go a different path instead of merging everything into one table. I am using multiple tables which is how the original code worked. If some one still wants to tackle it, be my guest
<?php
$dbservertype='mysql';
$servername='localhost';
// username and password to log onto db server
$dbusername='root';
$dbpassword='';
// name of database
$dbname='databasey';
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> Selection</title>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.Parent.options[form.Parent.options.selectedIndex].value;
self.location='ddorig.php?cat=' + val ;
}
</script>
</head>
<body>
<?php
@$cat=$_GET['Parent']; // Use this line or below line if register_global is off
//Echo ['Parent'] ."<BR>";
if(strlen($cat) < 0 and !is_numeric($cat)){ //
echo "Data Error";
exit;
}
///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT Parent FROM tablex"); //// Find Unique Parents
///////////// 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){ ////If Parent is chosen, find applicable CHILD's
$quer = mysql_query("SELECT DISTINCT CHILD FROM tablex where Parent= $cat"); /// Find CHILD's based on inputted Parent
echo "SELECT DISTINCT CHILD FROM tablex where Parent= $cat"."<BR>";
echo "CHILD Query Selected";/// Tell me that the Query has been selected
}
else {$quer3 = mysql_query("SELECT DISTINCT CHILD FROM tablex where Parent= $cat");
echo "SELECT DISTINCT CHILD FROM tablex where Parent= $cat"."<BR>";
}
////////// end of query for second subcategory drop down list box ///////////////////////////
echo "<form method=post name=f1 action='dd-checkorig.php'>";
///form processing page address to action in above line////
////////// Starting of first drop downlist /////////
echo "<select name='Parent' onchange=\"reload(this.form)\"><option value=''>Parent</option>";
while($noticia2 = mysql_fetch_array($quer2))
///{
///if($noticia2['Parent']==@$cat){echo "<option selected value='$noticia[Parent]'>$noticia2[Parent]</option>"."<BR>";}
echo "<option value='$noticia2[Parent]'>$noticia2[Parent]</option>";
///"<option selected value='$noticia2[Parent]'>$noticia2[Parent]</option>"."<BR>";}
echo "</select>";
////////////////// This will end the first drop down list ///////////
////////// Starting of second drop downlist /////////
echo "<select name='CHILD'><option value=''>CHILD</option>";
while($noticia = mysql_fetch_array($quer)) {
echo "<option value='$noticia[CHILD]'>$noticia[CHILD]</option>";
}
echo "</select>"."<BR>";
////////////////// This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";
echo "</form>";
?>
</body>
</html>
<?php
?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>
<body>
<?
$cat=$_POST['Parent'];
$Child=$_POST['Child'];
echo "Value of \$cat = $cat <br>Value of \$Child = $Child";
?>
</body>
</html>