选择框使用设置的会话回显空白

时间:2019-03-29 19:32:18

标签: php html session isset

          <select name="score-1" class="score-1">
            <?php
              if(isset($_SESSION['s_score1'])){ //echoes currently set team
              echo $_SESSION['s_score1'];
              }else {
                echo "<option>0</option>
                      <option>1</option>
                      <option>2</option>
                      <option>3</option>
                      <option>4</option>";
              }
            ?>
          </select>


<?php //SETTING SESSION VARIABLES FOR ROUND 1 TEAMS AND SCORE

session_start();

  if (isset($_POST['go1'])) {

    include_once 'dbcon.php';

    $_SESSION['t_team1'] = $_POST['team-1'];
    $_SESSION['t_team2'] = $_POST['team-2'];
    $_SESSION['s_score1'] = $_POST['score-1'];
    $_SESSION['s_score2'] = $_POST['score-2'];
    header("Location: ../tables.php?tables=winner");
}
?>

它基本上用单个空格代替当前会话变量填充选择框 我似乎找不到问题

1 个答案:

答案 0 :(得分:0)

您有条件地打印选项。您应该始终打印选项,并仅将适当的选项标记为“已选择”。

<select name="score-1" class="score-1">
            <?php
                 $val = isset($_SESSION['s_score1']) ? $_SESSION['s_score1'] : false;
                 foreach(range(0,4) as $n) {
                   $selected = $n === $val ? 'selected' : ''; 
                   echo '<option ' .$selected . '>'.$n.'</option>';
                 }
            ?>
          </select>