many elements of a listbox to another

Your form needs an action to submit to itself:

     <form name="form" id="form" action="<?php echo echo htmlspecialchars($_SERVER["REQUEST_URI"]); ?>" method="post"> 

You could add an onChange(); event to your select1 dropdown

     <select name="combo1" multiple size="8" onChange="submit_this();">

and have a JavaScript script in your <head>

     <script language="javascript"> function submit_this(){ this.form.submit();}</script>

Then you would have to use php to collect the values from the submitted page

      $combo1 =  mysqli_real_escape_string( $_POST['combo1'] );

and then you could return the page with the second dropdown, constructed in much the same way as you made the first one, using the data you get back from the first one to set up the second one. You could wrap the first dropdown in an isset():

        if( !isset( $combo1 ) ){
        // make your first select boxes 
        }

and the second one in

        if( isset( $combo1 ) ){
        // make your second select boxes if combo1 returned a value
        }

so that they will only appear when needed.

As @NanaPartykar says, mysqli is the only way to go.

It will be dependent on JavaScript, so, if you prefer, you could have a submit button instead of the JavaScript function:

     <input type="submit" name="submit" value="Pick Sizes" />

This looks a handy referenc e for how to use select boxes: PHP code to get selected text of a combo box

I hope this will give you an idea of how it could be done – the SESSION variables remain to be properly defined in the loop for the second dropdown but they will probably come directly from your database at that point rather than from the session array used for demo purposes.

All values are set as numbers;

intval(); converts whatever is submitted to an integer to clean it up.

Then you can get back the corresponding value of what was submitted from the session variable you set from your database originally.

That way you will only be getting a single number value from your dropdowns which you can use to look up to get the answer you want

  <?php session_start(); ?> 
  <!DOCTYPE html>
  <html lang = "es">
  <head>
  <title>PRECEPTORÍA</title>
  </head>
    <body>
    <form action="https://stackoverflow.com/questions/34361452/<?php echo htmlspecialchars($_SERVER["PHP_SELF']) ?>" method="post">
    <?php
    $main_opt = array();
    $_SESSION['opt_vals'] = array();
    $main_opt[0] = "Select Item";      
    $main_opt[1] = "Ice Cream";
    $main_opt[2] = "Trousers";
    $main_opt[3] = "Tee Shirts";
    $_SESSION['opt_vals'][1][0] = "Select Flavour";
    $_SESSION['opt_vals'][1][1] = "Vanilla";
    $_SESSION['opt_vals'][1][2] = "Chocolate";
    $_SESSION['opt_vals'][1][3] = "Strawberry";
    $_SESSION['opt_vals'][1][4] = "Tuttifrutti";
    $_SESSION['opt_vals'][2][0] = "Select Colour";
    $_SESSION['opt_vals'][2][1] = "Black";
    $_SESSION['opt_vals'][2][2] = "Red";
    $_SESSION['opt_vals'][2][3] = "Green";
    $_SESSION['opt_vals'][2][4] = "Blue";
    $_SESSION['opt_vals'][3][0] = "Select Size";
    $_SESSION['opt_vals'][3][1] = "Small";
    $_SESSION['opt_vals'][3][2] = "Medium";
    $_SESSION['opt_vals'][3][3] = "Large";
    $_SESSION['opt_vals'][3][4] = "X Large";
    $i = 0;
    if(!isset($_POST['combo1']) && !isset($_POST['combo2'])){
        echo '<select name="combo1" onChange="submit();">' . "\n";
        //while ($fila = mysqli_fetch_row($consulta)){
        while($i < count($main_opt)){
        echo "<option value="".$i."">".$main_opt[$i]."</option>\n";       
             $o = 0;
             // load your sub options array here
             if(!$_SESSION['opt_vals'][$i]) $_SESSION['opt_vals'][$i] = array();
             while($i < count($_SESSION['opt_vals'][$i])){
             $_SESSION['opt_vals'][$i] = $o;
             if(!$_SESSION['opt_vals'][$i]) $_SESSION['opt_vals'][$i] = array();
             $_SESSION['opt_vals'][$i][$o] = $_SESSION['opt_vals'][$i][$o];
             $o++;
             }
         $i++;
         }
     echo '</select>'  . "\n";    
     }else{
         if(intval($_POST['combo1']) >= 1) $_SESSION['combo1_val'] = $combo1_val =intval($_POST['combo1']);
         if(intval($_POST['combo1']) >=1){
             $i = 0;
             echo '<select name="combo2" onChange="submit();">' . "\n";
             while($i < count($_SESSION['opt_vals'][$combo1_val])){
             echo "<option value="".$i."">".$_SESSION['opt_vals'][$combo1_val][$i]."</option>\n";
             $i++;
             }
         }
     echo '</select>'  . "\n";
     }
     if(intval($_POST['combo2']) >= 1) $_SESSION['combo2_val'] = $combo2_val =intval($_POST['combo2']);
     if($_SESSION['combo1_val'] >=1 && $_SESSION['combo2_val'] >=1){
     // use the result to do  whatever you want
     echo 'Thank you! You have selected ' . $_SESSION['opt_vals'][$_SESSION['combo1_val']][$_SESSION['combo2_val']] . " " . $main_opt[$_SESSION['combo1_val']] . '.';
     }    
     // Do any mysqli adjustments to your database here
     // reset to go again
     if($_SESSION['combo2_val'] >= 1){
     $_SESSION['combo1_val'] = 0; 
     $_SESSION['combo2_val'] = 0;
     }
     ?>
             </form>
             <a href="https://stackoverflow.com/questions/34361452/<?php echo htmlspecialchars($_SERVER["REQUEST_URI']); ?>">Click to start a new selection</a>
          </body>
     </html>

You could remove onChange="submit();" and replace it with a normal submit button on the form if you prefer.

Leave a Comment