Send only the data of the fields completed by e-mail using PHP

Asked

Viewed 138 times

-1

I have an HTML form, and I’m using a PHP code to send the data via email. However, I would like to send by e-mail only the data of the fields filled, and their respective names.

Example:

[ ] Nuggets
[3] Hot Dog
[1] Cheese Burger
[ ] Pizza
[ ] Salad

The email flips with all text field, including items that have no marked drive, and would like it to come as follows:

[3] Hot Dog
[1] Cheese Burger

I am using the code below to send the email with the fields (which in 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.
    }
?>

Everything is working, I have customized the code above to put the fields I really need in the form. I believe it is necessary to use IF, in this PHP file, but I am not sure and do not know how to do it.

I apologize if the post code was put wrong, but it’s my first time on the forum. (yes I read the instructions)

From now on, thank you.

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 commented initially, the example cited does not match the basic 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, second name, email. If you just fill out your first name, I don’t want you to come in for example:

Name: Mário
Middle Name:
E-mail:

I want you to show up:

Name: Mário

The example was with food, because it will be used with food.

  • If possible click on [Edit] and post your HTML code.

  • The post has already been edited and added the HTML code.

2 answers

1


Problem solved. Below 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 if(condition) ternary that @Gilmar Alonso suggested, and modified, as it was not returning in the desired way.

I used the variables $val_fn and $val_ln to validate using ternary in the variables that receive the value of the fields of name and surname, 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 proper way, as I would like to receive.

0

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

$message = $first_name != '' ? 'Nome: '.$first_name : '' . " " . $last_name != '' ? 'Sobrenome: '.$last_name. " wrote the following:" . "\n\n" . 
  • 1

    Thanks @Gilmar Alonso. I was able to understand the code you suggested. But it is not returning in the right way. If I write the name, it does not appear in the email nor even the text "Name:" before, and the message does not appear. If I write the last name, it appears, if I do not write, only the text "Last name:" appears. And in none of the last name options the message appears. .

  • 1

    $message = $first_name != '' ? 'Nome: '.$first_name : '' . $last_name != '' ? 'Sobrenome: '.$last_name : '' . " wrote the following:" . "\n\n" . $_POST['message'];

  • 1

    I would like to thank you for your help, by example. I have no knowledge in PHP, but I have used other languages, and I have a basis of programming logic, which allowed me to understand the code, reason and develop in the correct way that could suit me.

  • 1

    Someone, unfortunately, and I believe it was not you @Gilmar Alonso, did not have patience, negatived my post as "the question is not clear or not useful". It is complicated to enter a programming language site, where people have a vast knowledge, and are not able to understand a simple question, while they are able to perform complex code. I received the same kind of reaction from the forum, in English. And this is a question that I saw some people have also, during my research, only that they did not succeed.

  • 1

    Once again I thank you for your help and continue to have the patience to help and understand the doubts of others, since you have the knowledge to do so. I hope the post can help other people as well. Thanks.

  • These people are complicated, they think that if it’s not their doubts, no one has the right to ask!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.