-1

I'm new to PHP and I'm making a contact form for my website. However whenever I fill out and submit the form it leads me to a blank page and I never receive an email. I have tested it when my website is live. My host is infinity free if it matters.

I tried troubleshooting such as making minor modifications to the code (changing a few names, etc.) Nothing worked.

<html>
<section id="contact">
  <div class="container-contact">
<div style="text-align:center">
  <h3> Contact Me </h3>
      <p> I will get back to you shortly. </p>
</div>
<div class="row">
      <div class="column">
    <form id="contact-form" method="post" action="contactform.php">
      <label for="firstname">First Name</label>
      <input type="text" id="firstname" placeholder="Your first name..." required>
      <label for="lastname">Last Name</label>
      <input type="text" id="lastname" placeholder="Your last name..." required>
      <label for="email">Email Address</label>
      <input type="email" id="email" placeholder="Your email..." required>
      <label for="message">Message</label>
      <textarea id="message" name="message" placeholder="Write your message..." style="height:170px" required></textarea>
      <input type="submit" name="submit" value="Submit">
    </form>
  </div>
</div>
  </div>
</section>
</html>

<?php
  $firstname = $_POST['firstname']
  $lastname = $_POST['lastname']
  $user_email = $_POST['email'];
  $message = $_POST['message']

  $email_from = 'myemail@domain.com'

  $email_subject = "New Form Submission from $firstname.\n";

  $email_body = "First Name: $firstname.\n".
                  "Last Name: $lastname.\n".
                    "Email: $user_email.\n".
                      "Message: $message.\n";

  $to = "myemail@domain.com";

  $headers = "From: $email_from \r\n";
  $headers .= "Reply To: $user_email \r\n";

  mail($to,$email_subject,$email_body,$headers);

  header("Location: contact.html");

?>

Τhe HTML file name is contact.html and the PHP file is contactform.php

The form doesn't submit and I don't receive an email. Am I doing something wrong? Do I need to setup something else?

Thanks in advance,

Thomas

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Thomas
  • 37
  • 1
  • 1
  • 6
  • Have you checked the browser console for errors? Have you checked the web server log for errors? Is the mail service setup on the server to send mail? – Jason K Sep 10 '19 at 16:59

2 Answers2

1

Guessing you are getting undefined index errors and your environment is not set to display errors. I would add the name attribute to the input fields that are missing it as a start.

For example the first name field

 <input type="text" id="firstname" placeholder="Your first name..." required>

should be

 <input type="text" name="firstname" id="firstname" placeholder="Your first name..." required>

In addition you should look at turning errors on or viewing the log file.

cOle2
  • 4,725
  • 1
  • 24
  • 26
  • I turned on display errors and added the name like you suggested. The display errors allowed me to find that I missed a few ”;”. Now I no longer get a blank screen but instead it sends me back to the contact page like I specified in the PHP code. But I don’t receive the email. Am I still doing something wrong? My code is the same as before except with ; added were there aren’t and I added the name in the HTML. – Thomas Sep 10 '19 at 18:45
  • @Thomas I took a look at your hosting provider and it seems they limit what can be done with PHP's mail() function (see https://infinityfree.net/support/php-mail/). My guess is that is why you don't receive the email. If you have a GMail account you can use their instruction for setting that up, the code will not be much different than what you already have. – cOle2 Sep 10 '19 at 19:14
0

Due to PHP parse error, you are getting blank page. You need to use semicolon(;) in PHP, in order to end a statement. For example, instead of, $firstname = $_POST['firstname'] it should be, $firstname = $_POST['firstname'];

Rahul K
  • 11
  • 4