0

I have extremely little knowledge of PHP and was trying to make a contact form for the website I'm helping to develop. The PHP form sends the email but for whatever reason doesn't replace the variables with the input fields from the contact page. The website is SWFDA. Here is the PHP form I created:

<?php

if(!isset($_POST['submit'])) {

$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

$email_from = 'myemail@website.com';

$email_to = 'myemail@website.com';

$email_subject = 'Contact Form Submission (Under testing)';
$email_body = 'You have received a form submission from $name .
Email: $visitor_email .
Here is the message: $message'.;

mail($email_to,$email_subject,$email_body);

header('Location: contact-sent.html');

}

?>

This is the HTML for the form.

<form method="post" action="contactForm.php">
    <div class="row uniform">
        <div class="6u 12u$(large) 6u(medium) 12u$(xsmall)">
            <label for="name">Name</label>
            <input type="text" name="name" id="name" required/>
        </div>
        <div class="6u$ 12u$(large) 6u$(medium) 12u$(xsmall)">
            <label for="email">Email</label>
            <input type="email" name="email" id="email" required/>
        </div>
        <div class="12u$">
            <label for="message">Message</label>
            <textarea name="message" id="message" rows="5" required></textarea>
        </div>
        <div class="12u$">
            <ul class="actions">
                <li><input type="submit" value="Send Message" class="special" /></li>
                <li><input type="reset" value="Reset" /></li>
            </ul>
        </div>
    </div>
</form>

The received email looks like:

You have received a form submission from $name.
Email: $visitor_email.
Here is the message: $message.

Steven W.
  • 3
  • 3
  • 5
    You just need to switch your single quotes to double! Just read this [SO question](http://stackoverflow.com/q/3446216/5447994). So your email body should have double quotes – Thamilhan Jan 20 '17 at 13:02
  • I'll give it a try. Update: It worked. Thanks! – Steven W. Jan 20 '17 at 13:13

2 Answers2

0

Try it like this,

$email_body = 'You have received a form submission from '.$name.'
Email: '.$visitor_email.'
Here is the message: '.$message;

or use double quote " "

$email_body = "You have received a form submission from $name
Email: $visitor_email
Here is the message: $message";
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
0

Just change single quotes(') to double quotes(") in the $email_body then your email should look like this :

<?php

if(!isset($_POST['submit'])) {

$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

$email_from = 'myemail@website.com';

$email_to = 'myemail@website.com';

$email_subject = 'Contact Form Submission (Under testing)';
$email_body = "You have received a form submission from $name .
Email: $visitor_email .
Here is the message: $message".;

mail($email_to,$email_subject,$email_body);

header("Location: contact-sent.html");

}

?>
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34