1

I'm working with loops and arrays. I want to submit table rows that are checked and wait for ajax success before next row is submitted. I tried a few things but no luck on getting only the rows I need. I could probably sort it out on the php side but I'd like it to be correct up front for changes later.

This article got me on the right track with a counter: stack: Ajax counter.

JS

$(document).ready(function () {

//submit boxes
$("#clickMe").click(sendrow);

// find and send boxes
num = 0;

function sendrow() {


    var boxes = $("input[class='box' ]:checked").length; //get count of boxes
    var values = $("input[class='box' ]:checked").map(function() {
        x = this.name;
        //alert( x); 
    }).get();



    if (boxes >= 0 && num <= boxes) { //must be some boxes*/


        //alert('boxes='+boxes+' num='+ num );

        //$("tr:has(input[name='row1']:checked)").each(function() {

        //rowData( num ); // gar rows




        ////////////////////////////////////////////////////////////////

        var data = {};
        //START ROW LOOP
        //alert(' row num='+ num );
        $("tr:has(input[name='row" + num + "']:checked)").each(function() {

            var feild = this;
            var values = "";
            //START FEILD LOOP
            $('input', this).each(function() {

                data[this.name] = this.value;
                // data =  $(this).val();
                //alert( $(this).val() ); 
            });
            //values =  values.substring(1) ; //sneek peek data

            //  alert(data); 

        }); // get forms

         //////////////////////////////////////////////////////////


        //ajax works
        $.ajax({
            url: "postdata.php",
            data: {
                data: data
            },
            type: 'post',
            //  dataType: 'json',
            //  contentType: "application/x-www-form-urlencoded",

            success: function(data) {

                $('#success').append(data);

                num++
                //alert('num success ='+ num);
                var next = num;
                //alert('num increment ='+ num);
                sendrow();
                //alert(data);
            }
        });


    } //end if num< boxes   
    else {
        'num' + num + '  was met, no more rows';
      }

  }; // end function


}); //end document

HTML

<form action="" method="POST" name="postForm">

<table width="200" border="1" cellspacing="1" cellpadding="1">
    <tr>
        <td><span class="style1">
                  <input type="checkbox" class="select_all"  /> 
                  .Select_all </span>
        </td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>
            <input name="ClickMe" type="button" id="clickMe" value="Submit" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="box" name="row0" />
        </td>
        <td>
            <input value="0-image here" type="text" name="brd" class='item' />
        </td>
        <td>
            <input value="0-title here" type="text" name="mfg" class='item' />
        </td>
        <td>
            <input value="0-Category here" type="text" name="pcat" class='item' />
        </td>
        <td>
            <input value="0-Store cat here" type="text" name="scat" i class='item' />
        </td>
        <td>
            <input value="0-Condition here" type="text" name="cond" class='item' />
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="box" name="row1" />
        </td>
        <td>
            <input value="1-image here" type="text" name="brd" class='item' />
        </td>
        <td>
            <input value="1-title here" type="text" name="mfg" class='item' />
        </td>
        <td>
            <input value="1-Category here" type="text" name="pcat" class='item' />
        </td>
        <td>
            <input value="1-Store cat here" type="text" name="scat" i class='item' />
        </td>
        <td>
            <input value="1-Condition here" type="text" name="cond" class='item' />
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="box" name="row2" />
        </td>
        <td>
            <input value="2-1mage here" type="text" name="brd" class='item' />
        </td>
        <td>
            <input value="2-title here" type="text" name="mfg" class='item' />
        </td>
        <td>
            <input value="2-Category here" type="text" name="pcat" class='item' />
        </td>
        <td>
            <input value="2-Store cat here" type="text" name="scat" class='item' />
        </td>
        <td>
            <input value="2-Condition here" type="text" name="cond" class='item' />
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="box" name="row3" />
        </td>
        <td>
            <input value="3-1mage here" type="text" name="brd" class='item' />
        </td>
        <td>
            <input value="3-title here" type="text" name="mfg" class='item' />
        </td>
        <td>
            <input value="3-Category here" type="text" name="pcat" class='item' />
        </td>
        <td>
            <input value="3-Store cat here" type="text" name="scat" class='item' />
        </td>
        <td>
            <input value="3-Condition here" type="text" name="cond" class='item' />
        </td>
    </tr>
    <td>
        <input type="checkbox" class="box" name="row4" />
    </td>
    <td>
        <input value="4-1mage here" type="text" name="brd" class='item' />
    </td>
    <td>
        <input value="4-title here" type="text" name="mfg" class='item' />
    </td>
    <td>
        <input value="4-Category here" type="text" name="pcat" class='item' />
    </td>
    <td>
        <input value="4-Store cat here" type="text" name="scat" class='item' />
    </td>
    <td>
        <input value="4-Condition here" type="text" name="cond" class='item' />
    </td>
    </tr>
</table>

halfer
  • 19,824
  • 17
  • 99
  • 186
zzipper72
  • 945
  • 2
  • 10
  • 24

2 Answers2

1

here is an alternative using your code (untested) that uses an index as a counter.

// your code redone with a counter.
$(document).ready(function () {

    //submit boxes
    $("#clickMe").click(function () {
        var boxes = $("input[type='checkbox' ]:checked");
        sendrow(boxes, 0);
    });

});
function sendrow(boxes, index) {

    if (boxes.length == 0 || index >= boxes.length) {
        // no rows checked or all rows processed.
        return;
    }
    var currentRow = $(boxes[index]).closest("tr");

    // load data 
    var data = {};
    currentRow.find("input[type='text']").each(function () {
        data[this.name] = this.value;
    });

    //ajax works
    $.ajax({
        url: "postdata.php",
        data: {
            data: data
        },
        type: 'post',
        currentProcess: {rows:boxes, currentIndex:index},
        success: function (data) {
            $('#success').append(data);
            var nextIndex = ++this.currentProcess.currentIndex;
            var boxes = this.currentProcess.rows;
            if (nextIndex < boxes.length) {
                sendrow(boxes, nextIndex);
            }
        }
    });

}
Bindrid
  • 3,655
  • 2
  • 17
  • 18
  • This looks more like what I imagined, it fails at console.log says "....currentRow.find("input[type='text']").each( ... is not a function." I'm working on it now – zzipper72 Dec 02 '16 at 17:55
  • I forgot to added the $ on $(boxes[index]).closest("tr"); fixed – Bindrid Dec 02 '16 at 18:04
  • ..and this is waiting on success before the next send? .. this is EXACTLY what I needed!!... this looks great on the JSON return and the PHP was a simple array to work with. .OMG, my sanity restored ... you sir are my HERO. **The purpose here was to send rows to EBay (very long wait due to item rows AND many vehicle compatibility rows) .. user will see the row sent disappear and return in another table as the result completed item ... but not be timed out with a PHP loop...hence the AJAX separate calls. I can catch the error and return the users hard work(inputs) to them to fix. woot!! – zzipper72 Dec 02 '16 at 18:25
0

Ok, I got this working in my environment (Visual Studio) so I had to make some adjustment to the ajax but you should be able to convert it back to yours.

Basically, I get all to rows that are checked then add a class to mark which ones should be processed. When I send a row off, I change the class to processing and when it is done, I remove the class and call sendrow to process the next row.

Also, take note how I passed the row from the request to the success function to to removed the processing class.

I added a cell with the name of "status" so I could stuff proof that it hit the server in it.

        $(document).ready(function () {
            //submit boxes
            $("#clickMe").click(function () {

                var boxes = $("input[class='box' ]:checked");
                // add class to see what has to be processed
                boxes.closest("tr").addClass("waiting");
                sendrow();
            });

        });

        // process rows
        function sendrow() {
            var boxes = $(".waiting");
            if (boxes.length == 0) {
                // we are done
                return;
            }

            var $row = $(boxes[0]);
            $row.removeClass("waiting").addClass("processing");
            var row = $row.html();

            //ajax works
            $.ajax({
                url: "WebService1.asmx/GetADate",
                 data: JSON.stringify( {s:row}),
                type: 'post',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                rowprocessed:{ arow:$row}, 
                success: function (data, status) {

                    var rp = this.rowprocessed.arow;
                    rp.find("[name='status']").val(data.d);
                    rp.removeClass('processing')
                    sendrow();
                },
                error: function (one, two) {
                    debugger;
                }
            });
        }
Bindrid
  • 3,655
  • 2
  • 17
  • 18
  • ..so I think your answer is correct structurally and will allow my rows to return properly with no time outs ( EbayMotors API is so slow, but each row has alot of data!) ... so for that reason I'm going to mark answered for the next person... and I'll post a fiddle once my side works ... THANK YOU!!!!! – zzipper72 Dec 02 '16 at 15:45
  • Because the whole thing was running on my box, I put a sleep two seconds in the web service to simulate going across the internet. All my web service did (since this was proof of concept) was to accept the serialized row and return the current time in seconds as a string and stuff it into a textbox in a column I added to your html. – Bindrid Dec 02 '16 at 16:25
  • Like I said, it does work! I think I got it, but I cant make use of the html yet.. I need the values to talk to the Ebay API ..I'll get it shortly – zzipper72 Dec 02 '16 at 16:42
  • If you truly need it, I can write it to make use of a counter but I could not get to doing that til I get home (12 hours or so) – Bindrid Dec 02 '16 at 16:50
  • I go this to work to with small mod using a loop. var $row = $(boxes[0]); $row.removeClass("waiting").addClass("processing"); //row = $row.html(); row={}; //START FIELD LOOP $(' input', $row).each(function() { row[this.name] = this.value; }); //alert(JSON.stringify(row)); //row is all fields Ajax was changed to: data: row, This returned only the exact rows chosen with all the fields in a neat array – zzipper72 Dec 02 '16 at 18:28