列表框的许多元素到另一个 [英] many elements of a listbox to another

查看:140
本文介绍了列表框的许多元素到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用此媒体寻求帮助。我想知道在我的代码中如何包含两个列表框。一个使查询数据库,并通过按钮,将其发送到另一个,并使用多个选项,然后,如在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.

所有值都设置为数字;

All values are set as numbers;

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();">' . "\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>

您可以删除 onChange =submit();并根据您的喜好将其替换为表单上的普通提交按钮。

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

这篇关于列表框的许多元素到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆