Skip to content
Advertisement

How do fix ‘404 page not found’ error when running a php file?

I am just starting to learn php and when I run my php file, no matter the browser, i get a 404 error. What is my code missing that is causing this? This file is supposed to display a message after a user submits a ‘contact me’ form.

<!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>

Advertisement

Answer

this code works. see this is firefox: 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>

if you don’t specify a default value on your parameters from another form you could run into an unexpected error. This version is better 😉

I’m sure you get this error because you have a typo in one of your urls

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement