A
A
Andrey Filimonov2017-04-17 23:26:09
PHP
Andrey Filimonov, 2017-04-17 23:26:09

How to correctly set up sending data from several different forms on one page?

Hello,
I set up sending data from the form on the html page :

<form method="post" action="javascript:void(0);" onsubmit="send_form();" id="forma">
<input id="name1" name="fio" placeholder="Ф.И.О" />
<input id="phone1" type="text" name="phone" placeholder="Телефон" />
<input id="age1" type="text" name="age" placeholder="Возраст девочки" />
<input id="adress1" type="text" name="adress" placeholder="Адрес зала" />
<textarea id="comment1" name="comment" type="text" placeholder="Комментарий"></textarea>
<br/>
<button type="submit" id="submit1" value="отправить" /> Записаться </button>
</form>

js:
<!-- отправка формы -->
<script>
function send_form() { 
var msg = $("#forma").serialize();
$.ajax({
type: "POST",
url: "/wp-content/themes/gymbalance/send.php",
data: msg,
success: function(data) {
alert("Сообщение отправлено");
setTimeout(function () {
$(".feedback_form_bg").fadeOut();}, 1000);
},
error:  function(xhr, str){
alert("Возникла ошибка!");
setTimeout(function () {$(".feedback_form_bg").fadeOut();}, 1000);
}
});
}
</script>

php in send.php:
<?php
$fio= $_POST['fio'];
$phone= $_POST['phone'];
$age= $_POST['age'];
$adress= $_POST['adress'];
$comment= $_POST['comment'];
$emailTo = '[email protected]'; //Сюда введите Ваш email
$body = "ФИО: $fio \n\nТелефон: $phone\n\nВозраст: $age \n\nАдрес зала: $adress \n\nКомментарий: $comment";
$headers = "Content-Type: text/plain; charset=utf-8\r\n".'From: Заявка с сайта <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $emailTo;
mail($emailTo, $fio, $body, $headers);
$emailSent = true;
?>

Can you please tell me how to correctly change the classes and other parameters in these codes so that data is sent from several forms with different content? (For example, 2 fields "example1" and "example2" will be added to the second form, and 1 field "address" will be deleted from the third form).
Or do you need to create a separate send.php and script for each form?
How is it generally better, since now the form is implemented or try to do it only in php? Or install a plugin?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dummyman, 2017-04-18
@dummyman

In general, the form - should be an abstract element, it makes sense to combine them into one form if they are all sent at the same time. But you can add any value
instead of a string

var msg = $("#forma").serializeArray();
msg.push({  
   name: "example1",
   value: $("#forma [name=example1]").val()
});
msg.push({  
   name: "example2",
   value: $("#forma [name=example2]").val()
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question