0

A fried of mine asked me if I could take a look at his website and fix a form for him. Thinking it would be easy I accepted, but here I am stuck and asking for help.

So the website has a simple form (name, email and message)

 <form action="mail.php" method="post">
  <div class="large-7 columns">
    <div id="contact-form">
      <div id="name">
        <div class="form-icon">
          <i class="typcn typcn-user"></i>
        </div>
        <input name="name" id="form-name" type="text" placeholder="Name" />
      </div>

      <div id="email">
        <div class="form-icon">
          <i class="typcn typcn-mail"></i>
        </div>
        <input
          name="email"
          id="form-email"
          type="text"
          placeholder="Email"
        />
      </div>

      <div id="message">
        <div class="form-icon">
          <i class="typcn typcn-pencil"></i>
        </div>
        <textarea
          name="message"
          id="form-message"
          placeholder="Message"
        ></textarea>
      </div>
      <input class="btn" type="submit" name="submit" value="Submit" />
    </div>
  </div>
</form>

I want to use PHP to send an email with these answers, but after the SUBMIT button is pressed the server returns ERROR 404. I've spent all day trying to make it work looking at tutorials and reading answers from other people who had this problem, but I have no knowledge of how PHP works and I am stuck.

PHP code in mail.php is:

<?php
  if (isset($_POST['submit'])) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  $formcontent=" From: $name \n Message: $message";
  $recipient = "name@gmail.com";
  $subject = "Contact Form";
  $mailheader = "From: $email \r\n";
  mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
  echo "Thank You!" . " -" . "<a href='index.html' style='text- 
   decoration:none;color:#ff0099;'> Return Home</a>";
  }
?>

How I want it to work is for the visitor to enter the answers in the form and after submitting to be returned to the same page. I thought that it will work and I've deployed the code on Netlify here (you can see the form at the bottom of the page), but with no luck.

I apologize if this is post is too long and I appreciate your time spent reading this and your answer!

1 Answers1

0

To fix that redirection problem, you could try to add a starting '/' to your action attribute.

<form action="/mail.php">

Also, your mail.php has a really strange code smell. In my opinion it's better to use a library like PHPMailer or similar things to send mails in PHP.

Happy coding c: