简体   繁体   English

AJAX POST 不发送所有数据

[英]AJAX POST not sending all data

I have the following HTML code within a form and the top one (policy) sends in post but the second one does not (mfa).我在表单中有以下 HTML 代码,顶部的(政策)在帖子中发送,但第二个没有(mfa)。 I'm lost at why this is happening, any ideas?我不知道为什么会这样,有什么想法吗?

                                    <label class="switch switch-flat">
                                        <input class="switch-input" type="checkbox" id="policy" method="post" name="policy" <?php if($row['policy'] === '1') echo 'checked="checked"';?> />
                                        <span class="switch-label" data-on="Yes" data-off="No"></span>                                      
                                        <span class="switch-handle"></span> 


                                    </label>
                                    <label for="policy">Does the vendor organization have a written Information Security policy?</label>
                                    </td>

                                </tr>
                                <tr>
                                    <td>
                                    <div class="col-md-12">

                                    <label class="switch switch-flat">
                                        <input class="switch-input" type="checkbox" name="mfa" id="mfa" method="post" <?php if($row['mfa'] === '1') echo 'checked="checked"';?> />
                                        <span class="switch-label" data-on="Yes" data-off="No"></span>                                      
                                        <span class="switch-handle"></span> 


                                    </label>
                                    <label for="mfa">Do they support and utilize multi-factor authentication?</label>
                                    </td>

Here's my ajax to send the post data.这是我发送帖子数据的ajax。 When I browse in the Dev tools for headers being sent I see a lot of the other HTML names going but most of the ones listed as toggle checkboxes aren't being sent except for the top one 'policy'.当我在 Dev 工具中浏览要发送的标头时,我看到许多其他 HTML 名称正在运行,但除了最上面的“策略”之外,大多数列为切换复选框的名称都没有发送。

<script type='text/javascript'>
    /* attach a submit handler to the form */
$('#update').submit(function(e){
    e.preventDefault();
    $.ajax({
        url:'updateVendor.php',
        type:'post',
        data:$('#update').serialize(),
        success:function(){
            //whatever you wanna do after the form is successfully submitted
        }
    });
});
</script>

A checkbox is only considered to have a value when it is checked.复选框仅在被选中时才被认为具有值。 Otherwise, it is not included in the submitted form data.否则,它不会包含在提交的表单数据中。

From the MDN website :MDN 网站

Note: If a checkbox is unchecked when its form is submitted, there is no value submitted to the server to represent its unchecked state (eg value=unchecked);注意:如果一个checkbox在提交表单时未勾选,则没有提交给服务器的值代表其未勾选状态(例如value=unchecked); the value is not submitted to the server at all.该值根本不提交给服务器。 If you wanted to submit a default value for the checkbox when it is unchecked, you could include an <input type="hidden"> inside the form with the same name and value , generated by JavaScript perhaps.如果您想在未选中时为复选框提交默认值,您可以在表单中包含一个<input type="hidden">具有相同namevalue ,可能由 JavaScript 生成。

A trick to circumvent this limitation is to include a hidden input with a default value.规避此限制的一个技巧是包含具有默认值的隐藏输入。 Eg例如

<input name="mfa" type="hidden" value="0">
<input name="mfa" type="checkbox" value="1">

With this approach, a value for mfa will always be submitted with the form data.使用这种方法, mfa的值将始终与表单数据一起提交。

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

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