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 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图