Only send the data of the fields filled by email using PHP

-1

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.

    
asked by anonymous 26.10.2018 / 11:28

2 answers

1

Problem solved. Here is the PHP code that performs the function described in the post doubt:

<?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'];
    $val_fn = $first_name != '' ? 'Nome: '.$first_name.' '."\n\n" : '';
    $val_ln = $last_name != '' ? 'Sobrenome: '.$last_name.' ' : '';
    $subject = "Form submission";
    $subject2 = "Copy of your form submission";
    $message = $val_fn . $val_ln . "escreveu a seguinte mensagem" . "\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.
    }
?>

I followed the example of ternary if (condition) that @ Gilmar Alonso suggested, and I modified it, as it was not returning the way I wanted.

I used the variables $ val_fn and $ val_ln to validate using ternary in the variables that receive the value of the first and last name fields, in the case $ first_name and $ last_name.

And then, I simply put the validation variables inside the $ message variable and build my email message.

Now, from this, I can build my form with food, as I had commented in the description of the post. And receive the email in the appropriate way, as you would like to receive.

    
27.10.2018 / 11:01
0

You can use a ternary IF. receive the posts normally, which will come empty, and mount the message with the following structure!

$message = $first_name != '' ? 'Nome: '.$first_name : '' . " " . $last_name != '' ? 'Sobrenome: '.$last_name. " wrote the following:" . "\n\n" . 
    
26.10.2018 / 14:09