many elements of a listbox to another

后端 未结 1 1680
心在旅途
心在旅途 2020-12-02 02:59

This is the first time I use this medium to ask for help. I wanted to know how , in my code , include two listbox . One that makes querying the database and , through a butt

1条回答
  •  时光取名叫无心
    2020-12-02 03:45

    Your form needs an action to submit to itself:

         
    " method="post">

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

         
    

    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

       
      
      
      
      PRECEPTORÍA
      
        
        
        ' . "\n";
            //while ($fila = mysqli_fetch_row($consulta)){
            while($i < count($main_opt)){
            echo "\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 ''  . "\n";    
         }else{
             if(intval($_POST['combo1']) >= 1) $_SESSION['combo1_val'] = $combo1_val =intval($_POST['combo1']);
             if(intval($_POST['combo1']) >=1){
                 $i = 0;
                 echo ''  . "\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;
         }
         ?>
                 
                 Click to start a new selection
              
         
    

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

    0 讨论(0)
提交回复
热议问题