3

I have a jQuery function that takes the value from a PHP-generated check box and sends it over AJAX. The value is always a single word with nothing but letters by the way. Below is the script.

<script type="text/javascript">
    $(document).ready(function() {
        $("input:checkbox").on("click", function () {
            step = this.value;
            //document.getElementById("test").innerHTML = step;
            responseArray = [];

            xmlhttp=new XMLHttpRequest();
            xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                    responseArray = eval("(" + xmlhttp.responseText + ")");
                    document.getElementById("test").innerHTML = responseArray;
                }
            }

            xmlhttp.open("GET", "checkbox.php?step="+step, true);
            xmlhttp.send();
        });
    });
</script>

The above code results in the "ReferenceError: [this.value] is not defined". [this.value] is the actual value though which changes based on the box that is checked. If you notice the 5th line in the above code, when I don't comment that line out it displays the correct value in "test" for the step variable, so it's something after that. Below is the checkbox.php file completely simplified down to basically nothing and it still causes the error.

<?php       
    $step = $_GET["step"];
    echo "[" . $step . "]";
?>
4
  • Wow! It's been years since I see some code calling the XMLHttpRequest() object directly!! Commented Oct 30, 2012 at 22:13
  • Lol it's not like that in the actual file. I edited it down quite a bit so it wasn't as overwhelming. Commented Oct 30, 2012 at 22:18
  • Really weird, this is always something, not sure how that could happen in any scenario. have a screen shot of the error and the line where it actually errors out (maybe from chrome dev tools) Commented Oct 30, 2012 at 22:43
  • The line that it references as where the error occurs is the "responseArray = eval("(" + xmlhttp.responseText + ")");" line which would insinuate it's the AJAX result causing the problem, but I just can't see how that is. Commented Oct 30, 2012 at 22:52

1 Answer 1

3

As I can see your using Jquery, you should use JQuery's AJAX object. It simplifies the request a lot:

$.ajax({
  url: 'checkbox.php?step=',
  success: function(data) {
    alert(data);
  }
});

http://api.jquery.com/jQuery.ajax/

Now your problem seems to be that you're not getting the value of the checkbox through properly. Checkboxes are either on or off. Here's how to get the various parameters:

$(document).ready(function () {
    $("input:checkbox").on("click", function () {
        var checkboxID = $(this).attr("ID");        // Returns the ID of the checkbox
        var isChecked = $(this).is(':checked');     // Returns true or false

        alert($(this).attr("ID") + " is " + isChecked);
    });
});

So your final code might look something like:

$(document).ready(function () {
    $("input:checkbox").on("click", function () {
        var checkboxID = $(this).attr("ID");        // Returns the ID of the checkbox
        var isChecked = $(this).is(':checked');     // Returns true or false

        $.ajax({
           url: 'checkbox.php?step=' + isChecked ,
           success: function(data) {
             alert(data);
           }
         });
    });
});

(untested code) which would send a request to the url checkbox.php?step=true

And hello from 2+2 :D

Sign up to request clarification or add additional context in comments.

1 Comment

Why not just use this.id and this.checked?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.