Jump to content

Updating a count in a form


watchnoface21

Recommended Posts

I want to write a guessing game that gives a user four tries to guess a number between 1 and 10.

The problem is that every time the user hits submit, count is reset to zero and 1 is added. Please help.

Here is my code:

 

<html>

<head>

<title>Guessing Game </title>

</head>

<h1>Guessing Game </h1>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<input type="hidden" name="count" value="<?php $count++; ?>" />

<p><input type="text" name="guess" /></p>

<p><input type="submit" value="Guess" /></p>

</form>

 

</body>

</html>

 

<?php

 

$random = rand(1,10);

$guess = $_POST['guess'];

 

if ($guess >= 10)

{

echo"Guess has to be lower than 10";

}

elseif  (isset($guess) && ($guess <= 1))

{

echo "Guess has to be higher than 1";

}

else

{

 

if ($guess == $random)

{

echo "Congratulations. You Win!!!!";

$count = 4;

 

}

elseif (isset($guess) && ($guess != $random))

{

echo "Try again";

 

 

$count++;

 

}

elseif (($count == 4) && ($guess != $random))

{

echo "Game Over. You Lose!!!";

}

 

}

 

 

?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/215549-updating-a-count-in-a-form/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.