未选中复选框时出错

时间:2014-03-04 22:16:35

标签: php jquery html ajax json

我有这个ajax请求,它将一些表单数据发送到外部php脚本然后返回结果,问题是我收到了错误

Uncaught SyntaxError: Unexpected token < index.php
hr.onreadystatechange

如果我尝试发送表单而不检查两个复选框中的任何一个。

这是ajax请求

$('#ind_reg_submit').on('click', function (e) {



    var vars = $("#ind_register_form").serialize(); // the script where you handle the form input.
    //alert("gu");
    var hr = new XMLHttpRequest();
    hr.open("POST", "scripts/index/ind_register_submit.php", true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var data = JSON.parse(hr.responseText);
            for(var obj in data){
                if(obj == "error"){
                    alert(data[obj]);

                }else if(obj == "success"){
                    alert(data[obj]);
                    window.location.replace("http://localhost/shoutr/registration_complete.php");
                }
            }
            //alert(hr.responseText);
            //location.load();
        }
    };
    hr.send(vars);
    //results.innerHTML = "requesting...";
    event.preventDefault();
});

这是表格

<form method="post" id="ind_register_form">
                    <input type="text" class="ind_reg_text" name="ind_reg_firstname" placeholder="Firstname" />
                    <input type="text" class="ind_reg_text" name="ind_reg_lastname" placeholder="Lastname" />
                    <input type="text" class="ind_reg_text" name="ind_reg_email" placeholder="Email" />
                    <input type="password" class="ind_reg_text" name="ind_reg_password" placeholder="Password" />

                    <input type="checkbox" name="ind_reg_check_terms" class="ind_reg_check" value="Yes"/>
                    <span id="ind_reg_label">I agree to the Terms and Conditions</span>
                    <input type="checkbox" name="ind_reg_check_privacy" class="ind_reg_check" value="Yes"/>
                    <span id="ind_reg_label">I agree to the Privacy Policy</span>
                    <div class="ind_reg_submit" id="ind_reg_submit">Register</div>
                </form>

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

使用$ _POST时,将不会设置未选中的复选框 所以使用

if(isset($_POST['ind_reg_check_terms']))
    $ind_reg_check_terms=1;
else
    ind_reg_check_terms=0;

if(isset($_POST['ind_reg_check_privacy']))
    $ind_reg_check_privacy=1;
else
    ind_reg_check_privacy=0;