未定义的变量错误(对于占位符)显示在 html 文件中,即使它是在 php 文件中定义的


Undefined variable error (for a placeholder) is shown on html file, even when it's defined in php file

我遇到了一个N00bish问题,正在进行一个简单的表单验证。

我创建了一个表单(html),当它提交到单独的php文件时,它应该执行简单的数据验证。
html 包含错误消息的占位符,该占位符在 php 文件中定义。

但是,当我加载表单(在提交之前)时,我收到可怕的"注意:未定义的变量:C:''xampp''htdocs''practice''form.html中的 fnameError 在第 19 行"错误。
但是变量是在 php 文件上声明和初始化的......

我还尝试使用"include"在 html 中添加对 php 文件的引用。
但是,我随后收到一条不同的错误消息:"解析错误:语法错误,行中意外的文件结尾"

html 文件包含以下代码:

<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
</head>
<body>
<div class="maindiv">
<div class="form_div">
<div class="title">
<h2>Form validation with PHP</h2>
</div>
<form action="action_page.php" method="post" name="form_name" id="form_id" class="form_class">
<h3>Form:</h3>
<span class="error">* required field.</span><br>
<label>First Name:</label>
<input type="text" name="firstname" id="fname" placeholder="First Name" value="" pattern="[a-zA-Z]+" required><br>
<span class="error">* <?php echo $fnameError;?></span>
<label>Last Name:</label>
<input type="text" name="lastname" id="lname" placeholder="Last Name" value="" pattern="[a-zA-Z]+" required><br>
<span class="error">* <?php echo $lnameError;?></span>
<label>Email Address:</label>
<input type="email" name="emailaddress" id="email" placeholder="Email Address" value="" required><br>
<span class="error">* <?php echo $emailError;?></span>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</div>
</div>
</body>
</html>

PHP 文件包含以下代码:

<?php
// Initialize variables to null.
$fnameError = "";
$lnameError = "";
$emailError = "";
// On submitting the form, the below functions will execute.
if(isset($_POST['submit'])){
if(empty($_POST["firstname"])){
$fnameError = "First name is required";
} else {
$fname = test_input($_POST["firstname"]);
// Check firstname contains only letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
$fnameError = "Only letters and whitespace allowed";
}
}
if(empty($_POST["lastname"])){
$lnameError = "Last name is required";
} else {
$lname = test_input($_POST["lastname"]);
// Check firstname contains only letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
$lnameError = "Only letters and whitespace allowed";
}
}
if (empty($_POST["emailaddress"])) {
$emailError = "Email is required";
} else {
$email = test_input($_POST["emailaddress"]);
// check if e-mail address syntax is valid or not
if (!preg_match("/(['w'-]+'@['w'-]+'.['w'-]+)/",$email)) {
$emailError = "Invalid email format";
}
}
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//php code ends here
?>

我在这里做错了什么?
谢谢!

你需要

做两件事:-

  1. 关闭action_page.php文件中的第一个if语句。

  2. 写:- <?php if(isset($fnameError)) {echo $fnameError;}?>并对其他两个做同样的事情。

你没有关闭第一个 php 条件:if(isset($_POST['submit'])){". Close this with a },你的代码似乎很好!

代码中有两个错误:

解析错误:语法错误、行中文件意外结尾

您错过了其他伴侣已经提到的if(isset($_POST['submit']))的闭合大括号。

注意:未定义的变量:fname错误 C:''xampp''htdocs''practice''form.html 在第 19 行

您在初始化之前使用的是 PHP 变量。

溶液:

  1. 对于 ist 点,请在函数启动之前添加结束}
  2. 对于第二点,在HTML之前移动PHP代码。

1)因为,我清楚地看到了错误

"注意:未定义的变量:fnameError in C:''xampp''htdocs''practice''form.html 在第 19 行"

意味着,您具有页面名称形式.html因此,通常,您只能在扩展名.php文件中执行PHP代码,因为您的Web服务器是像PHP那样设置的。

表单.html更改为表单.php

创建一个名为 .htaccess 的文件并将以下代码放入其中,然后将此文件放在该网站的根目录中

AddType application/x-httpd-php .html

2) 将您的 php 代码放在<form></form>之前以显示相应的错误。

3)写

<span class="error">* <?php if(isset($fnameError)) {echo $fnameError;}?></span> 
<span class="error">* <?php if(isset($lnameError)) {echo $lnameError;}?></span>
<span class="error">* <?php if(isset($emailError)) {echo $emailError;}?></span>

因为没有得到

"注意:未定义的变量:"

4)您没有为if(isset($_POST['submit'])){关闭}。因此,关闭它以获得

解析错误:语法错误

更新的代码:

<?php
// On submitting the form, the below functions will execute.
if(isset($_POST['submit'])){
    if(empty($_POST["firstname"])){
        $fnameError = "First name is required";
    } 
    else {
        $fname = test_input($_POST["firstname"]);
        // Check firstname contains only letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
            $fnameError = "Only letters and whitespace allowed";
        }
    }
    if(empty($_POST["lastname"])){
        $lnameError = "Last name is required";
    } else {
        $lname = test_input($_POST["lastname"]);
        // Check firstname contains only letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
            $lnameError = "Only letters and whitespace allowed";
        }
    }
    if (empty($_POST["emailaddress"])) {
        $emailError = "Email is required";
    } else {
        $email = test_input($_POST["emailaddress"]);
        // check if e-mail address syntax is valid or not
        if (!preg_match("/(['w'-]+'@['w'-]+'.['w'-]+)/",$email)) {
            $emailError = "Invalid email format";
        }
    }
}
function test_input($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}
//php code ends here
?>
<!DOCTYPE html>
<html>
    <head><title>Form Validation</title></head>
    <body>
        <div class="maindiv">
        <div class="form_div">
            <div class="title">
                <h2>Form validation with PHP</h2>
                </div>
                <form action="action_page.php" method="post" name="form_name" id="form_id" class="form_class" >
                    <h3>Form:</h3>
                    <span class="error">* required field.</span><br>
                    <label>First Name:</label>
                    <input type="text" name="firstname" id="fname" placeholder="First Name" value="" pattern="[a-zA-Z]+" required><br>
                    <span class="error">* <?php if(isset($fnameError)) {echo $fnameError;}?></span>
                    <label>Last Name:</label>
                    <input type="text" name="lastname" id="lname" placeholder="Last Name" value="" pattern="[a-zA-Z]+" required><br>
                    <span class="error">* <?php if(isset($lnameError)) {echo $lnameError;}?></span>
                    <label>Email Address:</label>
                    <input type="email" name="emailaddress" id="email" placeholder="Email Address" value="" required><br>
                    <span class="error">* <?php if(isset($emailError)) {echo $emailError;}?></span>
                    <input type="submit" name="submit" id="submit" value="Submit">
                </form>
            </div>
        </div>
    </body>
</html>