-3

I'm new in programming and I'm stuck with dropdown lists.

I have to display 2 dropdown lists. In first one I have to display all categories from one table. In second one I have to display values from another table depends of the selected value in first dropdown list.
I found this answer, but I don't know how can I get selected value and use it for second dropdown list. I tried with $_POST, but it doesn't work.

Can someone give me any instructions how to do that? Thank you.

<?php
... 
$conn = new mysqli($hostname, $username, $password, $databaseName)  
or die ('Cannot connect to db');

$kat = $conn->query("select idkategorija, kategorija from kategorija");

echo "<html>";
echo "<body>";
echo "<select name='izb_kategorija' method = 'post'>";

while ($row = $kat->fetch_assoc()) {

    unset($idkategorija, $kategorija);
    $idkategorija = $row['idkategorija'];
    $kategorija = $row['kategorija']; 
    echo '<option value="'.$idkategorija.'">'.$kategorija.'</option>';                   
}
echo "</select>";

echo $idkategorija; // I want to use idkategorija in my next query for second dropdown list 

echo "</body>";
echo "</html>";
?> 
Community
  • 1
  • 1
Nami
  • 1
  • 7
  • 1
    Could you provide any example code you have made? – undefined Jun 10 '16 at 08:59
  • 1
    Welcome to SO. Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) And [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) SO is **not a free coding or code conversion or tutorial or library finding service** You also have to show that you have made some effort to solve your own problem. – RiggsFolly Jun 10 '16 at 09:02
  • 1
    We cannot give you suggestions unless you have provided your code – codeSeven Jun 10 '16 at 09:02
  • When you do your mysql query, return the results inside the drop down list. – krisph Jun 10 '16 at 09:03
  • You have to use Ajax to achieve that. Generate first drop-down from database and after selecting the first generate second drop-down depending on selection in second drop-down using ajax – S. M. Shahinul Islam Jun 10 '16 at 09:04
  • Code added. I hope it's possible without Ajax... – Nami Jun 10 '16 at 09:41
  • why would you hope that? – S. M. Shahinul Islam Jun 10 '16 at 09:42
  • @Alvi_1987: Because I've never worked with Ajax before and I'm not sure how much time I have to finish my work. I'll try, thank you! Others: Many thanks for all downvotes, this really helped me a lot... – Nami Jun 10 '16 at 10:01

1 Answers1

0

I modified this code for you.Use this code,i hope it will work for you.

<?php
...
$conn = new mysqli($hostname, $username, $password, $databaseName);
or die ('Cannot connect to db');
$kat = $conn->query("select idkategorija, kategorija from kategorija");
?>
<html>
<body>
<select name='izb_kategorija' method = 'post'>
<?php
while ($row = $kat->fetch_assoc()) 
{
  $idkategorija = $row['idkategorija'];
  $kategorija = $row['kategorija'];
?>
<option value="<?php echo $idkategorija; ?>"><?php echo $kategorija; ?></option>
<?php
}
?>
</select>
</body>
</html>
  • thank you. But this code does the same as the one in my first post, maybe it's easier to read/understand. I suppose I really should use Ajax for second dropdown list.. – Nami Jun 10 '16 at 11:05