-1

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("myemail@gmail.com","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.

kit
  • 13
  • 3
  • you're missing all the `name=""` attributes from your html. [you should probably read this](http://php.net/manual/en/tutorial.forms.php) – castis Jun 10 '15 at 21:19
  • input fields and textarea require a `name` attribute –  Jun 10 '15 at 21:20
  • [What to use when coding blindly...](http://php.net/manual/en/function.error-reporting.php) and [dealing with forms...](http://php.net/manual/en/tutorial.forms.php) – Funk Forty Niner Jun 10 '15 at 21:25

1 Answers1

2

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 :)

  • 1
    you can also skip the variable reassignment. –  Jun 10 '15 at 21:23
  • Yes. Common mistake. `id` is for uniquely identifying the element in the DOM (for styling and JavaScript), while `name` is used for posting input values. – GolezTrol Jun 10 '15 at 21:23
  • @GolezTrol he as using the placeholder not id :( –  Jun 10 '15 at 21:25
  • @Dagon? You mean you don't have to assign it to $Name and $Email? That's true, but I think it makes your code much more readable if you read the post array once, do any typecasting, converting and validating, and work with the usable, readable variables from that point on. – GolezTrol Jun 10 '15 at 21:25
  • i disagree - and it doubles the memory foot print –  Jun 10 '15 at 21:25
  • A name, an e-mail address and a message? A couple of KB maybe? PHP's default memory limit is 128**M**B. Not that you should use all of that, but to put it in perspective. Besides, if you are scared, you can unset the $_POST array after you've parsed its contents. – GolezTrol Jun 10 '15 at 21:26