0

I've got the following script:

jQuery('.checkbox-for-accept').click(function(){
            var value= jQuery(this).is(':checked');
            jQuery.ajax({
                url: '',
                type: 'POST',
                data: {check:value},
                success: function(data){
                    alert(data);
                }
            });
        });

However, when clicking the checkbox nothing seems to happen (no alert, nothing). I've tried to find solutions in numerous places by now, and just can't seem to figure out, what is wrong.

My goal is to send it to the current page, which is why i have left the url blank (i read that somewhere, that was how i should do it).

I simply can't figure out, what I've done wrong?

as a note: i've also found numerous questions and answers in here, regarding something similar, but nothing has helped me so far.


Edit 1: completely forgot the relevant HTML as pointed out. The click event is for a checkbox to see whether the checkbox is checked or not. The checkbox is used to accept terms of use before allowing a download.

<input type="checkbox" id="checkbox1" class="checkbox-for-accept" name="checkbox" value="acceptchecked" />
Chri.s
  • 1,386
  • 1
  • 12
  • 23
  • Will you show your HTML code ? – Shailesh Dec 11 '16 at 18:25
  • You don't have any url for ajax so how can it be success? – Ruhul Amin Dec 11 '16 at 18:26
  • @Shailesh of course, that was my mistake that i forgot it. It's now edited into the original question. – Chri.s Dec 11 '16 at 18:45
  • @RuhulAmin well, honestly i dont know. I read in another question, that for the post to be sent to the current page, I just had to leave the url out completely. Any suggestions as to correct this, and make it happen for the current page in which the Ajax was sent? – Chri.s Dec 11 '16 at 18:45
  • @Taplar i'll be damned. To check if it was executed at all, I inserted a `console.log(value)` after the variable declaration to check it's value, and afterwards the alert of the Ajax was actually fired. However, the alert didn't contain the value of the var, but instead it showed the html of the entire page ? Is that due to the lacking url? and if so, how do i fix this? – Chri.s Dec 11 '16 at 18:50
  • according to this post http://stackoverflow.com/a/8423235/2048391 you should listen to change event, like this $(".checkbox-for-accept").change(function() { if(this.checked) { //Do stuff } }); – jyrkim Dec 11 '16 at 18:52
  • Html code should be like if you are using checkbox – Shailesh Dec 11 '16 at 18:53
  • @jyrkim: i've just changed it to `jQuery('.checkbox-for-accept').on("change", function(){ if(this.checked){ var value= 1; } else { var value = 0; }; jQuery.ajax({ .... // code continues as before` however without any changes. – Chri.s Dec 11 '16 at 18:59
  • @Taplar it is php and it makes sense i think, i just dont have any idea as to how i'm supposed to fix it (sadly). With regard to the checkbox state it should not be checked from start. – Chri.s Dec 11 '16 at 19:03
  • Well, first question, does it *have* to hit the same page? Can it not hit another page that does only the logic you expect? Otherwise I suppose one way for hitting the same page would be to check if `$_POST['check']` exists to tell if it came from this request or not. Which you would need to do first, do your logic, and then possibly `exit()` or something so none of the following html logic would be returned. Personally, i'd use a different endpoint. – Taplar Dec 11 '16 at 19:06
  • @Taplar Well no, i guess it doesn't have to be the same page. I just tried making the following url: `url: '/checkbox-checked.txt` and that just returned a completely empty alert. I also tried with .php instead of .txt with no difference. – Chri.s Dec 11 '16 at 19:20

0 Answers0