duanjiwang2927 2018-12-08 20:09
浏览 75
已采纳

使用ajax更新数据库值

I'm trying to update a database value each time complete() occurs. The following code is located in a file called 'user.php' and $u is globally defined elsewhere in the file. The value pT simply doesn't update when complete() is ran. Thanks in advance.

<?php
    if (isset($_POST['pT'])){
        $pT = $_POST['pT'];

        $sql = "UPDATE progress SET progressbar='$pT' WHERE username='$u'";
        $query = mysqli_query($conn, $sql);

        exit();
    }
?>

var pT = 0;

function complete(){

    pT = pT + 25;

    $.ajax({
        url: 'user.php',
        type: 'POST',
        data: {'pT': pT},
        success: function() {}
    });

}
  • 写回答

1条回答 默认 最新

  • doushengyou2617 2018-12-08 20:40
    关注
    1. split the files

      user.php
      _______
      <?php
      if (isset($_POST['pT'])){
          $pT = $_POST['pT'];
      
          $sql = "UPDATE progress SET progressbar='$pT' WHERE username='$u'";
          $query = mysqli_query($conn, $sql);
      
          exit();
      }
      ?>
      

    1. then make sure your javascript/jquery ajax isn't malformed

      somewherelse.html
      ____________
      var pT = 0;
      
      function complete(){
      
        pT = pT + 25;
      
        $.ajax({
          url: 'user.php',
          type: 'POST',
          data: 'pT='+ pT,
          success: function() {
              alert('done');
          }
        });
      
      }
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 streamingtool
  • ¥15 MATLAB图像问题
  • ¥20 树莓派5做人脸情感识别与反馈系统
  • ¥15 selenium 控制 chrome-for-testing 在 Linux 环境下报错 SessionNotCreatedException
  • ¥15 使用pyodbc操作SQL数据库
  • ¥15 MATLAB实现下列
  • ¥30 mininet可视化打不开.mn文件
  • ¥50 C# 全屏打开Edge浏览器
  • ¥80 WEBPACK性能优化
  • ¥30 python拟合回归分析