简体   繁体   中英

Refresh variable after update

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';    
  }
?>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM