You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP石头剪刀布游戏玩家1选择数据丢失问题求助

Fixing the Disappearing Player 1 Choice in Your PHP Rock Paper Scissors Game

Hey there! Let's fix that annoying issue where Player 1's choice disappears when Player 2 submits theirs in your Rock Paper Scissors PHP game.

The root problem here is that each form submission is a brand new HTTP request—so when Player 2 sends their choice, the server doesn't remember Player 1's selection from the previous request. Let's go through two solid fixes, starting with a quick one and then a more robust approach for future-proofing your game.


Method 1: Pass Player 1's Choice via a Hidden Form Field

This approach adds a hidden input to Player 2's form that carries Player 1's selection through to the final submission.

<?php
// Initialize variables with fallback values
$keuze1 = isset($_GET['keuze1']) ? $_GET['speler1'] : (isset($_GET['hidden_keuze1']) ? $_GET['hidden_keuze1'] : '');
$keuze2 = isset($_GET['keuze2']) ? $_GET['speler2'] : '';

// Show start button if no game actions have been taken
if (!isset($_GET['knop']) && !isset($_GET['keuze1']) && !isset($_GET['keuze2'])) {
    echo "<form method='GET'>
            <input type='submit' name='knop' value='Begin met spelen'>
          </form>";
}

// Show Player 1's choice form when start button is clicked
if (isset($_GET['knop'])) {
    echo "<h2>Speler 1</h2>
          <form method='GET'>
            <select name='speler1'>
              <option value='steen'>Steen</option>
              <option value='papier'>Papier</option>
              <option value='schaar'>Schaar</option>
            </select>
            <input type='submit' name='keuze1' value='kiezen'>
          </form>";
}

// Show Player 2's form with hidden field for Player 1's choice
if (isset($_GET['keuze1'])) {
    $keuze1 = $_GET['speler1'];
    echo "<h2>Speler 2</h2>
          <form method='GET'>
            <!-- Hidden field to carry Player 1's choice through -->
            <input type='hidden' name='hidden_keuze1' value='$keuze1'>
            <select name='speler2'>
              <option value='steen'>Steen</option>
              <option value='papier'>Papier</option>
              <option value='schaar'>Schaar</option>
            </select>
            <input type='submit' name='keuze2' value='kiezen'>
          </form>";
}

// Display the final results
if (isset($_GET['keuze2'])) {
    $keuze1 = $_GET['hidden_keuze1'];
    $keuze2 = $_GET['speler2'];
    echo "Speler 1 koos $keuze1 en Speler 2 koos $keuze2";
}
?>

What Changed:

  • Added a hidden_keuze1 input field to Player 2's form that stores Player 1's selection. When Player 2 submits, this value is sent alongside their choice.
  • Updated the $keuze1 initialization to pull from the hidden field when Player 2 submits their choice.

For more complex games or if you need to persist data across multiple requests, sessions are a better solution. They store data on the server tied to the user's browser session.

<?php
// Start the session - MUST be called before any HTML output
session_start();

// Initialize variables from session or request
$keuze1 = isset($_SESSION['keuze1']) ? $_SESSION['keuze1'] : '';
$keuze2 = isset($_GET['keuze2']) ? $_GET['speler2'] : '';

// Reset session and show start button for new game
if (!isset($_GET['knop']) && !isset($_GET['keuze1']) && !isset($_GET['keuze2'])) {
    session_unset(); // Clear old game data
    echo "<form method='GET'>
            <input type='submit' name='knop' value='Begin met spelen'>
          </form>";
}

// Show Player 1's choice form
if (isset($_GET['knop'])) {
    echo "<h2>Speler 1</h2>
          <form method='GET'>
            <select name='speler1'>
              <option value='steen'>Steen</option>
              <option value='papier'>Papier</option>
              <option value='schaar'>Schaar</option>
            </select>
            <input type='submit' name='keuze1' value='kiezen'>
          </form>";
}

// Save Player 1's choice to session and show Player 2's form
if (isset($_GET['keuze1'])) {
    $keuze1 = $_GET['speler1'];
    $_SESSION['keuze1'] = $keuze1; // Store choice in session
    echo "<h2>Speler 2</h2>
          <form method='GET'>
            <select name='speler2'>
              <option value='steen'>Steen</option>
              <option value='papier'>Papier</option>
              <option value='schaar'>Schaar</option>
            </select>
            <input type='submit' name='keuze2' value='kiezen'>
          </form>";
}

// Display final results
if (isset($_GET['keuze2'])) {
    $keuze2 = $_GET['speler2'];
    echo "Speler 1 koos $keuze1 en Speler 2 koos $keuze2";
    // Optional: Clear session after game ends
    // session_unset();
}
?>

What Changed:

  • Added session_start() at the top (critical: this must come before any HTML output, including spaces).
  • When Player 1 makes a choice, we save it to $_SESSION['keuze1'], so it persists across requests.
  • Reset the session when starting a new game to avoid leftover data from previous plays.

Final Notes:

  • The hidden field method works great for simple, single-step data passing.
  • Sessions are more scalable—use this if you plan to add scores, multiple rounds, or other features that require persistent state.

内容的提问来源于stack exchange,提问作者Mauro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 16:22:38