M
M
Mixail012020-02-11 22:10:55
JavaScript
Mixail01, 2020-02-11 22:10:55

How to auto collect data from all fields from the form in mail.php and send via phpmailer?

There are 2 forms that have a different number of fields, but also have the same inputs by name.
Sending is implemented through a script and using phpmailer:

$(document).ready(function() {

//E-mail Ajax Send
$("form").submit(function a() { //Change
var th = $(this );
$.ajax({
type: "POST",
url: "core/components/mail.php", //Change
data: th.serialize()
}).done(function() {
setTimeout(function() {
// Done Functions
th.trigger("reset");
}, 1000);
});
return false;
});

});

The data is pulled out from the fields and pushed into the starting table, this is 1 form

$number = $_POST['NumberOfOperators'];
$name = $_POST['Name'];
$namecom = $_POST['NameCompany'];
$phone = $_POST['Phone'];
$email = $_POST['Email'];

$html = "

Number of managers (operators): {$_POST['NumberOfOperators']}
Name : {$_POST['Name']}
Name of the company : {$_POST['NameCompany']}
Telephone : {$_POST['Phone']}
Email : {$_POST['Email']}
";

And 2

$name = $_POST['Name'];
$phone = $_POST['Phone'];
$email = $_POST['Email'];
$message = $_POST['Message'];

$html = "
Name : {$_POST['Name']}
Telephone : {$_POST['Phone']}
Email : {$_POST['Email']}
Message : {$_POST['Message']}
";
If, when using 1 form template, then either not all fields or empty ones will be sent, and with 2.
How best to make the body of the letter be formed according to the number of fields when using 2 forms.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sashabeep, 2020-02-11
@sashabeep

$phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
.....
if($name){ $message = "<b>Имя:</b> ".$name."<br>"; }				
if($phone){ $message .= "<b>Телефон:</b> ".$phone."<br>"; }
if($email){ $message .= "<b>e-mail:</b> ".$email."<br>"; }
.....
mail($to, $subject, $message, $headers);......

The phpmailer example describes itself well enough if there is a need to send through it.
You always have the name and phone number, this is enough to send

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question