I have an HTML form, and I'm using PHP code to send the data via email. However, I would like to e-mail only the data of the filled fields, and their names.
Example:
[] Nuggets
[3] Hot Dog
[1] Cheese Burger
[] Pizza
[] Salad
The email will come with all the text fields, including the items that have a marked unit, and would like it to come as follows:
[3] Hot Dog
[1] Cheese Burger
I'm using the code below to send the email with the fields (which in the case the fields do not match the ones above, was just an example):
<?php
if(isset($_POST['submit'])){
$to = "[email protected]"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
It's all working, I've customized the code above to put the fields I really need on the form. I believe it is necessary to use IF in this file in PHP, but I'm not sure and I have no idea how to do it.
I apologize if the post code was put in the wrong way, but it's my first time on the forum. (yes I read the instructions)
Thanks in advance.
PS: HTML code
<html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="mail_haddor.php" method="post">
First Name: <input type="text" name="first_name" size="40"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
As I mentioned earlier, the above example does not match the base code I used. In this case, it would be like, send and appear in the body of the email, only the fields filled as first name, middle name, email. If you fill in the first name only, I do not want it to come for example:
Name: Mário
Second Name:
E-mail:
I want it to appear:
Name: Mario's
The example was with food as it will be used with food.