Reputation: 1
<html>
<head>
<title>Guessing Game</title>
</head>
<body>
<h1>Welcome to my guessing game</h1>
<form action="" method="GET">
<label for="numinput">Enter your number</label>
<input type="text" name="numinput">
<input type="submit" name="submit">
</form>
<?php
$num = $_GET['numinput'];
if (!isset($_GET['num'])) {
echo("Missing Guess Parameter");
} elseif ( strlen($_GET['num']) <60){
echo ("Your guess is too short");
} elseif ( !is_numeric($_GET['num'])){
echo("Your guess is not a number");
} elseif ($_GET['num'] > 60){
echo ("Your guess is too high");
} else {
echo ("Congratulations - You are right");
}
?>
</html>
My goal is to create a simple number guessing game using $_GET in php Output: "Missing Guess Parameter" despite i provide any number in the input. Also, I couldn't understand whether to get the user provided data from submit of the textbox.
Upvotes: 0
Views: 641
Reputation: 1071
Name of the html input and the key of $_GET
must be identical, so change name="numinput"
to name="num"
will be a solution to fixed the problem.
NB: $num = $_GET['num'];
may accure UNDEFINED INDEX if you use it outside the if
condition when isset($_GET['num'])
equal to true
<html>
<head>
<title>Guessing Game</title>
</head>
<body>
<h1>Welcome to my guessing game</h1>
<form action="" method="GET">
<label for="numinput">Enter your number</label>
<input type="text" name="num">
<input type="submit" name="submit">
</form>
<?php
//$num = $_GET['num']; this should be after isset($_GET['num']) == true
if (!isset($_GET['num'])) {
echo("Missing Guess Parameter");
} elseif ( strlen($_GET['num']) <60){
echo ("Your guess is too short");
} elseif ( !is_numeric($_GET['num'])){
echo("Your guess is not a number");
} elseif ($_GET['num'] > 60){
echo ("Your guess is too high");
} else {
echo ("Congratulations - You are right");
}
?>
</body>
</html>
Upvotes: 1
Reputation: 1888
You need to put $num
instead of $_GET['num']
after the variable $num is set
<html>
<head>
<title>Guessing Game</title>
</head>
<body>
<h1>Welcome to my guessing game</h1>
<form action="" method="GET">
<label for="numinput">Enter your number</label>
<input type="text" name="numinput">
<input type="submit" name="submit">
</form>
<?php
$num = $_GET['numinput'];
if (!isset($num)) {
echo("Missing Guess Parameter");}
elseif ( strlen($num) <60){
echo ("Your guess is too short");}
elseif( !is_numeric($num)){
echo("Your guess is not a number");}
elseif ($num > 60){
echo ("Your guess is too high");}
else{
echo ("Congratulations - You are right");}
?>
</html>
Upvotes: 1