I’m a newbie in php & i’m trying to load dropdown list from a database. From the below code only the elseloop is working. The if and elseif is not working.
I don’t know what’s the mistake in the loop.
<html>
<head>
</head>
<body>
<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
<select name="value">
<option value="1">CLUB/FEDERATION/LIGUE</option>
<option value="2">SPONSOR</option>
<option value="7">AGENCE CONSEIL</option>
</select>
<br>
<?php
$db = JFactory::getDBO();
if($_POST['value'] == '1') {
$query = "SELECT name FROM `fs01_metier` WHERE id_cat=1";
$result = mysql_query($query);
$result = mysql_query($query);
echo "<select name=category>";
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
echo "</select>";
}
elseif($_POST['value'] == '2') {
$query = "SELECT name FROM `fs01_metier` WHERE id_cat=2";
$result = mysql_query($query);
echo "<select name=category>";
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
echo "</select>";
}
else {
$query = "SELECT name FROM `fs01_metier` WHERE id_cat=7";
$result = mysql_query($query);
echo "<select name=category>";
while($row=mysql_fetch_array($result)) {
echo "<option value='".$row['name']."'>".$row['name']."</option>";
echo "</select>";
}
}
?>
</form>
</body>
</html>
There are a few problems with the script.
First, there is no submit button (before the
</form>). That allows the content to be returned to the server.Second, the ELSE will be executed even if there is no selection made on the first load of the page.
In each of the ifs, there seems to be missing a closing
}in the loop. And there is an extra one at the end.Here is what I would do: