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

2022-01-16 00:00:00 listbox jquery php javascript

这是我第一次使用这种媒介寻求帮助.我想知道如何在我的代码中包含两个列表框.一个查询数据库,并通过一个按钮将其发送给另一个,并使用多个选项来执行此操作,然后,就像在 php 代码中一样,将第二个列表框中的数据放在表数据库中.谢谢.

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. 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"> 

你可以添加一个 onChange();事件到您的 select1 下拉列表

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

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

在你的 <head>

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

那么你将不得不使用 php 从提交的页面中收集值

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

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

然后您可以返回带有第二个下拉列表的页面,其构造方式与创建第一个下拉列表的方式大致相同,使用从第一个下拉列表中返回的数据来设置第二个下拉列表.您可以将第一个下拉列表包装在 isset() 中:

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 
        }

和第二个在

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

这样它们只会在需要时出现.

so that they will only appear when needed.

正如@NanaPartykar 所说,mysqli 是唯一的出路.

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

它将依赖于 JavaScript,因此,如果您愿意,可以使用提交按钮而不是 JavaScript 函数:

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" />

这看起来是如何使用选择框的方便参考:获取组合框选定文本的 PHP 代码

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

我希望这能让您了解它是如何完成的 - SESSION 变量仍然需要在第二个下拉列表的循环中正确定义,但它们可能会直接来自您的数据库,而不是来自会话用于演示目的的数组.

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.

所有值都设置为数字;

intval(); 将提交的任何内容转换为整数以进行清理.

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();">' . "
";
        //while ($fila = mysqli_fetch_row($consulta)){
        while($i < count($main_opt)){
        echo "<option value='".$i."'>".$main_opt[$i]."</option>
";       
             $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>'  . "
";    
     }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();">' . "
";
             while($i < count($_SESSION['opt_vals'][$combo1_val])){
             echo "<option value='".$i."'>".$_SESSION['opt_vals'][$combo1_val][$i]."</option>
";
             $i++;
             }
         }
     echo '</select>'  . "
";
     }
     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>

如果您愿意,您可以删除 onChange="submit();" 并将其替换为表单上的普通提交按钮.

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

相关文章