简体   繁体   English

HTML 形式的正确 PHP 输入

[英]Proper PHP input for HTML form

Currently getting an 'Unidentified Index' error for my PHP file.目前我的 PHP 文件出现“未知索引”错误。

I created a form on an HTML that requires users to enter info into a textbox.我在 HTML 上创建了一个表单,要求用户在文本框中输入信息。 It's a basic calculation, like 5X3, so the user must enter '15'.这是一个基本的计算,如 5X3,因此用户必须输入“15”。 I then need to use PHP as well as an if-else statement to determine if the user correctly entered the right number.然后我需要使用 PHP 以及 if-else 语句来确定用户是否正确输入了正确的数字。 But I believe that I have not properly set up my PHP file.但我相信我没有正确设置我的 PHP 文件。 This is my first time using PHP, so I'm not entirely sure what other alternatives to using.这是我第一次使用 PHP,所以我不完全确定可以使用哪些其他替代方案。 I have pretty much just structured my PHP file off examples from our lecture notes.我几乎只是根据我们的讲义中的示例构建了我的 PHP 文件。 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注意:未定义索引:第 6 行 /Applications/XAMPP/xamppfiles/htdocs/CIT273/timetable.php 中的 Q1

You first need to wrap ur input in a form tag with a post action您首先需要将您的输入包装在带有 post 操作的表单标签中

<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.";
        }
?>

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.";
        }
    }
?>

you can resolve that in 2 ways你可以通过两种方式解决这个问题

as @Robin Singh pointed //PHP code for corresponding label正如@Robin Singh 指出的那样//对应 label 的 PHP 代码

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这样,当用户不输入任何内容时,您将在 $Q3 中拥有 null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM