Navigation

    ask avan logo
    • Register
    • Login
    • Search
    • Categories
    • Unsolved
    • Solved

    How do I get the selected ID of the second dropbox based on the first dropbox in PHP with a refresh?

    PHP
    1
    1
    6
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Laurens van Oorschot
      Laurens van Oorschot last edited by

      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 second category dropbox will have the selected 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. Can anyone help?

      <?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);
              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()) {
      	  $resultofcar = ("<option value='{$row ['catid']}'> {$row['cat']}</option>");
            echo $resultofcar;
          }
        ?>
      
      </select>
      
      <input id='Ga' type='submit'  value='Ga'name='Ga' />
      Reply Quote 0
        1 Reply Last reply

      • First post
        Last post