【问题标题】:Select multiple options from option从选项中选择多个选项
【发布时间】:2015-07-24 20:49:20
【问题描述】:

所以我有一个从数据库中获取信息的选择器。

当我从选择器中选择某些内容并按下:Add to list 时,它会生成一个包含所选信息的表格。

现在这是它应该做的。但是现在,当我选择另一个结果并按Add to list 时。它会删除旧的并用新的替换它。

但我实际上不希望它删除旧的,而是在它下面创建一个新行。所以那张桌子变大了。我该怎么做?

选择器代码:

<!--Selector-->
<?php
    //Get name and id data from the db. In an assoc array
        $results = $database->Selector();
        echo "<form name='form' method='POST' id='selector'>";
        echo "<select name='train_name' id='train_name' multiple='multiple'>";
        // Loop trough the results and make an option of every train_name
        foreach($results as $res){
                    echo "<option value=" . $res['train_name'] . ">" . $res['train_name'] . "</option>";
            }  
        echo "</select>";
        echo "<br />" . "<td>" . "<input type='submit' name='Add' value='Add to list'/>" . "</td>";
        echo "</form>";
    if(isset($_POST["train_name"])){

    //Get all data from database, in an assoc array
        $results = $database->getAllAssoc();

        //Make table headers
        ?>
        <div id="train_select_table">
        <form name="selector" method="post" action="customer_list.php?user_id=<?php echo $_SESSION['user_id']?>">
            <table>
                <tr>
                    <th>Train name</th>
                    <th>Number of bogies</th>
                    <th>Number of axles</th>
                    <th>Delete</th>
                    <th>More info</th>
                    <th>Check</th>
                <!--Only for admins (later!)-->
                <!--<th>XML</th>
                    <th>SQL</th>    -->
                </tr>
            <div id="looprow">
                <?php      
                    foreach($results as $res){
                    //Loop trough results, generate a tablerow every time
                ?>
                <tr>     
                    <td name="train_name"><?php echo $res['train_name']?></td>
                    <td><?php echo $res['number_of_bogies']?></td>
                    <td><?php echo $res['number_of_axles']?></td>
                    <td><a href="remove_from_table.php?train_id=<?php echo $res['train_id']?>">Delete</a></td>
                    <td><a href="expand_info.php?train_id=<?php echo $res['train_id']?>">More Information</a></td> 
                    <td><input type="checkbox" name="checkbox[]" value="<?php echo $res['train_id']?>"></td>
                                                <!--Only for admins (later!)--> 
                    <!--<td><a href="convert_to_xml.php?train_id=<?php echo $res['train_id']?>">XML</a></td>
                    <td><a href="convert_to_sql.php?train_id=<?php echo $res['train_id']?>">SQL</a></td>-->
                </tr>
            <?php
                }
            ?>
            </div>
        </table><br />
        <input name="Add to list" type="submit" id="add_to_list" value="add_to_list">
    </form>
    </div>
    <?php
        }
    ?>

功能:

function getAllAssoc() {
        $sql = "SELECT * FROM train_information WHERE train_name = :train_name";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(":train_name", $_POST["train_name"]);
        $sth->execute();
        return $sth->fetchAll();
    }

function selector() {
        $sql = "SELECT train_name, train_id FROM train_information";
        $sth = $this->pdo->prepare($sql);   
        $sth->execute();
        return $sth->fetchAll();
    }

我明白为什么它不断替换旧行。这是因为我发送了一个新查询。但我不知道如何保留旧的。

【问题讨论】:

    标签: php html mysql forms mysqli


    【解决方案1】:

    只需将多选列表的名称从 train_name 更改为 train_name[],如下所示:

    ...
    echo "<select name='train_name[]' id='train_name' multiple='multiple'>";
    ...
    

    通过这种方式,您的$_POST['train_name'] 将成为传递给bindParam 的数组

    【讨论】:

      【解决方案2】:

      因为每次用户都会选择一个值而不是多个值,所以如果你想同时使用新值和旧值,你应该使用cookiesession存储旧值或隐藏输入那个表格。这取决于你。

      例如一开始,这样做:

      <?php
      session_start();//make it be before your output if you want to use session or cookie
      if(!$_SESSION['train_name']) $_SESSION['train_name'] = array();
      if(isset($_POST["train_name"])) $_SESSION['train_name'] = array_merge ($_SESSION['train_name'], $_POST["train_name"]);
      .......
      //next step, inquiry with $_SESSION['train_name']
      

      【讨论】:

        猜你喜欢
        • 2022-07-16
        • 1970-01-01
        • 2020-03-21
        • 2010-10-06
        • 1970-01-01
        • 2023-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多