+ 2
Guessing machine using PHP and HTML
I need assistance https://code.sololearn.com/wD2fMicHY99d/?ref=app
9 Antworten
+ 5
// Try this :
<?php
/*
// For testing in Code Playground
$_POST["submit"] = "submit";
$_POST["guess"]=75;
$_POST["number"]=75;
//
*/
if(isset($_POST["submit"]))
{
$number = $_POST["number"]; // from hidden field
$guess = $_POST["guess"]; // from user input
if($guess % 2 == 0)
{
$message = "Wrong answer! you guessed an even number. it should be odd!!";
}
else
{
if($guess == $number)
{
$message = "Correct answer!";
// Generate a new number again after
// user entered a correct answer
$number= rand(1,99);
$number= $number|1;
}
elseif($guess < $number)
{
$message = " It's too small! try a bigger one";
}
elseif($guess > $number)
{
$message = " It's too large! try a smaller one";
}
}
}
else
{
$message=" Hello there, can you guess what is the odd number?";
// Generate number on first time page loads
$number= rand(1,99);
$number= $number|1;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>A simple HTML form</title>
</head>
<body>
<h3 align="center"><?php echo $message; ?></h3>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<label for="guess">Try to guess an odd number between 1 and 99!</label><br/>
<input type="text" id="guess" name="guess">
</p>
<p>
<input type="hidden" name="number" value="<?php echo $number; ?>">
<button type="submit" name="submit" value="submit">Check</button>
</p>
</form>
</body>
</html>
+ 4
You're very welcome bro, glad to help, keep learning and stick around the neighborhood :-D
+ 3
Thanks : )
Well on your previous code you generate new random number every time the page loads or reloads, I simply check whether or not there's an entry in the request ($_POST) named "submit", which indicates that there's a value submitted. When the page loads the first time, that doesn't exist, then we create random number, and also we generate new random number after user submitted a correct answer, you can check the comments in the code, I put a little notes there too.
+ 1
I'm required to give a program that is a guessing machine, where everytime the user guessed it a new odd number wikk be generated
+ 1
The issue I'm facing with my code is, after each entry the program is generating a new odd number, so the previous generated one is gone
+ 1
Maaaan i love you ❤️🙊
+ 1
Can you let me know what was my issue
+ 1
Really thanks 🌹🌹🌹🌹🌹I'm reading the comments and trying to compare it with the previous code... Bundle of thanks Boss 😁
+ 1
Sure thing 👊🏻😎