Col*_*ole 5 html php jquery serialization
索引.php:
<form id = "regform" action = "registration.php" method = "POST">
    <label id = "namelabel">Username: </label> <input type = "text" name = "username" class = "username" maxlength = "40" placeholder = "Enter a Username"><br>
    <label id = "namelabel">Password: </label> <input type = "password" class = "password" name = "password" placeholder = "Enter Password"><br>
    <label id = "namelabel">Gender: </label>
    <input type="radio" name="gender" value = "Male"> Male
    <input type="radio" name="gender" value = "Female"> Female
    <label id = "namelabel">Email: </label> <input type = "text" name = "email" class = "email" placeholder = "Enter your Email"><br>
    <a href="#" title="Register" id = "register" style = "margin-left:25px;">Register</a>
</form>
$("#register").click(function () {
    $.post($('#regform').attr("action"), $('#regform').serializeArray(), function (data) {
        if (data == 'checkradio') {
            $('#regmsg').html('Please choose a gender.');
        }
    });
});
服务器端:
$required_fields = array('username','password','gender','email');
foreach($_POST as $key =>$value) {
    if(empty($value) && in_array($key, $required_fields) === true){
        echo 'checkradio';
    }
}
它序列化除单选按钮之外的所有内容。问题是什么?
正如@undefine 告诉你的,未选中的复选框和单选按钮不会被发送。
如果您不希望性别字段处于默认选中状态,请在单选按钮之前添加一个具有相同名称和空值的隐藏输入字段:
<input type="hidden" name="gender" value="" /> 
表单是按顺序处理的,因此如果选中单选,其值将覆盖隐藏的输入值