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 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频