2016-05-14 266 views
-1

我有一個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> 

但由於某種原因,這是行不通的。有什麼問題

+0

當您提交給網頁它去'scoreboard.php'所以如何設置' $ _SESSION'在該頁面中? –

回答

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> 
+0

以及它不起作用,無論我是否在這些頁面開始會話,它只是不起作用 – Reaper9806

相關問題