Chris Zimmerman Chris Zimmerman - 1 month ago 4
HTML Question

My HTML form only returns 1 value into the PHP mail

The email I receive only shows the message, not the email address or phone number or even the name.

It also doesn't show from which email it has been sent.

I should receive whole information from the form, otherwise it wouldn't show me the message, I guess.

Maybe someone has more simple code than this one?

I tried but I'm stuck.

Can someone help me out?

Below is the HTML and PHP code

HTML Code:

<form name="mail" action="mail.php" method="post" >
<div class="form-group ">
<input id="name" class="form-control" placeholder="Name" type="text" required>
<label for="name" class="sr-only">Name</label>

</div>
<div class="form-group ">
<label for="email" class="sr-only">Email</label>
<input id="email" class="form-control" placeholder="Email" type="email" required>
</div>
<div class="form-group ">
<label for="phone" class="sr-only">Phone</label>
<input id="phone" class="form-control" placeholder="Phone" type="text" required>
</div>
<div class="form-group ">
<label for="message" class="sr-only">Message</label>
<textarea name="message" id="message" cols="30" rows="5" class="form-control" placeholder="Message" required></textarea>
</div>
<div class="form-group ">
<input class="btn btn-primary btn-lg" value="Send Message" name="submit" type="submit">
</div>
</div>
</form>


PHP code:



<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];



$email_from = "$email";//<== update the email address
$email_subject = "New Form submission";
$email_phone = $phone;
$email_body = "You have received a new message from the user ".$name.
"\nHere is the message:\n ".$message.
"\nThe phone number:\n ".$phone.
"The email:\n ".$email.

$to = "info@czwebdesign.be";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: index.html');



?>




Answer

Look here at the missing name="" attribute in your <input>
Use this code instead:

<form name="mail"  action="mail.php" method="post" >
                <div class="form-group ">
                    <input name="name" id="name" class="form-control" placeholder="Name" type="text" required>
                    <label for="name" class="sr-only">Name</label>
                </div>
                <div class="form-group ">
                    <label for="email" class="sr-only">Email</label>
                    <input name="email" id="email" class="form-control" placeholder="Email" type="email" required>
                </div>
                <div class="form-group ">
                    <label for="phone" class="sr-only">Phone</label>
                    <input name="phone" id="phone" class="form-control" placeholder="Phone" type="text" required>
                </div>
                <div class="form-group ">
                    <label for="message" class="sr-only">Message</label>
                    <textarea name="message" id="message" cols="30" rows="5" class="form-control" placeholder="Message" required></textarea>
                </div>
                <div class="form-group ">
                    <input class="btn btn-primary btn-lg" value="Send Message" name="submit" type="submit">
                </div>
                </div>
                </form>