PHP不拾取后数据从jQuery的AJAX表单提交 [英] PHP Not Picking Up Post Data from jQuery AJAX Form Submit

查看:136
本文介绍了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>

我jQuery的AJAX是:

My jQuery AJAX is:

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

在我的PHP,我无法拿起$ _ POST ['删除图像'] isset()函数eventhough提交按钮发送名为删除图像的形式。

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'])) {
}

删除图像应该被置1,因为这是提交按钮,我点击。为什么PHP没有从这个AJAX拿起这个贴varaible提交?

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

推荐答案

在这里的jQuery documenation:的http:// api.jquery.com/serialize/
只有成功的控制是系列化,请参阅下面的文档:

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.

所以提交buttn通过jQuery.serialize()函数将不序列。解决方法是,您可以添加隐藏的输入,它将系列化。

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

更新:
您需要更改的形式提交的ajax绑定按钮点击。而在Ajax请求,你可以手动添加按钮值。下面是我的code,它是工作。

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);
        }
    });
});

这篇关于PHP不拾取后数据从jQuery的AJAX表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆