PHP表单操作标记中的PHP

时间:2022-10-16 10:53:39

First PHP page, so this is likely a problem surfacing out of something stupid I'm missing, but I'm not sure what. I'm following the tutorial on W3Schools to create a form with protection from XSS, but when I use the code<form method="post" action="<?php echo $_SERVER['PHP_SELF'); ?>">, it is parsed such that the first > is associated with the form tag, so the quotes are mismatched, and the action doesn't complete correctly.

第一个PHP页面,所以这可能是一个问题,因为我错过了一些愚蠢的东西,但我不确定是什么。我正在按照W3Schools的教程创建一个受XSS保护的表单,但是当我使用代码

”>时,它被解析为第一个>与表单标记相关联,因此引号不匹配,并且操作无法正确完成。

This is what the page looks like:

这就是页面的样子:

PHP表单操作标记中的PHP

EDIT: Full Code Below

编辑:下面的完整代码

<body>
<?php
    $fname = $lname = $email = $student = "";
    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
        $fname = $_POST["fname"];
        $lname = $_POST["lname"];
        $email = $_POST["email"];
        switch($_POST["student"])
        {
            case "u":
                $student = "Undergraduate";
                break;
            case "g":
                $student = "Graduate";
                break;
            default:
                $student = "Non-Student";
        }   
    }
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'); ?>">
    <p>First Name: <input type="text" name="fname"> </p>
    <p>Last Name: <input type="text" name="lname"> </p>
    <p>Email: <input type="email" name="email"> </p>
    <p>Student Status: <select name="student">
                        <option value="u">Undergraduate</option>
                        <option value="g">Graduate</option>
                        <option value="x">Non-Student</option>
                    </select> </p>
    <input type="submit" value="Submit">
</form>

<?php
    echo "<h3>Input:</h3>"
    echo "Name: " . $fname . " " . $lname . "<br>";
    echo "Email: <a href=mailto:" . $email . ">" . $email . "</a><br>";
    echo "Student: " . $student;
?>
</body>

2 个解决方案

#1


.html files do not get parsed like .php files do, therefore you will need to install a Webserver on your system.

.html文件不像.php文件那样被解析,因此您需要在系统上安装Web服务器。

Sidenote: You can instruct Apache to treat .html files as PHP, if and when the time comes that you want to do this, it is possible.

旁注:您可以指示Apache将.html文件视为PHP,如果时间到了,您可以这样做。

.php files cannot be run directly from a web browser, unless they are parsed and running off a server or a hosted site.

.php文件不能直接从Web浏览器运行,除非它们被解析并在服务器或托管站点上运行。

They require to be accessed as http://localhost/file.php from a local machine.

它们需要从本地计算机以http://localhost/file.php的形式访问。

Depending on your platform, you can use Xampp which runs on Windows, Mac and Linux.

根据您的平台,您可以使用在Windows,Mac和Linux上运行的Xampp。

Wamp:

Mamp (Mac):


Plus, you have a few syntax errors.

另外,您有一些语法错误。

action="<?php echo $_SERVER['PHP_SELF'); ?>">
                                      ^

that should be a square bracket, rather than a parentheses.

这应该是一个方括号,而不是括号。

action="<?php echo $_SERVER['PHP_SELF']; ?>">

and echo "<h3>Input:</h3>" is missing a closing semi-colon.

和echo“

输入: ”缺少一个结束分号。

Those would throw/cause a parse error.

那些会抛出/导致解析错误。

#2


The solution may be apparent, the closing bracket is mismatched.

解决方案可能很明显,结束括号不匹配。

Change:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'); ?>">

To:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Notice ['PHP_SELF') and ['PHP_SELF'].

注意['PHP_SELF')和['PHP_SELF']。

#1


.html files do not get parsed like .php files do, therefore you will need to install a Webserver on your system.

.html文件不像.php文件那样被解析,因此您需要在系统上安装Web服务器。

Sidenote: You can instruct Apache to treat .html files as PHP, if and when the time comes that you want to do this, it is possible.

旁注:您可以指示Apache将.html文件视为PHP,如果时间到了,您可以这样做。

.php files cannot be run directly from a web browser, unless they are parsed and running off a server or a hosted site.

.php文件不能直接从Web浏览器运行,除非它们被解析并在服务器或托管站点上运行。

They require to be accessed as http://localhost/file.php from a local machine.

它们需要从本地计算机以http://localhost/file.php的形式访问。

Depending on your platform, you can use Xampp which runs on Windows, Mac and Linux.

根据您的平台,您可以使用在Windows,Mac和Linux上运行的Xampp。

Wamp:

Mamp (Mac):


Plus, you have a few syntax errors.

另外,您有一些语法错误。

action="<?php echo $_SERVER['PHP_SELF'); ?>">
                                      ^

that should be a square bracket, rather than a parentheses.

这应该是一个方括号,而不是括号。

action="<?php echo $_SERVER['PHP_SELF']; ?>">

and echo "<h3>Input:</h3>" is missing a closing semi-colon.

和echo“

输入: ”缺少一个结束分号。

Those would throw/cause a parse error.

那些会抛出/导致解析错误。

#2


The solution may be apparent, the closing bracket is mismatched.

解决方案可能很明显,结束括号不匹配。

Change:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'); ?>">

To:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Notice ['PHP_SELF') and ['PHP_SELF'].

注意['PHP_SELF')和['PHP_SELF']。