Can anyone help me or get me going in the right direction? Here is the problem.
I need to create a script that presents a word guessing game. Allow users to guess the word letter-by-letter by entering a character in a form. Start by assigning a secret word to avariable. After each gues, print the word using astricks for each remaing letter, but fill in the letters that the user guessed correxctly. You need to store the user's guess in a hidden form field. For example, if I want them to guess "suspicious" and they already guessed "S" "I" then store s*s*i*i**s in the hidden form field. I need to have the one document that process and displays the form.

Dani AI

Generated

asked for a single-file PHP guess-the-word page that keeps the revealed letters in a hidden input (example progress: s*s*i*i**s). was right to point out carrying state in a hidden field. The snippet below shows a compact, single-page pattern that updates the masked string on every POST, does basic validation, preserves previously revealed letters, and escapes output to avoid XSS.

The idea in plain steps: keep a secret word in the script and initialize a progress string of asterisks of the same length; when the form posts, read the submitted one-letter guess and the current progress string; validate the guess as a single alphabetic character; rebuild the progress by scanning each character of the secret and revealing it only where the guess matches (case-insensitive) while keeping any letters already revealed; put the updated progress back into the hidden input for the next submit.

<?php
$secret = 'suspicious';
$len = strlen($secret);
$notice = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $letter = isset($_POST['letter']) ? trim($_POST['letter']) : '';
    $letter = substr($letter, 0, 1);
    // simple validation: single alphabetic char
    if (!preg_match('/^[A-Za-z]$/', $letter)) {
        $notice = 'Enter a single letter.';
        $progress = isset($_POST['progress']) && strlen($_POST['progress']) === $len ? $_POST['progress'] : str_repeat('*', $len);
    } else {
        $letterLower = strtolower($letter);
        $progress = isset($_POST['progress']) && strlen($_POST['progress']) === $len ? $_POST['progress'] : str_repeat('*', $len);
        $newProgress = '';
        for ($i = 0; $i < $len; $i++) {
            $ch = $secret[$i];
            $newProgress .= (strtolower($ch) === $letterLower) ? $ch : $progress[$i];
        }
        $progress = $newProgress;
    }
} else {
    $progress = str_repeat('*', $len);
}
?>
<form method="post" action="">
  <p>Word: <?php echo htmlspecialchars($progress, ENT_QUOTES, 'UTF-8'); ?></p>
  <p><input name="letter" maxlength="1" /></p>
  <input type="hidden" name="progress" value="<?php echo htmlspecialchars($progress, ENT_QUOTES, 'UTF-8'); ?>" />
  <p><input type="submit" value="Submit Letter" /></p>
  <?php if ($notice) echo '<p>'.htmlspecialchars($notice, ENT_QUOTES, 'UTF-8').'</p>'; ?>
</form>

Notes and cautions: hidden fields are client-editable, so if integrity matters, store the secret/progress server-side (sessions). Prefer POST over GET to avoid leaking state in URLs. Check that the hidden progress length equals the secret length to avoid malformed input. To improve UX, track guessed letters (another hidden field or session) to prevent repeated guesses and add a max-attempt counter if needed. This pattern implements the single-document flow requested and preserves revealed letters between submits.

Recommended Answers

All 3 Replies

The steps you need to take are: create a variable that holds the word, make a textbox for the user to put in a guess. You can use echo to print out the remaining letters and the function substr_replace to replace the letters with asterisks. For the hidden field, set the input type to hidden when you create the field. Try to get the form set up first and then we can help you with the processing if you need it.

Thanks I will work on it.

Thanks I will work on it.

I was thinking to split the string into an array. Do you use the sub_replace after this?
<html>
<head>
<title>Guessing Game</title>

</head>
<body>
<h1>Guessing Game</h1><hr />
<?php

$MysteryWord = "suspicious";
$MysteryWordArray = str_split("suspicious");


echo "<p>Mystery word: **********</p>";
echo "<p>Enter a letter and click the Submit Letter button.</p>";

?>
<form action="GuessingGame.php" method="get" enctype="application/x-www-form-urlencoded">
<p><input type="text" name="letter" />
<input type="hidden" name="progress"
<?php if (isset($Progress)) echo "value='$Progress'"; else echo "value='**********'"; ?> /></p>
<p><input type="submit" value="Submit Letter" />
</form>
</body>
</html>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.