1

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>

3 Answers 3

1

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:

<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 (array_key_exists('value', $_POST) && $_POST['value']) { 
    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>"; 
    }
}

?>

<input type="submit" value="Submit" />
</form>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

1

You are not closing your while loop's in the first if statement and else if statement. Missing the closing '}'

while($row=mysql_fetch_array($result)) {
   echo "<option value='".$row['name']."'>".$row['name']."</option>"; 
}
echo "</select>";

Also in the 'else' statement you want to move the '}' bracket to before the line

echo "</select>";

Comments

0

try this

<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.