-1

I've been working on getting a PHP email form set up on my personal webpage, and I am running into an issue where when I hit submit, the page displays "Cannot post /mail.php".

I've seen several similar questions on this site before, but I haven't found any approaches that have solved the issue. I am new to PHP so I'm not sure how deep the error could be, or if I'm possibly missing something obvious.

I'm using Brackets Live Preview on Mac OS Big Sur.

Things I've tried:

  • Made sure that the mail.php file is in the same directory as my HTML files
  • Made sure I'm setting the name attribute in my HTML file so the PHP can pick up on the relevant entered information
  • Switched POST with GET (POST errors, GET download the PHP file instead of running it)
  • Downloaded PHP with Homebrew
  • Redownloaded Brackets

For reference this is my code: HTML File:



       <form action="mail.php" method="post">
            <p>Name</p> <input type="text" name="name">
            <p>Email</p> <input type="text" name="email">
            <p>Message</p><textarea name="message" rows="6" cols="40"></textarea><br>
            <input type="submit" value="Send"><input type="reset" value="Clear">
        </form> 

PHP File:

<?php
          $name = $_POST['name'];
          $email = $_POST['email'];
          $message = $_POST['message'];
          $formcontent="From $name \n Message: $message";
          $recipient = "email.redacted@gmail.com";
          $subject = "Contact Form";
          $mailheader = "From $email \r\n";
          mail($recipient, $subject, $formcontent, $mailheader) or die("Error");
?>

Any insight into what may be going wrong or some debugging steps to take would be much appreciated!

AeroFighter76
  • 73
  • 1
  • 9
  • 1
    `Made sure that the mail.php file is in the same directory as my HTML files` .... But the `/` in `/mail.php` goes to the base path, *not* the current path. Remove it, and see what happens. – GetSet Dec 26 '20 at 02:43
  • Same issue when I remove it as well, but thank you for the correction! – AeroFighter76 Dec 26 '20 at 02:48
  • Additional information that I am not sure is relevant, but preview basically runs the HTML/CSS on my localhost (127.0.0.1) – AeroFighter76 Dec 26 '20 at 02:51
  • 1
    Are you able to run any PHP using this preview? So if you just did `echo "Hello World";` does it appear? – Zack Dec 26 '20 at 02:53
  • Same error if I do echo "Hello World". I tried running the HTML file in a browser instead of on the preview, and when I click submit it just shows the text of the php file (like the code) – AeroFighter76 Dec 26 '20 at 02:56
  • @Zack is saying to run your app from the browser using "localhost" (or equivalent) so that your server preprocesses your PHP files and scripts when encountered. Not simply pulling up whatever html file in the browser from the filesystem. Which are you doing? – GetSet Dec 26 '20 at 03:38

2 Answers2

1

Here <? php you have whitespace after the ?. Remove it, and see what happens.

It should be <?php to be valid syntax. Otherwise the script breaks.

Edit: I'm not claiming this will solve any logical errors in your code, but that space in there is making the script not even PHP.

GetSet
  • 1,511
  • 2
  • 10
  • 13
  • I do not know how that space got included in the question but it is – AeroFighter76 Dec 26 '20 at 03:11
  • 2
    @AeroFighter76 This is the 2nd time you said that something in your editor is different from your question's code. Did you not simply copy and paste from editor to here? – GetSet Dec 26 '20 at 03:12
  • Was changing some things around during debugging so I copied a earlier version onto a word doc, I believe I copied that into here instead of my editor one ;/ def a mistake on my part but I don't think there's any more differences with my editor version at this point. I've updated the question code to the editor one just to be sure – AeroFighter76 Dec 26 '20 at 03:16
  • Then its likely a `Brackets Live Preview` issue. Your error is not reproducible when served by the server directly in the browser is it? E.g. `localhost/path_to_app/`? – GetSet Dec 26 '20 at 03:23
  • Ya, when run directly on a browser it's a different issue (just the code appearing rather than running) but I don't encounter the POST issue – AeroFighter76 Dec 26 '20 at 03:25
  • And are you using `localhost/` in your path, or are you using the "filesystem"? You need to call the server from the browser with `localhost/` or it's equivalent on your machine and server setup. Otherwise the browser *will* download the PHP files since at that point, they wont be getting processed by the server. ..... This is one way to rule out a server problem. And narrow it down to a Brackets Live Preview problem. – GetSet Dec 26 '20 at 03:27
  • If "localhost" or whatever local IP your server hosts at is not reachable, then your server is probably not even running. – GetSet Dec 26 '20 at 03:31
  • I was using the filesystem before, but if I access using localhost I get downloading behavior as well. I checked my XAMPP and my Apache web server seems to be up and running – AeroFighter76 Dec 26 '20 at 03:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226431/discussion-between-getset-and-aerofighter76). – GetSet Dec 26 '20 at 04:02
0

Is your form you have method="get". For you to access the information using $_POST you'll need to change it to method="post".

   <form action="/mail.php" method="post">
        <p>Name</p> <input type="text" name="name">
        <p>Email</p> <input type="text" name="email">
        <p>Message</p><textarea name="message" rows="6" cols="40"></textarea><br>
        <input type="submit" value="Send"><input type="reset" value="Clear">
    </form>

With regards to why your Bracket Live Preview is downloading the PHP file instead of running it. It sounds like your Bracket Live Preview isn't configured correctly.

You might be able to get some help from this question. Using Brackets for PHP files

Zack
  • 101
  • 6