PHP变量通过表单从页面传递到另一个页面

时间:2014-09-04 17:52:29

标签: php forms session variables

我知道这个问题已经以多种方式提出,但我似乎可以弄清楚问题是什么。

基本上我有一个表单,我只需要通过会话传递给另一个页面的变量。表单是一个下拉选择框。用户选择位置,然后表单转到根据该位置加载信息的页面。

<form method = "post" action="sneakpeek.php">

                            <?php
                                $con=mysqli_connect("******","********","*******","*****");
                             if (mysqli_connect_errno()) {
                                echo "Failed to connect to MySQL: " . mysqli_connect_error();
                             }
                            session_start();


                             $result = mysqli_query($con,"SELECT * FROM installations");

                             echo '<select id="selected_page">';
                             while($row = mysqli_fetch_array($result)) {
                                echo '<option value="sneakpeek.php">'.$row['name'].'</option>';
                             }
                             echo '</select>';
                               if($_SERVER['REQUEST_METHOD']=='POST')
                               {
                                $installation = $_POST['installation_id'];
                                $_SESSION['installation_id']=$installation_id;
                              }
                             mysqli_close($con);
                                ?>

                            <button>Load it ! </button>
                        </form>

接收页面:

    session_start();

$installation_id = $_SESSION['installation_id'];
<?php echo $installation_id; ?>

2 个答案:

答案 0 :(得分:1)

在任何输出之前

session_start(); 必须在页面顶部

<?php
session_start();
?>
<form method = "post" action="sneakpeek.php">
<?php
    $con=mysqli_connect("******","********","*******","*****");

    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM installations");

答案 1 :(得分:0)

session_start()必须是输出的第一件事。

http://php.net/manual/en/function.session-start.php