du12197 2018-06-10 18:05
浏览 40
已采纳

如何使用jQuery POST而不是html表单来删除表项

I am trying to delete the table entry without opening the .php file using jQuery post. The whole thing works without problems when I just use the usual html post form.

The alert(data) does not trigger, it only adds ".../?player_id_del=1" or whatever ID click into the URL.

What am I doing wrong?

Here is some of my index.php, i get the whole data from a database:

<table class = "table table-hover">
            <thead>
            <tr>
                <th>Player_ID</th>
                <th>Username</th>
                <th>First_Name</th>
                <th>Last_Name</th>
                <th>Rating</th>
                <th>Country</th>
                <th>Colour</th>
                <th></th>
            </tr>
            </thead>
            <tbody>
            <? foreach($playerArray as $player):?>
                <tr>
                    <td><? echo $player["PLAYER_ID"]; ?></td>
                    <td><? echo $player["USERNAME"]; ?></td>
                    <td><? echo $player["FIRST_NAME"]; ?></td>
                    <td><? echo $player["LAST_NAME"]; ?></td>
                    <td><? echo $player["RATING"]; ?></td>
                    <td><? echo $player["COUNTRY"]; ?></td>
                    <td><? echo $player["COLOUR"]; ?></td>
                    <td>
                        <form id="del-form">
                            <div>
                                <input type="number" id="player_id_del" name="player_id_del" value="<?php echo  htmlspecialchars($player["PLAYER_ID"]); ?>" />
                            </div>
                            <div>
                                <button type="submit" id="submit-btn" class="btn btn-danger">Delete</button>
                            </div>
                        </form>
                        <script>
                            $("#submit-btn").click(function(){
                                $.post("deletePlayer.php", $("#del-form").serialize() , function(data) {
                                    alert(data);
                                });
                            });
                        </script>
                    </td>
                </tr>
            <? endforeach ?>
            </tbody>
        </table>

Here is my deletePlayer.php:

<?php
//include DatabaseHelper.php file
require_once('DatabaseHelper.php');

//instantiate DatabaseHelper class
$database = new DatabaseHelper();

//Grab variable id from POST request
$player_id = '';
if(isset($_POST['player_id_del'])){
    $player_id = $_POST['player_id_del'];
}

// Delete method
$error_code = $database->deletePlayer($player_id);


// Check result
if ($error_code == 1){
    echo "Player with ID: '{$player_id}' successfully deleted!'";
}
else{
    echo "Error can't delete Player with ID: '{$player_id}'. Errorcode: {$error_code}";
}
?>

Thank You in advance for any help!

  • 写回答

2条回答 默认 最新

  • dswfyq6201 2018-06-10 18:17
    关注

    There are many issues in your code E.g IDs for form and delete input button are repeating (id of element should not be same it should be unique), The following code is the tested and working.

    <?php
    //include DatabaseHelper.php file
    require_once('DatabaseHelper.php');
    
    //instantiate DatabaseHelper class
    $database = new DatabaseHelper();
    $response = array();
    //Grab variable id from POST request
    $player_id = '';
    if(isset($_POST['player_id_del'])){
        $player_id = $_POST['player_id_del'];
    }
    
    // Delete method
    $error_code = $database->deletePlayer($player_id);
    
    
    // Check result
    if ($error_code == 1){
        $response["success"] = 1;
        $response["id"] = $player_id;
        $response["message"] = "Player with ID: '{$player_id}' successfully deleted!'";
    }
    else{
        $response["success"] = 0;
        $response["message"]= "Error can't delete Player with ID: '{$player_id}'. Errorcode: {$error_code}";
    }
    
    echo json_encode($response);
    ?>
    <table class = "table table-hover" id="mPlayersTabel">
       <thead>
          <tr>
             <th>Player_ID</th>
             <th>Username</th>
             <th>First_Name</th>
             <th>Last_Name</th>
             <th>Rating</th>
             <th>Country</th>
             <th>Colour</th>
             <th></th>
          </tr>
       </thead>
       <tbody>
          <? foreach($playerArray as $player):?>
          <tr id= "<? echo $player["PLAYER_ID"]; ?>">
             <td><? echo $player["PLAYER_ID"]; ?></td>
             <td><? echo $player["USERNAME"]; ?></td>
             <td><? echo $player["FIRST_NAME"]; ?></td>
             <td><? echo $player["LAST_NAME"]; ?></td>
             <td><? echo $player["RATING"]; ?></td>
             <td><? echo $player["COUNTRY"]; ?></td>
             <td><? echo $player["COLOUR"]; ?></td>
             <td>
               
                   <div>
                     <button type="submit" player-id="<? echo $player["PLAYER_ID"]; ?>" class="btn btn-danger" >Delete</button>
    
                   </div>
              
             </td>
          </tr>
          <? endforeach ?>
       </tbody>
    </table>
    <script>
       $(document).ready(function(){
            $(".btn-danger").on("click touchend" , function(){
                    var id =  $(this).attr("player-id");
                     $.ajax({
                        url: 'deletePlayer.php',
                        type: 'POST',
                        data: {
                            player_id_del: id
                        },
                        dataType: 'json',
                        success: function (response) {
                            //Add this line and try
                            response = JSON.parse(JSON.stringify(response));
                            alert(response['message']);
                            switch (response['success']) {
                                case 1:
                                    $("#mPlayer" + response['id']).remove();
                                    break;
                            }
            
                        }
                });
                });
        });
    </script>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题