I am similar to php, I don’t understand where the problem lies.
Sometimes php functions send me empty messages like
Parent’s Name
too late:
Lynn:
telephone number:
e-mail:
date of birth:
Message text:
But it should be filled with values like this
Parent Name Test
Too Many Mistakes: Test
Lynn: Test
Phone Number: Test
Email: test@test
Date of birth: 21313
Message text: Test
This is my php code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Обратная Связь</title> </head> <body> <?php if (isset($_POST['parent'])) {$parent = $_POST['parent'];} if (isset($_POST['child'])) {$child = $_POST['child'];} if (isset($_POST['contacts'])) {$contacts = $_POST['contacts'];} if (isset($_POST['email'])) {$email = $_POST['email'];} if (isset($_POST['bbd'])) {$bbd = $_POST['bbd'];} if (isset($_POST['city'])) {$city = $_POST['city'];} if (isset($_POST['mess'])) {$mess = $_POST['mess'];} $to = "info@test.ee"; /*Укажите ваш адрес электоронной почты*/ $headers = "Content-type: text/plain; text/html; charset=utf-8"; $subject = "Kontakti Info"; $message = "Vanema nimi $parent \n Lapse nimi: $child \nLinn: $city \nTelefoninumber: $contacts \nEmail: $email \nSünnikuupäev: $bbd \nSõnumi tekst: $mess"; $send = mail ($to, $subject, $message, $headers); if ($send == 'true') { echo "<b>Спасибо за отправку вашего сообщения!<p>"; echo "<a href=index.php>Нажмите,</a> чтобы вернуться на главную страницу"; } else { echo "<p><b>Ошибка. Сообщение не отправлено!"; } ?> </body> </html> <?php header('Location: https://test.ee/aitah.html '); ?>
Please give me advice, what went wrong.
If your script is just a form handler, you can e.g. add
if(empty($_POST)) { die('No form data!'); }
to the top to prevent it from running, Unless the response form is submitted.If you need to fill in all fields, you must check each field before processing the email. You could cram all of these
isset
into one giantif(isset(...)
statement. However, there is a simpler, more readable way to do it .First, let’s set up a few variables:We then loop over the fields and if the value exists, add it to
$data
, otherwise we add an error comment.If an error occurs (= missing field), the email will not be sent. As a bonus, you now have a reusable piece of code that can be used in other forms with similar functionality by simply modifying the
$fields
array. (If you really need to reuse it, it's a good idea to wrap it into a function; don't copy-paste the code.function x($post, $fields) { ... }
Used for basic manipulation helpers function.)Please note that here we use
Side note If the above code is complete and your script just handles form data and redirects, then you don't need the HTML wrapper at all. It never displays.empty
instead ofisset
. If a blank form is submitted, field will be set (to the empty string""
). Also note thatempty
returnstrue
for anything equal tofalse
(i.e.""
,0
,false
, null,[]
). ( If "0" is an expected and acceptable value, please note its "emptiness"! ) On the other hand,isset
for anything that is not # The content of ##nullreturns true.