weixin_33701617 2018-01-17 00:25 采纳率: 0%
浏览 16

非活动和活动PHP MySQL

Having problem to change the state if its active or inactive everytime I clicked one of them nothings happened.

This is my code on ajax.

<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).on('click','.status_checks',function(){
      var status = ($(this).hasClass("btn-success")) ? '0' : '1';
      var msg = (status=='0')? 'Deactivate' : 'Activate';
      if(confirm("Are you sure to "+ msg)){
        var current_element = $(this);
        url = "ajax.php";
        $.ajax({
          type:"POST",
          url: url,
          data: {id:$(current_element).attr('data'),status:status},
          success: function(data)
          {   
            location.reload();
          }
        });
      }      
    });
</script>

my php code ajax.php

<?php $db= new mysqli('localhost','root','password','dbname'); 
extract($_POST);
$user_id=$db->real_escape_string($id);
$status=$db->real_escape_string($status);
$sql=$db->query("UPDATE user SET status='$status' WHERE id='$id'");
echo 1;
?>

this is my code for displaying it, on this part everything works fine, when the value are 0 it will display inactive and 1 for active, however when clicking the status theres nothing happen only the notification and reload the page.

<td><i data="<?php echo $user['id'];?>" class="status_checks btn
  <?php echo ($user['status'])?
  'btn-success': 'btn-danger'?>"><?php echo ($user['status'])? 'Active' : 'Inactive'?>
 </i></td>
  • 写回答

1条回答 默认 最新

  • weixin_33717298 2018-01-17 00:37
    关注

    Try to configure your code to:

    $(document).on('click','.status_checks',function(){
      var status = '1';
      var msg = 'Activate';
    if($(this).hasClass("btn-success")){
      status = '0';
      msg = 'Deactivate';
     }
    
    
      if(confirm("Are you sure to "+ msg)){
        var id= $(this).data('id');
        url = "/ajax.php";
        $.ajax({
          type:"POST",
          url: url,
          data: {id:id,status:status},
          dataType: "json",
          success: function(data)
          {   
            console.log(data);
            location.reload();
          }
        });
      }      
    });
    
    <?php $db= new mysqli('localhost','root','password','dbname'); 
    
    $user_id=$_POST['id'];
    $newStatus=$_POST['$status'];
    $sql = "UPDATE user SET status=".$newStatus." WHERE id=".$user_id." 
    ";
    if($db->query($sql) === TRUE){
    echo json_encode(1);
    }else{
    echo json_encode(0);
    }
    
    ?>
    
    
    
    
    <td><i data-id="<?php echo $user['id'];?>" class="status_checks btn
    <?php echo ($user['status'])?
    'btn-success': 'btn-danger'?>"><?php echo ($user['status'])? 'Active' : 
    'Inactive'?>
    </i></td>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题