Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How do I get the selected ID for the second dropbox based on the first dropbox in PHP with a refresh?

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 1.15k
    Answer it

    I am building an editor for a blog. Each post will be categorised.

    When the user clicks on a post title from the first dropdown menu and presses Ga (go in Dutch), then the page reloads with the second category dropbox selected to the category associated with it, and display all the other categories in the same dropbox.

    Having all the categories in the second dropbox will allow the user to change the category of article. I am able to get both dropbox populated, but not getting the selection part working. 

    I think that the (isset($_POST['Ga'])) is missing something, but I don't know.

    Any suggestions?

    
      <?php
       if (isset($_POST['Ga'])) {
       $db = new mysqli("dbhost", "username", "password", "dbname"); //set your database handler
        $query = "SELECT c.catid , a.titel , c.cat FROM artikelen a JOIN Categorie c ON c.catid = a.catid where c.catid = a.catid";
        $result = $db->query($query);
         
        while ($row = $result->fetch_assoc()) {
        
         if ($row ['a.catid'] = ['c.catid']) {
          $selected = ($row['catid'] == $result);
         $resultofcat = echo "<option value=\"".$row["catid"]."\" ".($selected ? " selected=\"selected\"":"").">".$row["cat"]."</option>";   
         }
        }
       }
      ?>
       
      <?php  
       $link = mysqli_connect("dbhost", "username", "password", "dbname");
       $db = new mysqli("dbhost", "username", "password", "dbname");//set your database handler
       $query = "SELECT c.catid , a.titel , c.cat FROM artikelen a JOIN Categorie c ON c.catid = a.catid";
       $result = $db->query($query);
     
       echo "<form action='test.php' method='post' enctype='multipart/form-data'>";
    
      ?>   
       
      <select id='subcatsSelect'>
        
      <?php
       // $query = "SELECT catid, cat FROM Categorie";
       $result = $db->query($query);
     
       while ($row = $result->fetch_assoc()) {
        // $subcats[$row['catid']][] = array("catid" => $row['catid'], "val" => $row['cat']); 
        echo "<option value='catid'> {$row['titel']}</option>";
       }
        
      ?>
     
      </select>
       
      <select id='categoriesSelect'>
     
       <?php  
        $result = $db->query($query);
        while ($row = $result->fetch_assoc()) {
        $resultofcat = ("<option value='{$row ['catid']}'> {$row['cat']}</option>");
         echo $resultofcar;
        }
       ?>
    
      </select>
    
      <input id='Ga' type='submit' value='Ga'name='Ga' />   

     

 1 Answer(s)

  • Hi Laurens,
    It seems like there are lot of issues in your example, I am not sure if the same code is working fine in your environment.
    Anyway I am not going to point out all these errors and just trying to make your dropbox selection working. So for making selection part working, you have to make following changes:
    1: Add name attributes to your dropboxes (select box)
    <select id='subcatsSelect' name="subcatsSelect">
    <select id='categoriesSelect' name="categoriesSelect">

    2: Within your "if (isset($_POST['Ga'])) " condition, change the "if" condition and instead of echoing OPTION value, just store it's id:

    if ($row ['catid'] == $_POST['subcatsSelect']) {
          $selectedId = $row['catid'];
     }

    3: Replace your "while" condition of second select box with this:

    while ($row = $result->fetch_assoc()) {
        if ($selectedId == $row['catid']) {
            $resultofcat = "<option value='{$row['catid']}' selected='selected'> {$row['cat']}</option>";
        } else {
            $resultofcat = "<option value='{$row['catid']}'> {$row['cat']}</option>";
        }
         echo $resultofcat;
        }

    Also try to improve your database connection in better way. I hope this will help.
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: