0

I'm having some trouble with a basic mail PHP script. I can get the email to send, but can't get the variables from the html form.

Here is the code, any ideas?

<form action='sendmail.php' method="POST">

    <label for="name">Name</label>
    <input type="text" name="name" id="name" />

    <label for="email">Email</label>
    <input type="email" name="email" id="email" />

    <label for="email">Phone Number</label>
    <input type="number" name="number" id="number" />

    <label for="comments">Message</label>
    <textarea id="comments" name="message"></textarea>

    <input type="submit">
</form>

And here is the PHP file

<?php

$name = $_POST["name"];
$email_from = $_POST["email"];
$phone = $_POST["number"];
$message = $_POST["message"];


$to      = '[email protected]';
$subject = 'Contact Form Message';
$message = "Name: ". $name  . "\r\nPhone: " . $phone . "\r\nMessage: " . $message;
$headers = "From: " . $email_from . "\r\n" .
    "Reply-To: " . $email_from . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


mail($to, $subject, $message, $headers);
?>
2
  • 1
    method="POST" and $_GET["name"]? Might want to review that again. Commented Apr 19, 2017 at 4:56
  • You are using the "POST" method and using superglobals $_GET to get the post data that's wrong. if you want to get the post data use superglobals $_POST or otherwise use $_REQUEST to get the value. Commented Apr 19, 2017 at 4:59

4 Answers 4

3

You are using POST method to send form data to PHP. Use $_POST to get the values.

$name = $_POST["name"];
$email_from = $_POST["email"];
$phone = $_POST["number"];
$message = $_POST["message"];
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I was experimenting before I copied and pasted. I have used, $_POST, $_GET and $_REQUEST and all I am getting in the email is:
0

Either change method when posting your form

<form action='sendmail.php' method="GET">

Or

change the variables

$name = $_POST["name"];
$email_from = $_POST["email"];
$phone = $_POST["number"];
$message = $_POST["message"];

Comments

0

You can use $_POST or $_REQUEST to fetch values. $_REQUEST will be the best option because it works for both get and post methods.

$name = $_REQUEST["name"];
$email_from = $_REQUEST["email"];
$phone = $_REQUEST["number"];
$message = $_REQUEST["message"];

Comments

0

U should change your method to 'GET'. Or u should change the variables. The answers above all work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.