SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Multiple html input rows like this

    HTML Code:
    <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

    Code:
    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....

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by rigaconnect View Post
    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:

    Code javascript:
    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:

    Code javascript:
    $('[id^="row"]').each(function (index, row) {
        var suffix = row.id.substring(3);
        autosave(suffix);
    });
    Last edited by paul_wilkins; May 15, 2013 at 21:48.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for advice!

    Will go through code to understand

  4. #4
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Script works without

    Code:
    autosave(1);
    autosave(2);
    autosave(3);
    I deleted, but the script works....

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by rigaconnect View Post
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah, yes, now understand
    Very useful code!
    Exactly what is necessary


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •