Problem with drop down menu data

Drop down menu unable to post data selected on submit in php, the coding returns the category id number not the data in category after selection. I need the data to be posted in the next page not the id… how can i do that?

<?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.category.options[form.category.options.selectedIndex].value;
self.location='dd.php?category=' + val ;
}

</script>
</head>

<body>
<?Php

@$cat=$_GET['category']; // 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=check_page.php////
//////////        Starting of first drop downlist /////////
echo "<select name='category' 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='subcategory'><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 "<br>";

echo "<input type=submit value=Search>";
//for testing

echo "Value of \$cat1 = $cat";




echo "</form>";
?>
<br><br>
<a href=dd.php>Reset and start again</a>

</body>

</html>

just query the data on the next page by ID.

<off-topic>
Whyever are you still using HTML 3.2?
</off-topic>

2 Likes

Yes I just started my learning in to web page … so most of the contents from net… ::slight_smile:

If you’re going to learn from online tutorials, then you need to ensure you choose a reputable and up-to-date source.

You need to be learning HTML5. Try these for the basics:

https://www.htmldog.com/guides/html/

or

thank you let me try

I don’t think this

@$cat=$_GET['category']; // Use this 

and this

echo "<form method=post name=f1 action='dd-check.php'>";

will have the effect you desire, unless the reload() function is by-passing the normal form processing.

While you’re learning, don’t use @ to suppress error messages. The proper way to deal with whether or not the $_GET variable exists, IMO of course, is

if (isset($_GET['category'])) {

and work with the result.

Can you expand on that? In your first drop-down, your code assigns the cat_id as the value of each option. Is that not the information you want?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.