dongshengyin0147 2014-07-29 05:18
浏览 68
已采纳

PHP不从jQuery AJAX表单提交发布数据

I have a form with a submit button like this:

<form action="?edit-form" method="post" class="addEdit">
    <input type="submit" name="delete-image" value="Delete Image">
</form>

My jQuery AJAX is:

$.ajax({
    url: $('form.addEdit').attr('action'),
    type: $('form.addEdit').attr('method'),
    data: $('form.addEdit').serialize(),
    success: function(html) { }
});

In my PHP, I am unable to pick up that $_POST['delete-image'] isset() eventhough the submit button that sent the form is named "delete-image".

if (isset($_POST['delete-image'])) {
}

"delete-image" should be set since that was the submit button I clicked. Why is PHP not picking up this posted varaible from this AJAX submit?

  • 写回答

2条回答 默认 最新

  • doujionggan9570 2014-07-29 05:37
    关注

    In jQuery documenation here: http://api.jquery.com/serialize/
    Only succesful control are serialized, please see the documentation below:

    Note: Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.

    So submit buttn won't serialize through jQuery.serialize() function. The work around is you can add hidden input, and it will serialized.

    UPDATE:
    You need to change the form submit ajax to bind the button click. and in ajax request you can add the button value manually. Below is my code that is working.

    $( "#delImage" ).click(function( event ) {
        event.preventDefault();
        $form = $(this).parent('form');
        $btnid = $(this).attr('name');
        $btnval = $(this).attr('value');
    
    
        $.ajax({
            url: $form.attr('action'),
            type: $form.attr('method'),
            data: { "btnid" : $btnid, "btnval": $btnval, "form-data": $form.serialize() },
            success: function(html) {
                console.log(html);
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用