12 Replies - 610 Views - Last Post: 22 May 2020 - 05:12 AM Rate Topic: -----

#1 laurensvo92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-January 20

How do I get the selected value of the second dropdown from the first

Posted 21 May 2020 - 09:26 AM

I want to get the first dropdown menu to get the selected value for the selected dropbox, but I would like to to have all the categories still being in the second dropbox, so I can change the category of the article.

For example, the user chooses a article in the first dropbox, then the category that is associated with that article gets selected, but the user can change the category in the dropbox. Can this be done?

I have tried using Javascript from a previous question , but it's only getting the selected category associated with that article and not all the other categories. What should I do? Does anyone know how this can be done?

    	  
    <?php 	$link = mysqli_connect("host", "username", "password", "lvo92");
    
      $db = new mysqli("host", "someusername", "somepassword", "lvo92");//set your database handler
      $query = "SELECT catid, titel FROM artikelen";
      $result = $db->query($query);
    
      while($row = $result->fetch_assoc()){
        $categories[] = array("catid" => $row['catid'], "val" => $row['titel']);
      }
    
      $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']);
      }
    
      $jsonCats = json_encode($categories);
      $jsonSubCats = json_encode($subcats);
    
    
    ?>			
    	
    	 <script type='text/javascript'>
          <?php
            echo "var categories = $jsonCats; \n";
            echo "var subcats = $jsonSubCats; \n";
          ?>
          function loadCategories(){
            var select = document.getElementById("categoriesSelect");
            select.onchange = updateSubCats;
            for(var i = 0; i < categories.length; i++){
              select.options[i] = new Option(categories[i].val,categories[i].catid);          
            }
          }
       function updateSubCats(){
        var catSelect = this;
        var catid = this.value;
        var testSelectionArray = new Array();
    
    for(var j = 0; j < this.options.length; j++) {
          if (this.options[j].selected) {
               testSelectionArray.push(this.options[j].value); 
           }
    }
    
        var subcatSelect = document.getElementById("subcatsSelect");
        subcatSelect.options.length = 0; //delete all options if any present
        var k=0;
        for(var i = 0; i < testSelectionArray.length; i++){
          catid = testSelectionArray[i];
            for(var j = 0; j < subcats[catid].length; j++){
                subcatSelect.options[k++] = new Option(subcats[catid][j].val,subcats[catid][j].catid);
            }        
        }
      }
        </script>
    	
    	 <body onload='loadCategories()'>
        <select id='categoriesSelect'>
        </select>
    
        <select id='subcatsSelect'>
    	    </select> 


Is This A Good Question/Topic? 0
  • +

Replies To: How do I get the selected value of the second dropdown from the first

#2 DarenR   User is offline

  • D.I.C Lover

Reputation: 705
  • View blog
  • Posts: 4,563
  • Joined: 12-January 10

Re: How do I get the selected value of the second dropdown from the first

Posted 21 May 2020 - 09:33 AM

i am not understanding the question.

are you saying that after the initial selection is made and the 2nd drop down is changed that you want to be able to select the 1st drop down again and have the second drop down change with it?


like
box 1 select a --> box 2 show a
box 1 select b --> box 2 show b


Was This Post Helpful? 0
  • +
  • -

#3 laurensvo92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-January 20

Re: How do I get the selected value of the second dropdown from the first

Posted 21 May 2020 - 10:09 AM

View PostDarenR, on 21 May 2020 - 09:33 AM, said:

i am not understanding the question.

are you saying that after the initial selection is made and the 2nd drop down is changed that you want to be able to select the 1st drop down again and have the second drop down change with it?


like
box 1 select a --> box 2 show a
box 1 select b --> box 2 show b



I am building an editor for a blog. Each post will be categorised. I am trying to get all the posts, with the category that they have been assigned to be displayed as dropdown menu. So, when the user clicks on a post title from the first dropdown menu, then the second category dropbox will have the selected category associated with it, and display all the other categories in the same dropbox. So. user can change the category of article. Does that help?
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2751
  • View blog
  • Posts: 8,056
  • Joined: 15-January 14

Re: How do I get the selected value of the second dropdown from the first

Posted 21 May 2020 - 10:18 AM

Then you need to change the options in the second dropdown at all, you only need to change which option is selected. Have it start with all of the categories and when you select an article just change which category is selected in the second dropdown.
Was This Post Helpful? 0
  • +
  • -

#5 laurensvo92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-January 20

Re: How do I get the selected value of the second dropdown from the first

Posted 21 May 2020 - 10:51 AM

In the code, it does display select the category associated with the post. However, it doesn't contain the other categories, so that the category can be changed. Initially, I thought that this could be done via button and then the page would refresh with the second dropbox displaying the category, but I read that javascript is required. I don't know a-lot about javascript.
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2751
  • View blog
  • Posts: 8,056
  • Joined: 15-January 14

Re: How do I get the selected value of the second dropdown from the first

Posted 21 May 2020 - 11:15 AM

Like I said, put all of the categories in the dropdown to start with, when the page loads, you can do that with PHP, and then the only thing you need to do when the first dropdown is selected is to find the matching category in the second dropdown and select it. You don't need to add and remove options from the second one, you only need to find and select an option.
Was This Post Helpful? 0
  • +
  • -

#7 laurensvo92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-January 20

Re: How do I get the selected value of the second dropdown from the first

Posted 21 May 2020 - 11:24 AM

So, I manage to populate all the categories for the categories field managed to figure out the matching part. How do I tell PHP to select the dropdown menu for the categories (second dropbox (categoriesSelect) based on the match?


				if(isset($_POST['Ga'])) {
					  $db = new mysqli("160.153.129.204", "someusername", "somepassword", "lvo92");//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);
		  while($row = $result->fetch_assoc()){
		
if($row ['a.catid'] = ['c.catid'])
   {
	
	
	
	
}}}
?>
	
	
	
	
	
	
	
	


	
			
	
	
	  
<?php 	$link = mysqli_connect("160.153.129.204", "someusername", "somepassword", "lvo92");

  $db = new mysqli("160.153.129.204", "someusername", "somepassword", "lvo92");//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>

    <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);
	
	
  }

?>
		 <input id='Ga' type='submit'  value='Ga'name='Ga' />
	

Was This Post Helpful? 0
  • +
  • -

#8 DarenR   User is offline

  • D.I.C Lover

Reputation: 705
  • View blog
  • Posts: 4,563
  • Joined: 12-January 10

Re: How do I get the selected value of the second dropdown from the first

Posted 21 May 2020 - 11:35 AM

add a where clause to your second drop down query where it will match the 1st drop down selected item value
Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2751
  • View blog
  • Posts: 8,056
  • Joined: 15-January 14

Re: How do I get the selected value of the second dropdown from the first

Posted 21 May 2020 - 12:24 PM

If you want to do it without refreshing the page you still need to use Javascript to get the option selected in the first dropdown, figure out what the matching category is, and find and select the option in the second dropdown. If you use PHP for that you need to refresh the page after they select the first one.
Was This Post Helpful? 0
  • +
  • -

#10 Ornstein   User is offline

  • D.I.C Head

Reputation: 105
  • View blog
  • Posts: 216
  • Joined: 13-May 15

Re: How do I get the selected value of the second dropdown from the first

Posted 22 May 2020 - 02:32 AM

Are we not going to address the fact that the username and password appear to have been left in the original code? :P
Was This Post Helpful? 0
  • +
  • -

#11 laurensvo92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 12-January 20

Re: How do I get the selected value of the second dropdown from the first

Posted 22 May 2020 - 02:43 AM

I know forgot to remove it, but I can't edit the post for some reason.
Was This Post Helpful? 0
  • +
  • -

#12 Ornstein   User is offline

  • D.I.C Head

Reputation: 105
  • View blog
  • Posts: 216
  • Joined: 13-May 15

Re: How do I get the selected value of the second dropdown from the first

Posted 22 May 2020 - 02:59 AM

Maybe a mod could edit it out. You may also want to change those credentials if you use them elsewhere and/or if your database allows remote connection.
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: How do I get the selected value of the second dropdown from the first

Posted 22 May 2020 - 05:12 AM

I have removed the usernames and passwords. Please note that the Report button can be used to make such a request.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1