/    Sign up×
Community /Pin to ProfileBookmark

Still Can’t Do Drop Down Boxes

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?[/URL] you’ll see a pair of drop downs and I’m sure you can see what is supposed to happen. Unfortunately, selecting something from the first box doesn’t leave it selected for long and it doesn’t populate the second box as it’s already populated before selecting anything.

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>
[/code]

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
[/code]

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!

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @Mykl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...