If you decide you want to chunk your data and send it in multiple requests, which is not advised, consider this example.
$(function() {
function sendData(url, data) {
$.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'json',
success: function(res) {
console.log(res);
}
});
}
function getDataPair(element) {
if ($(element).is("textarea")) {
return {
name: $(element).attr("name"),
value: $(element).text()
};
}
return {
name: $(element).attr("name"),
value: $(element).val()
};
}
function formSubmit(form, chunk) {
var elements = $("input, textarea, select", form);
if (chunk == undefined) {
chunk = 10;
}
var myData = [];
elements.each(function(index, el) {
if (myData.length == chunk) {
sendData("{{ route('originalMap_edit') }}", myData);
myData = [];
}
myData.push(getDataPair(el));
console.log(index, myData);
});
}
$('#botaosalvar').on('click', function(event) {
event.preventDefault();
formSubmit($('#formdados'), 10);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="formdados">
<div><input type="text" name="a" value="1" id="a"></div>
<div><input type="text" name="b" value="2" id="b"></div>
<div><input type="hidden" name="c" value="3" id="c"></div>
<div>
<textarea name="d" rows="2" cols="40">4</textarea>
</div>
<div>
<select name="e">
<option value="5" selected="selected">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
</div>
<div>
<input type="checkbox" name="f" value="8" id="f"> <label for="f">8</label>
</div>
<div><input type="text" name="g" value="9" id="g"></div>
<div><input type="text" name="h" value="10" id="h"></div>
<div><input type="hidden" name="i" value="11" id="i"></div>
<div>
<textarea name="j" rows="2" cols="40">12</textarea>
</div>
<div>
<select name="k">
<option value="13" selected="selected">13</option>
<option value="14">14</option>
<option value="15">15</option>
</select>
</div>
<div>
<input type="checkbox" name="l" value="16" id="l"> <label for="l">16</label>
</div>
<div>
<button type="submit" id="botaosalvar">Submit</button>
</div>
</form>
As was discussed, this has a lot of pitfalls. If you have 5000 fields, this will take a bunch of time iterating over all the items and will send about 500 POSTs to your server, back to back.
- Your POST handler will have to put all the data back together
- If any of these Fail, there may be missing data
- If the script halts or does not complete, you will have orphaned or lost data
- The number of concurrent connections might be more than your web server can handle, it may QUEUE or DROP the requests (Similar to a DOS attack)
It would be advisable to not separate the data. It might look like this:
$(function() {
function sendData(url, data) {
$.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'json',
success: function(res) {
console.log(res);
}
});
}
function getDataPair(element) {
if ($(element).is("textarea")) {
return {
name: $(element).attr("name"),
value: $(element).text()
};
}
return {
name: $(element).attr("name"),
value: $(element).val()
};
}
function formSubmit(form) {
var elements = $("input, textarea, select", form);
var myData = [];
elements.each(function(index, el) {
myData.push(getDataPair(el));
});
sendData("{{ route('originalMap_edit') }}", myData);
}
$('#botaosalvar').on('click', function(event) {
event.preventDefault();
formSubmit($('#formdados'));
});
});