1

I am trying to find a way to refresh the count on a class after a successfull mysql update. At the moment I am using a class to display the information and for styling and this works well on page load. Is is possible to refresh a class after update. $ni; stores the result after mysql count.

I have attached the code I am using and would be grateful for any advice. Thanks

html

<ul>
    <li>
      <a href="/domain/admin/test.php" title="Add">New Intake <span class="notification" style="float: right;"><?php echo $ni; ?></span></a>
    </li>
</ul>

php

<?php    
  $sql = mysqli_query($conn, "SELECT count(*) as total FROM act WHERE new = '1'"); // provide db connection object as first parameter
  $rows = mysqli_fetch_assoc($sql);
  $num = $rows['total'];
  $ni = $num;

  if ($ni < 1) {
    $ni = '0';    
  }
?>
2
  • 1
    You could do this by sending a request to run the PHP via AJAX then update the DOM using the AJAX callback method Commented Feb 26, 2018 at 11:21
  • @MasterYoda do you have example please. Just a starter would do. Would I do this within success call? Run another ajax. Thanks Commented Feb 26, 2018 at 11:27

1 Answer 1

1

You need to make an AJAX call after you execute the SQL update in order to obtain the latest data and insert it in the DOM as mentioned by Master Yoda in the comments.


Example ajax code:

$.ajax({
        url: 'yourURL',
        success: function(msg){
            $(".notification").text(msg);
        },
        error: function(jqXHR, textStatus)
        {
            //Manage your error.   
        }
    });


Explanation: 'yourURL' should be replaced with a link to the php page that calculates the value and returns it. On success that value will be written in the span with class notification. Also you need to execute the ajax code every time you do an updated.

More information about AJAX

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

1 Comment

Thanks very much for starter. Got it working this way: $.get('/ldomain/admin/requests/boxes/retrieve/refreshBox.php?rtvni=' + 'New Intake', function(data) { console.log(data); $(".notification").text(data); });

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.