Friday, 31 August 2018

My PHP code does not include variables when using the messaging feature

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 = 'test@email.com';
    $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>


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'];

0 comments:

Post a Comment