将会话变量从一个页面传递到另一个页面

时间:2016-05-14 12:35:34

标签: javascript php session-variables

我有一个php页面,它是一种带有3个下拉部分的子表单,当你点击一个按钮时,它必须从下拉列表中发送选择。表格如下:

<div class="statsValPlate">
            <label>Main Score</label>
            <select name="scoreSelect">
                <option value="301">301</option>
                <option value="501">501</option>
                <option value="701">701</option>
                <option value="1001">1001</option>
            </select>
        </div>
        <div class="statsValPlate">
            <label>Legs Format</label>
            <select name="legSelect">
                <option value="3">2 of 3</option>
                <option value="5">3 of 5</option>
                <option value="7">4 of 7</option>
                <option value="9">5 of 9</option>
            </select>
        </div>
        <div class="statsValPlate">
            <label>Sets Format</label>
            <select name="setSelect ">
                <option value="3">2 of 3</option>
                <option value="5">3 of 5</option>
                <option value="7">4 of 7</option>
                <option value="9">5 of 9</option>
            </select>
        </div>
        <form method="get" action="scoreboard.php">
            <button id="submit" name ="submit" class="push_button red">Start Game</a><br>
        </form>

<?php

        $_SESSION['gameScore'] = $_POST['scoreSelect'];
        $_SESSION['legScore'] = $_POST['legSelect'];
        $_SESSION['setScore'] = $_POST['setSelect'];

?>

我希望例如第一个表单的选择必须在这里显示为标签值:

<label class="scorePlate" id="num1" value="<?php echo $_SESSION['gameScore']?>">
                            <!--<?php echo $_SESSION["player1Score"]?>-->
                            <script>
                                document.write(new_score_player_1);
                            </script>
                        </label>

但由于某种原因,它不起作用。问题是什么

1 个答案:

答案 0 :(得分:0)

首先,开始会话 - session_start()

其次,将所有选择放在表单中。当他们在表单之外时,所选的值不会在表单提交时发送。

其次,使表单的方法 POST ,而不是GET。

您的代码应如下所示:

<?php
    session_start();

    if (isset($_POST['submit']) {
        $_SESSION['gameScore'] = $_POST['scoreSelect'];
        $_SESSION['legScore'] = $_POST['legSelect'];
        $_SESSION['setScore'] = $_POST['setSelect'];
    }
?>

<form method="POST" action="scoreboard.php">
    <div class="statsValPlate">
        <label>Main Score</label>
        <select name="scoreSelect">
            <option value="301">301</option>
            <option value="501">501</option>
            <option value="701">701</option>
            <option value="1001">1001</option>
        </select>
    </div>
    <div class="statsValPlate">
        <label>Legs Format</label>
        <select name="legSelect">
            <option value="3">2 of 3</option>
            <option value="5">3 of 5</option>
            <option value="7">4 of 7</option>
            <option value="9">5 of 9</option>
        </select>
    </div>
    <div class="statsValPlate">
        <label>Sets Format</label>
        <select name="setSelect ">
            <option value="3">2 of 3</option>
            <option value="5">3 of 5</option>
            <option value="7">4 of 7</option>
            <option value="9">5 of 9</option>
        </select>
    </div>

    <button id="submit" name ="submit" class="push_button red">Start Game</a><br>
</form>

在另一页上:

<?php
    session_start();
?>
<label class="scorePlate" id="num1" value="<?php echo $_SESSION['gameScore']; ?>">
    <!--<?php echo $_SESSION["player1Score"]?>-->
    <script>
        document.write(new_score_player_1);
    </script>
</label>