首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用jQuery POST代替html表单删除表项

如何使用jQuery POST代替html表单删除表项
EN

Stack Overflow用户
提问于 2018-06-11 02:05:48
回答 2查看 42关注 0票数 0

我试图在不使用jQuery post打开.php文件的情况下删除表项。当我只使用常见的html post表单时,整个工作都没有问题。

警报(Data)不会触发,它只会在URL中添加".../?player_ ID _del=1“或其他ID。

我做错了什么?

这是我的一些index.php,我从一个数据库中获得了全部数据:

<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>

这是我的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}";
}
?>

提前感谢您的帮助!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-11 02:17:03

你的代码中有很多问题,例如表单和删除输入按钮的id是重复的(元素的id不应该是相同的,它应该是唯一的),下面的代码是经过测试和工作的。

<?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>

票数 0
EN

Stack Overflow用户

发布于 2018-06-11 02:17:51

默认情况下,jQuery的click事件会重新加载文档,因此您应该尝试使用,

$("#submit-btn").click(function(e){ 
    e.preventDefault();
    e.stopPropagation();
});

另外,尝试使用$.ajax而不是$.post

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50786625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档