0

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>
Nudge
  • 51
  • 1
  • 6

2 Answers2

0

this code works. see this is firefox: https://i.stack.imgur.com/Ksw1N.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

  • That code gives me errors saying 'Coalesce operator is available in PHP 7 only' – Nudge Sep 16 '20 at 20:41
  • you are not running version 7 or above? https://www.php.net/supported-versions.php –  Sep 16 '20 at 20:43
  • Im using PHP Storm by jetbrains, its the most recent version, I have an education license for it, so I don't think I can change the version. – Nudge Sep 16 '20 at 20:48
  • https://stackoverflow.com/questions/4122585/how-can-i-set-the-php-version-in-phpstorm PHP 5 is old and bad. PHP7 was a huge improvement –  Sep 16 '20 at 20:56
  • Okay thanks, wish my professor would have mentioned that. Also my contact form is an html file. Does it have to be a php file for this to work, All I am trying to do is redirect the user to a blank page after they click submit that stats the thank you msg. – Nudge Sep 16 '20 at 21:00
  • yes it has to be a php file and you need somthing like xampp in order to execute php because php gets just in time translated and is not compiled at once. If you want to redirect a user in php there is the `header("Location: /path/to/file.php");` function –  Sep 16 '20 at 21:31
0

First of all use htmlspecialchars in:

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

you prevent XSS attack if user enter url like: http://www.example.com/test_form.php/%22%3E%3Cscript%3Ealert('hacked')%3C/script%3E

If you are getting 404 error it's probably becouse your action of the form on contact me page don't point to right script.

If you have two separate files, one for form and second for thanks message you have to make sure than the urls are correct.

// form.php
...
<form action="thanks-message.php">
    ...
</form>
...
// thanks-message.php
<?php
$name = $_POST["name"] ?? "Joe";
$message = $_POST["message"] ?? "Lorem ipsum...";
$email = $_POST["email"] ?? "joe@example.com";
?>
...
<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 element is not necessary on this page

wujido
  • 139
  • 1
  • 10