表单传递选择到下一页

时间:2015-05-13 11:12:02

标签: php html mysql forms mysqli

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

The selector

当我选择1项并按Add to list时,会生成一个表格:

When i selected something

这一切的代码是:

    <!--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
}

?>

Functions:

    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();
        }

当我选中复选按钮时,按Add to list。你会去customer_list.php。在此页面上,我想显示所选项目的信息。我现在拥有的是:

<?php
    echo $_POST["checkbox"];
?>

这显示了一个数字(当我选择一个项目时),它是火车/物品的ID。

但是如何显示所选复选框的所有信息? 此外,如果我在表中有多列火车,例如10.我只选择7并按下按钮。我希望下一页customer_list.php能够显示特定的7个结果。

3 个答案:

答案 0 :(得分:1)

两件事:

1.Change

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

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

2.Change

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 getAllAssoc() {
        $sql = "SELECT * FROM train_information WHERE train_name IN(:train_name)";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(":train_name", implode(",",$_POST["train_name"]));
        $sth->execute();
        return $sth->fetchAll();
    }

只是调整查询中的引号,因为我没有测试过此代码,并认为单引号会出现问题。

上面做的是我们已经在数组中获取输入,并相应地根据此数组输入获取结果。

希望你能得到这个想法!!

答案 1 :(得分:0)

您需要将复选框代码编辑为:

<td><input type="checkbox" name="checkbox[]" value="<?php echo $res['train_id']?>"></td>

customer_list.php代码:

<?php

function getAllAssoc_id($id) {
        $sql = "SELECT * FROM train_information WHERE id = :id";
        $sth = $this->pdo->prepare($sql);
        $sth->bindParam(":id", $id);
        $sth->execute();
        return $sth->fetchAll();
    }

    //echo $_POST["checkbox"];

    echo"<table><tr><td>Train Name</td><td>Train Id</td></tr>";
    foreach($_POST["checkbox"] as $key=>$val){
    $data=getAllAssoc_id($val);
    echo"<tr><td>".$data['train_name']."</td><td>".$data['id']."</td></tr>";

    }
    echo"</table";
?>

答案 2 :(得分:-1)

您必须将复选框名称作为数组name="checkbox[]",因此在操作页面上您将获得一系列ID。现在你只需要使用mysql&#34; IN&#34;查询所选的train_id。子句。

我可以向您显示查询

Select * from table where train_id IN(array you got from check boxes)