简体   繁体   English

为什么序列化不包含提交按钮名称?

[英]Why serialize does not include submit button name?

Why serialize (Jquery) does not include submit button name? 为什么序列化(Jquery)不包含提交按钮名称?

In PHP I normally do this: 在PHP中,我通常这样做:

if (isset($_POST['submit_signup'])) {
    print_r($_POST);
    //Then check firstname, password, etc POSTS
}

That don't work, what is the solution to this? 那不起作用,对此有什么解决方案?

Not sure if this is the most elegant solution, but you can use a hidden input box to contain a value (preferably something that changes, like a sort of hash) and check for that. 不确定这是否是最优雅的解决方案,但是您可以使用隐藏的输入框来包含一个值(最好是发生变化的值,例如某种哈希),然后进行检查。 Like: 喜欢:

<input type="hidden" name="signup_submitted" value="random_hash" />

And in your code you can do: 在您的代码中,您可以执行以下操作:

if(isset($_POST['signup_submitted']))
{
    // Do your work;
}

Warning: A hash is not a security thing here, only a way to do the same thing you're used to. 警告:这里的哈希值不是安全的事情,而只是一种处理您习惯的方法。

Small Update: I noticed you also asked 'why' JQuery does not include the submit button in the serialize() output. 小更新:我注意到您还问过“为什么”,jQuery在serialize()输出中不包含Submit按钮。 This is default behaviour as specified in the HTML draft(s). 这是HTML草案中指定的默认行为。 As long as no submit button is pressed, it should not be included in the submitted (or serialized) data. 只要没有按下提交按钮,就不应将其包含在已提交(或序列化)的数据中。 The JQuery creators decided to honour that and not include it in the serialized string. JQuery创建者决定尊重这一点,并且不将其包括在序列化字符串中。

The "value" attribute of the <input> tag of type "submit" is meant to determine a label - the label of the submit button. 类型为“ submit”的<input>标签的“ value”属性用于确定标签-提交按钮的标签。 It is not meant to be sent. 它不是要发送的。

The alternative solution you ask for could be an <input> tag of type "hidden" that holds a value specific to the form it lives in. 您要求的替代解决方案可能是类型为“隐藏”的<input>标记,其中包含特定于其所居住表单的值。

Here's an example: 这是一个例子:

<form method="post">
    <input type="hidden" value="my_form" />
    <!-- Everything else goes here unchanged. -->
</form>

Then in PHP you can use something like the following: 然后在PHP中,您可以使用类似以下内容的东西:

if(isset($_POST["my_form"]))
{
    // Deal with the sent data.
}

It seems to be more elegant workaround to handle the click first and then the submit and combine the array using .serializeArray(); 首先处理单击,然后使用.serializeArray();提交并合并数组,似乎是一种更优雅的解决方法.serializeArray(); and .push . .push It works even with forms with more submit buttons: 即使具有更多提交按钮的表单也可以使用:

<script type="text/javascript">
$(document).ready(function() {

    var submitname;
    $("#submit_signup").click(function(event) { 
        submitname='submit_signup'; 
    });


    $('#myform').submit(function( event ){
        var postarray = $('#myform').serializeArray();
        postarray.push({name: submitname, value: 1});

        $.post('script/myscript.php',postarray, function(data){
                /* do what you need with data variable */
            })
    event.preventDefault(); // avoid to execute the actual submit of the form.
    })
})
</script>

In this case, your submit field should have id attribute "submit_signup". 在这种情况下,您的提交字段应具有id属性“ submit_signup”。

<input type="submit" id="submit_signup" name="submit_signup" value="Submit your form" />

You can check if it is post request in php code: 您可以检查它是否为php代码中的发布请求:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // ...
}

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

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