There is an ajax request:

$(function() { $('#backmail').submit(function(e) { var $form = $(this); var button = $('#backmail button[type="submit"]') $.ajax({ type: 'POST', url: 'backmail.php', data: $form.serialize(), }).done(function() { $form.slideUp('slow', function(){ $(this).remove(); }); $('#success-highlight').show(); }).fail(function() { $form.slideUp('slow', function(){ $(this).remove(); }); $('#error-highlight').show(); }); e.preventDefault(); }); }); 

And there is a PHP script that receives and sends this data to me by mail:

 <? // config $adminemail="andrewdymov@gmail.com"; $date=date("dmy"); $time=date("H:i"); // confirm data $name = $_POST['name']; $email = $_POST['mail']; $subject = $_POST['subject']; $message = $_POST['message']; if (!preg_match("/.+@.+\..+/i", strtolower($email))) { header("HTTP/1.1 500 Internal Server Error"); } else { $msg="<p>Отправлено: $time $date</p> <p>Имя: $name</p> <p>E-mail: $email</p> <p>Тема: $subject</p> <p>Сообщение: $message</p>"; mail("$adminemail", "$subject", "$message"); } ?> 

The essence of the problem lies in the fact that no matter what abru-cadabra I wrote in the email field, the data to the php file is still sent successfully and the php script returns code 200, although I should have returned 500 to me. I did something not this way?

    1 answer 1

    Two options are the problem:

    1. The php file is saved with the UTF-8 BOM (or there is a space before the <? ) + Errors are turned off. Headers are sent via header only if nothing is output in front of them (including the UTF-8 BOM). Try to turn on the error (if they are turned off) and see.

    2. In the regular expression there is no ^ and $, respectively, it is enough that the regular expression matches only part of the string. For example, it will take any line that has "@tt", for example, "test test test @tt". Perhaps you have tested only variants similar to E-mail, since just the "test" will not accept.

    • I am very surprised that after <? php was not written and the script because of this did not work. Finished and everything worked fine. - JamesJGoodwin