-2

i have created a form with a PHP file, this is for a booking request and the aim is to get the variables sent to an email address. But for some reason the variables.

Below is the code for my html arm:

<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="number">Phone Number</label>
            <input type="number" id="number" name="number">
            </fieldset>
            <fieldset>
            <label for="email">Email:</label>
            <input type="email" id="email" name="user_email">
            <label for="pickup">Pick Me Up From</label>
            <input type="text" id="pickup" name="pickup">
            <label for="date">Date</label>
            <input type="text" id="datepicker" name="date">
            <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>

and now my php code for this form is as follows

<?php

$to = 'test@gmail.com';
$subject = 'Booking';

$name = $_POST['name'];
$email = $_POST['email'];
$pickup = $_POST['pickup'];
$destination = $_POST['destination'];
$date = $_POST['date'];


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

$header = $email;

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


echo "Message Sent";

?>

Now, when i receive the email i can see only $destination gets picked up. Have i done something wrong in my html, it looks fine to me. Also, how do i get my select elements as a variable.

4 Answers4

0

The form fields you want to capture the value of must all have name attributes and they should match what you use in PHP. For example $_POST['user_email'] not $_POST['email']. Note that none of your <select> elements have the name attribute.

j08691
  • 204,283
  • 31
  • 260
  • 272
0

If destination already gets through, you do most likely have some mistake in the naming of the name attribute. Make sure it matches 1:1 the names you try to get in your PHP.

Secondary, regarding your select, you can get the value via the name attribute the same way you get all the text elements. Inputs, in whatever form (except of files), are just strings which do get through the protocol. It does not make any difference for PHP if the UI is different.

Astinox
  • 154
  • 1
  • 10
0
<select class="form-control" name="hour">
   <option>--HR--</option>
   <option value="1">01</option>
   <option value="2">02</option>
   <option value="3">03</option>

</select>
<label>:</label>
<select class="form-control" name="minutes">
   <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 class="form-control" name="time_of_day">
   <option value="am">am</option>
   <option value="pm">pm</option>
</select>

in php:

//grab the value the user select for hour from the $_POST, which is an array which stores all values submitted via the post method 

$hour = $_POST['hour'];
$minutes = $_POST['minutes'];
$time_of_day = $_POST['time_of_day'];
//concate your variable to output what the user selected so it would out put something like 4:30 pm
$time = $hour.':'.$minutes.''.$time_of_day;

Essentially, you are adding the name attribute to the select statement like what you did for you inputs and you are accessing them the same way via php. if you do die(var_dump($_POST)); you will see have thing that has been submitted. If you do not see it in the dump then you either know you need to revisit the code

Parker Dell
  • 472
  • 4
  • 11
0

HTML

You omitted the name attributes in the select options. I added hour, min and periods. Also you had user_email value in the name attribute for email input field.

In the PHP code, i've assigned concatenated and assigned the POST values for the time and updated the message

 <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
        <fieldset>
            <legend>Book your Journey</legend>
            <label for ="name">Name:</label>
            <input type="text" id="name" name="name">
            <label for="number">Phone Number</label>
            <input type="number" id="number" name="number">
            </fieldset>
            <fieldset>
            <label for="email">Email:</label>
            <input type="email" id="email" name="user_email">
            <label for="pickup">Pick Me Up From</label>
            <input type="text" id="pickup" name="pickup">
            <label for="date">Date</label>
            <input type="text" id="datepicker" name="date">
            <label>Time:</label>
            <select name="hour">
                <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 name="min">
                <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 name="period">
                <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>

PHP

<?php
        $to = 'test@gmail.com';
    $subject = 'Booking';

    $name = $_POST['name'];
    $phone = $_POST['number'];
    $email = $_POST['user_email'];
    $pickup = $_POST['pickup'];
    $destination = $_POST['destination'];
    $date = $_POST['date'];
    $time = $_POST['hour'].':'.$_POST['min'].''.$_POST['period'];
echo "Message Sent";

?>

Hope this helps

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Oluwaseye
  • 685
  • 8
  • 20