I’ve looked at various tutorials about drop down boxes and dynamic content but I’m still not getting anywhere.
If you take a look at: [URL=”http://myklwebs.com/PHPtests/demodropdownbox.php?”]http://myklwebs.com/PHPtests/demodropdownbox.php?
Here’s the code:
[code=html]
<?php
$host = “localhost”;
$user = “USERNAME”;
$pass = “PASSWORD”;
$db = “DATABASENAME”;
$r = mysql_connect($host, $user, $pass);
if (!$r) {
echo “Could not connect to servern”;
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo “Connection establishedn”;
}
$r2 = mysql_select_db($db);
if (!$r2) {
echo “Cannot select databasen”;
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo “Database selectedn”;
}
?>
<html>
<head>
<title>Demo Drop-down box</title>
<script language=JavaScript>
function reload(form)
{
var selected_value=form.man.options[form.man.options.selectedIndex].value;
self.location=’demodropdownbox.php?man=’ + selected_value ;
}
</script>
</head>
<body>
<?
$man=$HTTP_GET_VARS[‘man’];
if(strlen($man) > 0 and !is_numeric($man)){
echo “INVALID DATA.”;
exit;
}
//This fetches content from car MANUFACTURER table first to get the AUDI,BMW,PORSCHE, SAAB list
$manufacturer_query=mysql_query(“SELECT category, id FROM MANUFACTURER order by category”);
if(isset($man) and strlen($man) > 0)
{
echo “Loaded: Manufacturers.”;
// If the numeric value of “man” (selected manufacturer model) is greater than zero we are OK
// Then we will perform an SQL query to find all car models with that number.
$models_query=mysql_query(“SELECT MODELS FROM MODELS where id=$man order by MODELS”);
echo “Loaded: Models by Manufacturer ID.”;
}else
{$models_query=mysql_query(“SELECT MODELS FROM MODELS order by MODELS”);
echo “Loaded: Models.”;
}
echo “<form method=post name=f1 action=’demodropdownbox_ui.php’>”;
echo “<select name=’man’ onchange=”reload(this.form)”><option value=”>Select car manufacturer</option>”;
while($qresult_manufacturer = mysql_fetch_array($manufacturer_query))
{
if($qresult_manufacturer[‘id’]==@$man){echo “<option selected value=’$qresult_manufacturer[id]’>$qresult_manufacturer[category]</option>”.”<BR>”;}
else{echo “<option value=’$qresult_manufacturer[id]’>$qresult_manufacturer[category]</option>”;}
}
echo “</select>”;
echo “<select name=’mod’><option value=”>Select car model</option>”;
while($qresult_models = mysql_fetch_array($models_query))
{
echo “Loaded: Models queried.”;
echo “<option value=’$qresult_models[MODELS]’>$qresult_models[MODELS]</option>”;
}
echo “</select>”;
echo “<input type=submit value=Submit>”;
echo “</form>”;
?>
</body>
and
[code=html]
<?php
?>
<html>
<head>
<title>Demo Drop-Down Box</title>
</head>
<body>
<?
$man=$_POST[‘man’];
$mod=$_POST[‘mod’];
echo “Manufacturer is $man = $man <br>Model is $mod = $mod “;
?>
</body>
</html
I don’t know if I’m doing something stupid, or if the code is fundamentally wrong or what. I’m really struggling to make progress here!