Отправка одной кнопкой несколько форм по ajax
Имеется n форм на странице, пытаюсь отправить последовательно содержимое всех форм по ajax на страницу save.php, но передаются пустые строки. Подскажите что не так?
```
<form enctype="multipart/form-data" method="POST" id="form1">
<input id="user_id1" name="user_id" value="1" class="d-none">
<input id="nomer1" name="nomer">
<input id="date11" name="date1" type="date">
<input id="date21" name="date2" type="date">
</form>
...
<form enctype="multipart/form-data" method="POST" id="form8">
<input id="user_id8" name="user_id" value="8" class="d-none">
<input id="nomer8" name="nomer">
<input id="date18" name="date1" type="date">
<input id="date28" name="date2" type="date">
</form>
<button id="save" type="submit" class="btn btn-primary">Сохранить</button>
<script>
$(document).ready(function() {
$("#save").click(function(){
form1 = $("#form1");
$.ajax({
url: "save.php",
method: "POST",
contentType:false,
processData:false,
//data: new FormData($("#form1")),
data: form1.serialize(),
success: function(msg){
alert(msg);
}
});
...
form8 = $("#form8");
$.ajax({
url: "save.php",
method: "POST",
contentType:false,
processData:false,
//data: new FormData($("#form8")),
data: form.serialize(),
success: function(msg){
alert(msg);
}
});
});
});
</script>
```
Ответы (1 шт):
Уберите эти две строки
contentType:false,
processData:false,
В результате этих инструкций вы просто ничего не передаете на сервер
processData (default: true) Type: Boolean By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.
contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8') Type: Boolean or String When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header. Note: The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding. Note: For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.