0
<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.

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.