The feedback form doesn't work

Hello! There is a landing page on the test domain so far http://elenadva.beget.tech/ At the moment, the feedback form does not work there. No messages are sent (nothing happens when the button is clicked). Please help me. The server works on Open, but not on hosting. Form code in index.php:

                   <form role="form" id="contact" action="php/mail.php" method="post">
                    <div class="row">
                        <div class="col-md-4">
                            <input type="text" class="form-control" id="name" name="name" placeholder="Ваши ФИО или название организации">
                            <input type="email" class="form-control" id="email" name="email" placeholder="Email">
                            <input type="text" class="form-control" id="subject" name="subject" placeholder="Курс">
                        </div>


                        <div class="col-md-8">
                            <textarea class="form-control" id="message" rows="25" cols="10"
							placeholder="Текст сообщения"></textarea>
                            <input class="btn btn-default submit-btn form_submit" type="submit" name="submit" value="Записаться на курс"></input>
                        </div>
                    </div>
                </form>

Code mail.php:

<?php
$post = (!empty($_POST)) ? true : false;
if($post) {
	$name = $_POST['name'];
	$email = $_POST['email'];
	$subject = $_POST['subject'];
	$message = $_POST['message'];
	$error = '';
	if(!$name) {$error .= 'Укажите свое имя. ';}
	if(!$email) {$error .= 'Укажите электронную почту. ';}
	if(!$subject) {$error .= 'Укажите интересующий Вас курс. ';}
	if(!$message || strlen($message) < 1) {$error .= 'Введите сообщение. ';}
	if(!$error) {
		$address = "[email protected]";
		$mes = "Имя: ".$name."\n\nТема: " .$subject."\n\nСообщение: ".$message."\n\n";
		$send = mail ($address,$subject,$mes,"Content-type:text/plain; charset = UTF-8\r\nFrom:$email");
		if($send) {echo 'OK';}
	}
	else {echo '<div class="err">'.$error.'</div>';}
}
?>

Js script code:

jQuery(document).ready(function($) {
	$("#contact").submit(function() {
		var str = $(this).serialize();
		$.ajax({
			type: "POST",
			url: "php/mail.php",
			data: str,
			success: function(msg) {
				if(msg == 'OK') {
					result = '<div class="ok">Ваше сообщение отправлено</div>';
					$("#fields").hide();
				}
				else {result = msg;}
				$('#note').html(result);
			}
		});
		return false;
	});
});
Author: Elena, 2017-10-22

1 answers

You don't have the form id that you have js hanging on to. Replace the first line of the form with this

<form role="form" id="contact" action="php/mail.php" method="post">

And also replace the button with this one (see below). You don't have it submit. And js hangs exactly on sending.

<input type="submit" class="btn btn-default submit-btn form_submit" value="Записаться на курс">

Also, add the name attribute to each input and textarea. It is with this name (which is specified in the attribute name) that the data will come to the server in the array $_POST. For example:

<input type="email" name='email' class="form-control" id="email" placeholder="Email">

So in the array $_POST there will be an entry with the key email from name and the value, which was entered in this input.

Php code

$errors=array(
    'name' => 'Укажите свое имя.',
    'email' => 'Укажите электронную почту.',
    'subject' => 'Укажите интересующий Вас курс.',
    'message' => 'Введите сообщение.',
);

foreach ($errors as $variable => $error_text) {
    if(!isset($_POST[$variable]) or empty($_POST[$variable])) exit('<div class="err">'.$error_text.'</div>');
}

extract($_POST);

$address = "[email protected]";
$mes = "Имя: ".$name."\n\nТема: " .$subject."\n\nСообщение: ".$message."\n\n";
$send = mail($address, $subject, $mes, "Content-type:text/plain; charset = UTF-8\r\nFrom:$email");
if($send) echo 'OK';
 2
Author: n.osennij, 2017-10-25 18:54:39