Thank you all for your help with this. I am working on this code that generates a random number as soon as the page is loaded. The input field automatically has the number one displayed as the number the user is using to see if they hit the magic number. However, each time I hit submit the random number changes. I need to store the random number and have the user input their guesses in the input field until they get it right. I think I am almost done however need some guidance. Thank you all in advance for your help.
<?php session_start() ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Random Number Guessing Game</title>
<style>
/* petals.css */
body {
font-family: cursive;
text-align: center;
background-color: tan;
}
ul {
text-align: left;
width: 50%;
margin-left: auto;
margin-right: auto;
border: 1px black groove;
padding-top: .5em;
padding-bottom: .5em;
background-color: white;
}
</style>
</style>
<script>
$(document).ready(function(){
$('#guess').val(random());
});
window.onLoad = function () {$('#guess').val(random()};
</script>
</head>
<body>
<form action = "" method = "post">
<?php
$_SESSION[$randomNumber];
printForm();
function printForm(){
global $numGuess;
$rand = @rand(1, 10);
$guess = @filter_input(INPUT_POST, "guess");
$submit = $_POST['submit'];
$_SESSION[$randomNumber] = $randomNumber;
//load up variables
$hdnCounter = @filter_input(INPUT_POST, "hdnCounter");
//increment the counters
$hdnCounter++;
print <<<HERE
<form>
<fieldset>
<h2>Welcome to the Random Number Guessing Game!!!</h2>
<h3>Can you guess the correct random number?</h3>
<p>You guessed: $guess </p>
<input type = "hidden" name = "hdnCounter" value = "$hdnCounter" />
<p>Your total tries are: $hdnCounter</p>
<input type = "submit" value = "Guess Your Number" />
<input type = "text" name = "guess" value = "1" /><br />
<input type = "hidden" name = "random" value = "$rand(1, 10);" />
<br />
HERE;
if ($guess > $rand){
echo "<h3>Your number is higher than the magic number!</h3>";
} else if ($guess != $rand && $guess < $rand ){
echo "<h3>Your number is lower than the magic number!</h3>";
} else {
echo "<h1>You are Correct!!!</h1>";
} // end // end printForm
}
//keep the random number hidden from user...
//print "The number of guesses:<span class='numberGuess'>" "</span>";
?>
</div>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire