0

My PHP code is not picking up the variables in $message, when $message is sent to an email address using the mail function the variables are missing. Not sure what I am doing wrong.

    <?php

    $to = '[email protected]';
    $subject = 'Booking';

    $name = $_post['name'];
    $email = $_post['email'];
    $pickup = $_post['pickup'];
    $destination = $_post['destination'];

    $message = <<<Email
    Hi,
    Please pick up $name from $pickup and drop to $destination
    Email;

    $header = $email;


    mail($to, $subject, $message, $header);

    echo "Message Sent";

    ?>

The html code is provided below, not sure why $_POST is not pulling through.

<!DOCTYPE html>
<html>
<head>
    <title>Sign Up</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>

    <form action="form.php" method="POST">
        <fieldset>
            <legend>Book your Journey</legend>
            <label for ="name">Name:</label>
            <input type="text" id="name" name="name">
            <label for="email">Email:</label>
            <input type="email" id="email" name="email">
            <label for="pickup"> Pick Up Location</label>
            <input type="text" id="pickup" name="pickup">
            <label>Time:</label>
            <select>
                <option>--HR--</option>
                <option value="1">01</option>
                <option value="2">02</option>
                <option value="3">03</option>
                <option value="4">04</option>
                <option value="5">05</option>
                <option value="6">06</option>
                <option value="7">07</option>
                <option value="8">08</option>
                <option value="9">09</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>
            </select>
            <label>:</label>
            <select>
                <option>--MIN--</option>
                <option value="00">00</option>
                <option value="15">15</option>
                <option value="30">30</option>
                <option value="45">45</option>
            </select>
            <select>
                <option value="am">am</option>
                <option value="pm">pm</option>
            </select>
            </fieldset>
            <fieldset>
            <label for="destination"> Destination Postal Code</label>
            <input type="text" id="destination" name="destination">
        </fieldset>
        <button>Submit</button>
    </form>

</body>
</html>
4
  • Does your email say Hi, Please pick up $name from $pickup and drop to $destination or Hi, Please pick up from and drop to? Commented Jul 4, 2017 at 19:59
  • 5
    I might be making something up right now, but I'm pretty sure it's $_POST and not $_post Commented Jul 4, 2017 at 20:00
  • Ah no, @Jhecht has figured it out. Commented Jul 4, 2017 at 20:01
  • why the line $header = $email; ? The header should have something like From or Reply-To have before a mail address Commented Jul 5, 2017 at 0:49

3 Answers 3

4

The problem isn't necessarily that $message isn't picking up the variables. Rather, the problem is that $name, $email, etc. are all blank values.

PHP's variables are case sensitive, and $_POST should be all upper case.

You can do a test by outputting your $email, $name, etc. and you'll see that they're blank.

To fix it, all you need to do is capitalize $_POST:

$name = $_POST['name'];
$email = $_POST['email'];
$pickup = $_POST['pickup'];
$destination = $_POST['destination'];
Sign up to request clarification or add additional context in comments.

3 Comments

@YasserHussain Perhaps the form element name has a typo in it, or something along those lines. Can you output $_POST (eg print '<pre>'.print_r($_POST, true).'</pre>';), to make sure that the key (pickup) is being passed to your script from your form? If you don't see an obvious problem from the output of your $_POST, can you post that output here for us to take a look at? Also, I would suggest editing out your email address when posting online.
it couldn't find location using $_POST['location'], i have added the html code to the original question, to me it looks fine :|
@YasserHussain Thanks for sharing your html. I just tested it, and I was able to get a result in $_POST['pickup'] without a problem. Do you mind posting your print_r($_POST) result here?
1

Case sensitive ...

I also recommend using:filter_input_array();

You can use it as follows:

<?php
$input = filter_input_array(INPUT_POST);

$to      = '[email protected]';
$subject = 'Booking';

$name        = $input['name'];
$email       = $input['email'];
$pickup      = $input['pickup'];
$destination = $input['destination'];

You can also use it with the GET method as follows:

filter_input_array (INPUT_GET);

The use of ?> is not necessary.

Comments

0

post always in upper case.$_POST['variable name'] then try it.

try this after $_POST implementation not working:

$message = <<<EOD
    Hi,
    Please pick up {$name}from {pickup} and drop to {$destination}
    EOD;

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.