如何使用jQuery调用php函数从MYSQL获取更新数据

如何使用jQuery调用php函数从MYSQL获取更新数据

问题描述:

So I have a number in the MYSQL table which is displayed on the screen using the function:

function getWinNumber($db) {

    $user = getProfileInfoFor($_SESSION['id'], $db); 

    $winNumber = $user->number;

    return $winNumber;
}

Then after some user actions that value changes in the DB and I want to update it in front-end when they close a foundation modal:

$(document).on('close.fndtn.reveal', '#ex6-4', function () {

        $('.number').html("<?php echo getWinNumber($db);?>");

    });

The problem is that even though the code works, and I can clearly see in the DB that the value has changed before I close the modal, the value being pulled is still the old one. Why is that?

If I put anything else in there like:

$(document).on('close.fndtn.reveal', '#ex6-4', function () {

        $('.number').html("test");

    });

it works. It updates when I close the modal.

所以我在MYSQL表中有一个数字,它使用以下函数显示在屏幕上: p> \ n

  function getWinNumber($ db){
 
 $ user = getProfileInfoFor($ _ SESSION ['id'],$ db);  
 
 $ winNumber = $ user-&gt; number; 
 
返回$ winNumber; 
} 
  code>  pre> 
 
 

然后在一些值更改的用户操作之后 在DB中,当我们关闭基础模式时,我想在前端更新它: p>

  $(document).on('close.fndtn.reveal','  #ex6-4',function(){
 
 $('。number')。html(“&lt;?php echo getWinNumber($ db);?&gt;”); 
 
}); \  n  code>  pre> 
 
 

问题在于,即使代码有效,并且我可以清楚地看到数据库中的值在关闭模态之前已经改变了,所以拉出的值是 还是旧的。 这是为什么? p>

如果我把其他东西放在那里: p>

  $(document).on('close.fndtn.reveal','  #ex6-4',function(){
 
 $('。number')。html(“test”); 
 
}); 
  code>  pre> 
 
  

它有效。 当我关闭模态时它会更新。 p> div>

I ended up doing it with AJAX like this and it worked:

$(document).on('close.fndtn.reveal', '#ex6-4', function () {

         $.ajax({ url: 'include/update.php',
             data: {action: 'update'},
             type: 'post',
             success: function(output) {

                      $('.encrypted').html(output);
                  }
        });  
    });

and for the PHP:

if(isset($_POST['action']) && !empty($_POST['action'])) {

    $action = $_POST['action'];
    if ($action == 'update') {

        echo getWinNumber($db);
    }
}