简体   繁体   English

列表框的许多元素到另一个

[英]many elements of a listbox to another

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 button , send it to the other, and to do so with more than one option , and then , as in the php code to put the data from the second listbox on a table database. 一个使查询数据库,然后通过一个按钮将其发送到另一个数据库,并使用多个选项进行发送,然后使用php代码中的,将来自第二个列表框的数据放在表数据库中。 Thank you. 谢谢。

<?php
include("server.php");
$conexion = mysql_connect($server,$user, $pass) or die ("<br> No se puede conectar con la base de datos");
$conectarbase = mysql_select_db ($bd);

$consulta = mysql_query("SELECT * FROM modalidad")

?>
<html lang = "es">
  <head>
    <meta charset = "iso-8859-1"/>
    <meta name = "description" content = "Ejemplo de HTML5"/>
    <meta name = "keywords" content = "HTML5, CSS3, Javascript"/>
    <title>PRECEPTORÍA</title>
    <link rel="shortcut icon" href="./style/favicon.png" /> 
    <link rel = "stylesheet" href = "./style/style.css">
  </head>
  <body class = "body">
    <section class = "section">
      <div class = "div">
        <form>
          <fieldset id = "fieldsetalum">
            <legend id = "legendalum">Registro de nuevos alumnos</legend>
            <select name="combo1" multiple size="8">
              <?php
while ($fila = mysql_fetch_row($consulta))
{
echo "<option value='".$fila['0']."'>".$fila['1']."</option>";
}
?>
            </select>
            <select name="combo2" multiple size=></select>
          </fieldset>
        </form>
      </div>
    </section>
  </body>
</html>

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(); 您可以添加一个onChange(); event to your select1 dropdown 您的select1下拉菜单中的事件

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

and have a JavaScript script in your <head> 并在<head>包含一个JavaScript脚本

     <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 然后,您将不得不使用php从提交的页面中收集值

      $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() : 您可以将第一个下拉列表包装在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. 正如@NanaPartykar所说,mysqli是唯一的方法。

It will be dependent on JavaScript, so, if you prefer, you could have a submit button instead of the JavaScript function: 它取决于JavaScript,因此,如果您愿意,可以有一个Submit按钮而不是JavaScript函数:

     <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 这似乎是有关如何使用选择框的方便参考: PHP代码以获取组合框的选定文本

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. 我希望这会让您对如何完成操作有一个想法-SESSION变量仍然需要在循环中为第二个下拉列表正确定义,但那时它们可能直接来自您的数据库,而不是来自用于演示目的。

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="<?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="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>">Click to start a new selection</a>
          </body>
     </html>

You could remove onChange="submit();" 您可以删除onChange="submit();" and replace it with a normal submit button on the form if you prefer. 并根据需要将其替换为表单上的常规提交按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM