成功ajax后如何提交表格?

时间:2014-10-24 04:01:42

标签: javascript php jquery ajax forms

当我加载页面index.php,然后点击链接DELETE时,它会将值发布到demo.php

并在将数据更新为demo.php之后table product为什么不自动提交表单id="myForm"

的index.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function delete_pro_fn() {
    $('#mySpan').hide();
    $('#loading').show();
        $.ajax
        (
            {
                url: 'demo.php',
                type: 'POST',
                data: $('#fid1').serialize(),
                cache: false,
                success: function (data) {
                    $('#loading').hide();
                    $('#mySpan').show();
                    $('#mySpan').html(data);
                }
            }
        )
}
</script>

<form id="fid1" method="post" action="" ENCTYPE = "multipart/form-data" style=" display: none; ">
    <input type="text" name="id_delete" value="1234567890"/>
</form>

<a href="JavaScript:delete_pro_fn()">DELETE</a>
<div id="loading" style="display: none;">Loading..........................</div>
<div id="mySpan"></div>

demo.php

<?PHP
include("connect.php");
$sql = "UPDATE product SET delete = '1' WHERE id = '$_POST[id_delete]'";
$dbQuery = mysql_query($sql);
?>
<form name="myForm" id="myForm" action="products.php" method="POST">
    <input name="delete" value="1" />
</form>

<script type="text/javascript">
window.onload = function(){
  document.forms['myForm'].submit()
}
</script>

1 个答案:

答案 0 :(得分:1)

要在ajax发布后提交表单,只需:

$('#myForm').submit();

编辑完整功能:

function delete_pro_fn() {
    $('#mySpan').hide();
    $('#loading').show();
        $.ajax
        (
            {
                url: 'demo.php',
                type: 'POST',
                data: $('#fid1').serialize(),
                cache: false,
                success: function (data) {
                    $('#loading').hide();
                    $('#mySpan').show();
                    $('#mySpan').html(data);
 $('#myForm').submit();
                }
            }
        )
}
</script>