drwghu6386 2015-05-05 13:54
浏览 47
已采纳

表单未通过AjaxForm / JQuery提交

There is a table which displays items and has a "Delete" button in each row. The delete button is inside a form. I'm using AjaxForm method to submit form. Clicking the delete button is supposed to delete the data without getting directed to any page or without refreshing current page. But what happens is form gets submitted in regular way as defined on action tag.

I'm displaying results on table in following way:

while($row = $result->fetch_assoc()) 
    {               
    ?>  


<tr>
  <td><?php echo  ++$x; ?></td>
  <td><?php echo $row["id"]; ?></td>
  <td><?php echo $row["title"]; ?></td>
  <td><?php echo $row["author"]; ?></td>
  <td>
  <form method="POST" id='deleteform' class="forms" action="deletebook.php">         
  <input type="hidden" name="deletebook" value='<?php echo $row["id"]; ?>' >
  <button type="submit" class="btn btn-danger btn-xs">Delete</button>
  </form>
  </td>
</tr> 


<?php
    }
}

else
{
    echo "No Books Found!";
}

This displays table in following way

To submit the form via AjaxForm, it's been done as follows: (I haven't shown <table> tags here. pardon me.)

<script>
            $(document).ready(function() { 
                $('#deleteform').ajaxForm(function() { 

                    alert("success");

                }); 
                event.preventDefault();
            }); 
    </script>

The problem is that form is submitted via regular way action="deletebook.php" Trust me in other pages the form gets submitted via AjaxForm. But in this page, the problem is occuring.

Could this be happening because every "delete" button is a form displayed according to the contents in the table. Please help me.

EDIT: The issue is not about being unable to delete data.The data was getting deleted before and is getting deleted now. Data is getting deleted by sending me to the deletebook.php page. I want data to get deleted without sending me to any other page.

  • 写回答

2条回答 默认 最新

  • douxiangshi6568 2015-05-05 14:24
    关注

    Try this way:

    while($row = $result->fetch_assoc()) 
        {               
        ?>  
    
    
    <tr>
      <td><?php echo  ++$x; ?></td>
      <td><?php echo $row["id"]; ?></td>
      <td><?php echo $row["title"]; ?></td>
      <td><?php echo $row["author"]; ?></td>
      <td>
          <input type="hidden" class="hiddenId" value="<?php echo $row["id"]; ?>" />
      <button type="button" class="btn btn-danger btn-xs">Delete</button>
      </td>
    </tr> 
    
    
    <?php
        }
    }
    
    else
    {
        echo "No Books Found!";
    }
    
    ?>
    
    <script>
        var id;
    
    $(document).ready(function(){
        $(".btn-danger").click(function(){
            id = $(this).prev().val();
            $.ajax({
                url: "deletebook.php",
                type: 'post',
                data: {
                    "deletebook": id
                }
            }).done(function(response){
                $(".hiddenId[value='" + id + "']").parent().parent().remove();
            });
        });
    });
    </script>
    

    Add:

    $('#addbook').ajaxForm(
    { 
        success: function(responseText, statusText, xhr, $form){
            $('#booksuccess').modal('show'); 
            $("#reset").click(); 
            $("tr").last().append(responseText);
        }
    }); 
    

    And in your php add method, return a string like this:

    "<tr><td></td><td>1</td><td>$Title</td><td>$Author</td><td><input type=\"hidden\" class=\"hiddenId\" value=\"$Id\" /><button type=\"button\" class=\"btn btn-danger btn-xs\">Delete</button></td></tr>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)