Al Hennessey
Al Hennessey

Reputation: 2445

Error when checkbox not checked

I have this ajax request that is sending some form data to an external php script and then returning the result, the problem is that i get the error

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

If i try to send the form without checking either of the two checkboxes.

Here is the ajax request

$('#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();
});

And here is the form

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

Any ideas?

Upvotes: 0

Views: 903

Answers (1)

Robert
Robert

Reputation: 2824

Check box that are not checked will be not set when use $_POST so use

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;

Upvotes: 1

Related Questions