0

Till now I am stuck with these code here the code for ajax I know This is wrong ..or something is not right ..

 <script>
      $(document).ready(function() {
      $.ajaxSetup({ cache: false }); 

      setInterval(function() {

         $('#divToRefresh').load('userCount.php', function(data) {

            $.each($(data), function(key, value) {
                  alert(value);
            });
           });
          }, 3000);
        });
      </script>

here the userCount.php

$get_temp = "SELECT * FROM temperature_setting WHERE device_key = 'YMR200'";
    $result = $conn->query($get_temp);

     while($row = $result->fetch_assoc()) {
          $temp_id[] = $row['temp_id'];

      }
      foreach ($temp_id as $id_value) {
         $get_current_value = "SELECT $id_value FROM data_current WHERE device_key = 'YMR200'";
            $result_value = $conn->query($get_current_value);
              $row_value = $result_value->fetch_assoc();
                  $value[] = $row_value[$id_value];
     }
        print_r($value);

there are 15 value so each value should go to each

 foreach ($temp_id as $id_value) { ?>

    <p id="divToRefresh"></p>
    <br>
<?php } ?>

according currently the out put of userCount is

Array ( [0] => 65 [1] => -8 [2] => -5 [3] => -3 [4] => -5 [5] => 25 [6] => -5 [7] => -5 [8] => -5 [9] => -5 [10] => -5 [11] => -5 [12] => -5 [13] => 25 [14] => -5 )

what I want is to put these value in each


    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>

    <p id="divToRefresh"></p>
    <br>
6
  • 1
    You've repeated the same id attribute throughout the DOM. That's invalid as they must be unique. Use a class instead Commented Apr 28, 2017 at 7:04
  • what will be the change ...even if I make the class it is inside loop so still will be the same Commented Apr 28, 2017 at 7:05
  • 2
    It's not clear what you're trying to do. You're looping through the 15 divs, making an AJAX request for each, then attempting to stuf the same looped values from your PHP in to every div? Making one AJAX request would make much more sense Commented Apr 28, 2017 at 7:08
  • What I am trying to do is... I have that div inside a loop that will give 15 div of same id..now I want to fetch the value from ajax load where I have return the 15 value .. to each div respectively.. Commented Apr 28, 2017 at 7:21
  • Can you add expected output how you want exactly Commented Apr 28, 2017 at 9:15

1 Answer 1

1

maybe this is what you want

$(document).ready(function() {
        setInterval(function() {
          $.ajax({
            type: 'get',
            url: 'userCount.php',
            cache: false
          }).done(function(data) {
            $.each($(data), function(key, value) {
              $('#itemsTableBody').append($('<tr>').append($('<td>').text(value)));
            });
          });
        }, 3000);
      });
<html>
  <body>
    <table>
      <thead>
        <tr>
          <td>Item</td>
        </tr>
      </thead>
      <tbody id="itemsTableBody"></tbody>
      <tfoot>
        <tr>
          <td>Item</td>
        </tr>
      </tfoot>
    </table>
  </body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.