1

Currently getting an 'Unidentified Index' error for my PHP file.

I created a form on an HTML that requires users to enter info into a textbox. It's a basic calculation, like 5X3, so the user must enter '15'. I then need to use PHP as well as an if-else statement to determine if the user correctly entered the right number. But I believe that I have not properly set up my PHP file. This is my first time using PHP, so I'm not entirely sure what other alternatives to using. I have pretty much just structured my PHP file off examples from our lecture notes. Have I properly set everything up?

//HTML form code
<label for="Question 3">Question Three: <strong>2 X 9</strong></label><br>
<input type="text" id="Q3" name="Q3" placeholder="Type Answer Here"><br>

//PHP code for corresponding label
$Q3 = $_POST['Q3'];

//if statement regarding a particular question
if ($Q3 == "18")
  {
    echo "Well done, that's Correct!";
  }
  else
  {
    echo "Sorry, that's incorrect.";
  }

Well done, that's correct! //if user inputs correct answer actual results:

Notice: Undefined index: Q1 in /Applications/XAMPP/xamppfiles/htdocs/CIT273/timetable.php on line 6

1
  • You have to use if(isset($_POST['Q3'])) { } and put your all php code inside this. Commented Oct 16, 2019 at 5:29

3 Answers 3

3

You first need to wrap ur input in a form tag with a post action

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  <label for="Question 3">Question Three: <strong>2 X 9</strong></label>
  <input type="text" id="Q3" name="Q3" placeholder="Type Answer Here">
  <input type="submit" name="submit" value="Submit">  
</form>

<?php
        //if statement regarding a particular question
        if ($_POST['Q3'] == "18") {
            echo "That's Correct!";
        } else {
            echo "That's incorrect.";
        }
?>

Sign up to request clarification or add additional context in comments.

1 Comment

I have a form method above, I just decided to choose 'Q3' as my example. Assumed people willing to help weren't going to waste their time glancing over pretty much a repetitive program.
0

Try This one.

<form method="post">
    <label for="Question 3">Question Three: <strong>2 X 9</strong></label><br>
    <input type="text" id="Q3" name="Q3" placeholder="Type Answer Here"><br>
</form>

<?php
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
        $Q3 = $_POST['Q3'];

        //if statement regarding a particular question
        if ($Q3 == "18") {
            echo "Well done, that's Correct!";
        } else {
            echo "Sorry, that's incorrect.";
        }
    }
?>

3 Comments

awesome. every time I tried to launch 'Submit' my form, I have been having extreme difficulty. I wasn't sure if this was because of how I set my PHP file up or what. But still trying to figure that one out!!
so, i still haven't been able to figure out my path yet. so when I load this into my server. should I be getting a blank page? Since I haven't inputted anything for my Q3?
You can put this code directly in your <body> tag. and it will work good.
0

you can resolve that in 2 ways

as @Robin Singh pointed //PHP code for corresponding label

if(!isset($_POST['Q3'])) {
    $Q3 = null;
}

OR

$Q3 = $_POST['Q3'] ?? null

that way you will have null in $Q3 when user don't input anything

2 Comments

ok awesome. Now this is a bit off-topic of my above question. But I created my PHP file externally from my html file. Would incorrectly setting the path obstruct my outcome? I have literally spent hours today trying to correctly set this up. I downloaded XAMPP for my server but for some reason just can't get my intended outcomes to display, or in fact even load once I hit my 'submit' button on my html form. Any idea how to fix this? I'm doing this on my personal Mac. If not, no biggie!
there is always room for mistake :)

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.