0

this is my whole javascript code what im trying to do here is loop through html table and look for checked checkbox and retrieve the data in every row of checked checkbox but i need to run this is php.

<script type='text/javascript'>//<![CDATA[
    $(window).load(function () {
        $('#save').click(function () {
            $('#dataTable').find('tr').each(function () {
                var row = $(this);
                if (row.find('input[type="checkbox"]').is(':checked') ) {
                    //alert('You must fill the text area!');
                    var $row1 = $(this).closest("tr"),        // Finds the closest row <tr> 
                    $tdi = $row1.find("td:nth-child(1)");
                    $.each($tdi, function () {                // Visits every single <td> element
                        var thirdrowval = $(this).text();         // Prints out the text within the <td>
                        //document.getElementById("signatoryid").value = thirdrowval
                        alert(thirdrowval);
                    });
                }
            });
        });
    });//]]> 

</script>

and after reading in this site i found a way to do it and here is the code. but it doesn't run the javascript. i expect an alert to pop up. but it doesn't worked as expected

$row1 = "";
$row = "";
$thirdrowval = "";
$tdi = "";
echo "
        <script type=\"text/javascript\">
                    $('#dataTable').find('tr').each(function () {
            var row = $(this);
            if (row.find('input[type='checkbox']').is(':checked') ) {
                var $row1 = $(this).closest('tr'),       
                $tdi = $row1.find('td:nth-child(1)');
                $.each($tdi, function () {                
                    var thirdrowval = $(this).text();         
                    alert(thirdrowval);
                });
            }
        });
        </script>
    ";
knowmeifyou
  • 217
  • 7
  • 17
  • 1
    Possible duplicate of [How to call a JavaScript function from PHP?](http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php) – Jeremy J Starcher Nov 13 '15 at 08:12
  • no sir. my question is different. i have two insert statement that need to be executed as one. the one is the data are from input types. and the other one is those data from html table with checked checkboxes. in order to loop the html table i need javascript – knowmeifyou Nov 13 '15 at 09:10

2 Answers2

0

Try to include your table $('#dataTable') also in your php file. I think javascript can't find the element that named #dataTable

Lierej39
  • 34
  • 2
  • Can you expand on that, where should it be included, etc. Also, wouldn't `dataTable` be the `id`, not `#dataTable` the `name`? – blm Nov 13 '15 at 08:22
  • also include your table tag in echo like this .. `echo "
    ` and also include your jquery file.
    – Lierej39 Nov 13 '15 at 08:30
0

in the line

if (row.find('input[type='checkbox']').is(':checked') ) {

you will have to escape the inner quotes ' it should be

if (row.find('input[type=\'checkbox\']').is(':checked') ) {

also you have to pay attention that variables in strings with double quotes like

echo "$row1";

will be replaced with the value of the php variable $row1. So in your example it will be replaced with an empty string. If this is not the exspected behaviour you can use single quotes:

echo '$row1';

this will print $row1