Basic PHP for EMAIL FORM?

145 views Asked by At

This is my first time here and also first time for PHP too. I only have few knowledge about html and zero knowledge about PHP.

I'm trying to set up a website from a free template. Everything is ok except the sending email part in the contact us form.

<form action="send_email.php" method="post">

                                                <div class="form-group">
                                                    <!--<label for="contact_name">Name:</label>-->
                                                    <input type="text" id="contact_name" class="form-control" placeholder="Name" />
                                                </div>
                                                <div class="form-group">
                                                    <!--<label for="contact_email">Email:</label>-->
                                                    <input type="text" id="contact_email" class="form-control" placeholder="Email Address" />
                                                </div>
                                                <div class="form-group">
                                                    <!--<label for="contact_message">Message:</label>-->
                                                    <textarea id="contact_message" class="form-control" rows="9" placeholder="Write a message"></textarea>
                                                </div>
                                                <button type="submit" class="btn btn-primary">Send</button>

                                        </form>

The template builder said that I have to create my own send_email.php file and I tried my best to learn about it and this is what I found from google which is a guide of how to create the send_email.php

<?php


$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));



$Name = $_POST['Name'];

$Email = $_POST['Email Address:']; // This is the name= from the form

$Message = $_POST['Write a message:'];

$sendthis="Name ".$Name."// This compiles all info into a single string
Email Address ".$Email."
Write a message: ".$Message."
";
$sendthis = stripslashes($sendthis);



mail("[email protected]","Email sent via www.mywebsite.com",$sendthis,"From: customer");


header("Refresh: 0;url=http://mywebsite.com");
?>

Well, when I tried to test the email. I did got the email, however, there is nothing in it. There is only

Name // This compiles all info into a single string
Email Address
Write a message:

There are no name or anything that I typed in the form.

So, could you please tell me where is the problem of the code? I tried to solve it for hours but nothing works.

Thank you in advance. Now I really have no clue how to fix the problem.

1

There are 1 answers

6
AudioBubble On

You need to put name attributes inside your html :

<form action="send_email.php" method="post">
    <div class="form-group">
        <input type="text" name="contact_name" id="contact_name" class="form-control" placeholder="Name" />
    </div>
    <div class="form-group">
        <!--<label for="contact_email">Email:</label>-->
        <input type="text" name="contact_email" id="contact_email" class="form-control" placeholder="Email Address" />
    </div>
    <div class="form-group">
        <textarea name="contact_message" id="contact_message" class="form-control" rows="9" placeholder="Write a message"></textarea>
    </div>
    <button type="submit" class="btn btn-primary">Send</button>
</form>

and then you'll get them in your $_POST array :

$Name = $_POST['contact_name'];

$Email = $_POST['contact_email'];

$Message = $_POST['contact_message'];

As @Dagon said, you can even directly write :

$sendthis = "Name " . $_POST['contact_name'] . "\n"
            . "Email Address " . $_POST['contact_email'] . "\n"
            . "Write a message: " . $_POST['contact_message'] . "\n";

Also just an advice : format correctly your code, especially variable names. It's easier to take good habits when you learn than later :)