简体   繁体   English

表单未通过AjaxForm / JQuery提交

[英]Form not getting submitted via 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. 我正在使用AjaxForm方法提交表单。 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.) 要通过AjaxForm提交表单,请按照以下步骤操作:(我这里没有显示<table>标签。请原谅。)

<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. 问题是表单是通过常规方式提交的。action action="deletebook.php"在其他页面上相信我,表单是通过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. 通过将我发送到deletebook.php页面来删除数据。 I want data to get deleted without sending me to any other page. 我希望删除数据而不将我发送到任何其他页面。

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: 并在您的php add方法中,返回如下字符串:

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

I suppose you are using the ajaxForm plugin here http://malsup.com/jquery/form/#ajaxForm 我想你在这里使用ajaxForm插件http://malsup.com/jquery/form/#ajaxForm

    $(document).ready(function() { 
        $('#deleteform').ajaxForm(
           {
              beforeSubmit: function(formData, jqForm, options){
                   //show loader
              },
              success:function(responseText, statusText, xhr, $form) { 

                 alert(responseText);
                 //hide loader
              }
           }
        ); 

    }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM