-1

html code

<?php
    while($row = mysqli_fetch_assoc($WltQRslt)){ 
            $Ldate = $row['LastDate'];
            $Bdate = $row['DateBought'];
            $id=sprintf('%s_%s',$Ldate,$Bdate);echo "<tr ><td class='center'><i class='fa fa-database fa-3x' aria-hidden='true'></i>&nbsp;&nbsp;&nbsp;&nbsp;{$row['Server']}</td><td id ='".$row['SNo']."' ><i class='fa fa-line-chart fa-3x'></i>&nbsp;&nbsp;&nbsp;&nbsp;{$row['Profit']}</td><td><i class='fa fa-refresh fa-spin fa-3x fa-fw'></i>&nbsp;&nbsp;&nbsp;&nbsp;<span class='date' date-start='$Bdate' date-end='$Ldate'></span></td>";
    }
?> 

jquery code

$('table td[id]').each(function (index, element) {
    var id = $(element).attr('id');

     $.ajax({
        url:"get_server.php",
        method:"POST",
         data:{id:id},
         success:function(data) {
            var no=data.count;
            for(i=0; i<=no;i++) {
               var price  = data;
               console.log(price);
            }
        }
    }); 
});

this jquery function get all the ID`s of each row contaning ID value. i want that on each iteration it get id from proccess it then move on to another iteration?

Ajay Singh
  • 634
  • 6
  • 18
user
  • 15
  • 6
  • 3
    Your HTML code looks a lot like PHP spaghetti – Rory McCrossan Dec 06 '19 at 10:43
  • i am passing data to html table through php – user Dec 06 '19 at 10:45
  • "_i want that on each iteration it get id from proccess it then move on to another iteration_" Now that we know what you want can you maybe also tell us what you get? Does something not work? Do you get errors? Do you get the wrong ids? – brombeer Dec 06 '19 at 10:45
  • `i want that on each iteration it get id from proccess it then move on to another iteration?` This is exactly what your loop is already doing: https://jsfiddle.net/RoryMcCrossan/go14cyrw/ – Rory McCrossan Dec 06 '19 at 10:46
  • actualy when all the table is loaded then it calls this jquery function. i want to call it on every loop – user Dec 06 '19 at 10:48
  • If you mean you want to call it on every loop of the PHP code, then that's not possible as PHP runs on the server and JS on the client. If that's not what you meant then you'll need to give a much clearer description of your goal. Also note that it's really not a good idea sending AJAX requests in a loop as you'll flood your server. Aggregate all data in to a single request and send that, once. – Rory McCrossan Dec 06 '19 at 10:49
  • exactly this is what i want on every loop call this functon. if its not possible. then is there any other alternative way. this jquery will actually process the id and make a value to display in same – user Dec 06 '19 at 10:51
  • ??????????????? – user Dec 06 '19 at 11:19
  • The alternative way is to do what you currently are. – Rory McCrossan Dec 06 '19 at 11:41
  • but its not working for me – user Dec 06 '19 at 12:27

1 Answers1

0

You can get the id of each td element directly as element.id and change selector to 'table td'.

$('table td').each(function (index, element) {
    var id = element.id;

     $.ajax({
        url:"get_server.php",
        method:"POST",

...

And a sample snippet to see how it works..

$('table td').each(function (index, element) {
  console.log(element.id);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<table>
    <tr><td id='2' class='center'></td></tr>
    <tr><td id='1' class='center'></td></tr>
</table>
Ajay Singh
  • 634
  • 6
  • 18
  • i want to call this jquery in a loop. each time the data is populated through while loop.means at every iteration – user Dec 06 '19 at 13:08
  • If I am getting it right, the '.each' command itself is a loop like while and for. And in your code, for each td element, the ajax code will execute. – Ajay Singh Dec 06 '19 at 13:21
  • 1
    While loop is in php, server side which executes first. So the html table will be available for jQ when javascript executes after php has completed execution. – Ajay Singh Dec 06 '19 at 13:21
  • is there any other alternative to javascript which runs with php loop? – user Dec 06 '19 at 14:23
  • 1
    You can also do this inside the php while loop itself depending on how frontend is hosted, using curl (if hosted at a different server) and make a post request. So by the time execution reaches html/js, you will have both id and price. Check the SO question https://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code for curl post. – Ajay Singh Dec 06 '19 at 14:35