With ajax get hml input (array-row) then if condition true pass to php file

Multiple html input rows like this

<tr id='row1'>
<td><input type="text" name="row1[]" id="date_day1" class="row_changed1"></td>
....
<td>
<input type="text" name="row1[]" id="is_row_changed1" size="1">
<script>
$(".row_changed1").on("change", function () {
document.getElementById('is_row_changed1').value = 1;
});
</script>
</td>
<td>
<tr>

At the moment for each row separate ajax code like this


if ($("#is_row_changed1").val() > 0) {
$.post("_autosave_array.php", $("#row1 :input").serialize(), function(data1) {
$('#load1').html(data1);
$('#is_row_changed1').val(0)
});
var str = $("#row1 :input").serialize();
$("#load1_1").text(str);
}

if ($("#is_row_changed2").val() > 0) {
$.post("_autosave_array.php", $("#row2 :input").serialize(), function(data2) {
$('#load2').html(data2);
$('#is_row_changed2').val(0)
});
var str = $("#row2 :input").serialize();
$("#load2_1").text(str);
}

Aim is to get write ajax code like foreach if ($(“#is_row_changed1,2,…1000”).val() > 0) {pass corresponding input <tr>“#row1,2,…100 :input” to php

May be some link/information/sample? To what direction to go…

You can do that by using Attribute Starts With Selector [name^=“value”]

A good way to get ready for that process is to turn some of your existing code in to a function:


function autosave(suffix) {
    if ($("#is_row_changed" + suffix).val() > 0) {
        $.post("_autosave_array.php", $("#row" + suffix + " :input").serialize(), function(data) {
            $('#load' + suffix).html(data);
            $('#is_row_changed' + suffix).val(0)
        });
       var str = $("#row" + suffix + " :input").serialize();
       $("#load" + suffix + "_1").text(str);
    }
}

autosave(1);
autosave(2);
autosave(3);
...

Doing the above will help to ensure that your code carries on doing the same thing that it should be doing.

After making the above type of change, you can then replace those calls to the autosave function with something like this:


$('[id^="row"]').each(function (index, row) {
    var suffix = row.id.substring(3);
    autosave(suffix);
});

Thank you for advice!

Will go through code to understand

Script works without

autosave(1);
autosave(2);
autosave(3);

I deleted, but the script works…

Yes, that is the point of the jQuery code that was put up afterwards. It’s to replace the functionality of the autosave commands that you removed.

ah, yes, now understand
Very useful code!
Exactly what is necessary