首页 > 解决方案 > 运行 php 文件时如何修复“404 page not found”错误?

问题描述

我刚刚开始学习 php,当我运行我的 php 文件时,无论使用哪种浏览器,我都会收到 404 错误。我的代码缺少什么导致这种情况?该文件应该在用户提交“联系我”表单后显示一条消息。

<!DOCTYPE html>
<html lang="">
<head>
    <title>Contact Form Submitted</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
    Thank you, <?php echo $_POST["name"]; ?> for your comments<br>
    You stated in your test Message:<br>
    <?php echo $_POST["message"]; ?>
    We will respond to you at <?php echo $_POST["email"]; ?> when we make updates to the site.
</p>
</body>
</html>

标签: phphtmlformshttp-status-code-404

解决方案


此代码有效。看到这是火狐:https ://i.imgur.com/YeBFt9B.png

<?php
// var    // variable value       // default value
$php_path = $_SERVER['PHP_SELF']  ?? "";
$name     = $_POST["name"]        ?? "name";
$message  = $_POST["message"]     ?? "message";
$email    = $_POST["email"]       ?? "email";

?>
<!DOCTYPE html>
<html lang="">
<head>
    <title>Contact Form Submitted</title>
</head>
<body>
<form action="<?= $php_path ?>" method="POST">
<p>
    Thank you, <?= $name ?> for your comments<br>
    You stated in your test Message:<br>
    <?= $message ?>
    <br><br>
    We will respond to you at <?= $email ?> when we make updates to the site.
</p>
</form>
</body>
</html>

如果您没有在其他表单的参数上指定默认值,您可能会遇到意外错误。这个版本更好;)

我确定您收到此错误是因为您的某个网址中有错字


推荐阅读