PHP & JS & HTML onClick event

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Hey guys, its me again.

So I have a checkbox and its styled as iPhone style and when I click it I want it to run an ajax request.

So HTML code:
HTML:
 <label class="switch" style="padding:10px;">
                                 <input  <?php if($maintenance == 1){ echo 'checked'; } ?> type="checkbox">
                                 <span id="click"></span>
                              </label>
JavaScript:
Code:
      $(document).ready(function(){
          $('label.switch').click(function(){
              $.ajax({
                  url:'system/js/maintenance.php',
                  type:'POST',
                  success:function(result){
                      alert('done');

                  }
              });
          });
      });

And it clearly not doing the request, I'm showing XMLHTTP Requests on console and nothing shows.

I also tried $('input') , $('#click') and such things but it didn't work.

Any clues?

Thanks
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Code:
$(document).ready(function(){
          $('#checkbox').change(function(){
              $.ajax({
                  url:'system/js/maintenance.php',
                  type:'POST',
                  success:function(result){
                      alert('done');

                  }
              });
          });
      });
Still don't work.
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
Code:
$(document).ready(function(){
          $('#checkbox').change(function(){
              $.ajax({
                  url:'system/js/maintenance.php',
                  type:'POST',
                  success:function(result){
                      alert('done');

                  }
              });
          });
      });
Still don't work.
Most likely because your HTML and JS is fucked.

Code:
<input <?= $maintenance ? 'checked' : ''; ?> type="checkbox" id="maintenance" />
PHP:
$(document).ready(function() {
   // .on(event, fn) instead of .event(fn)
   $('#maintenance').on('change', function() {
     $.ajax({
       url: 'system/js/maintenance.php',
       type: 'POST',
       /*
        * Both are deprecated
        *
        * success: fn,
        * error: fn
        */
     }).done(function(res) {
       // ...
     }).fail(function(res) {
       // ...
     });
   });
});
Unsure if this will fix it.
 
Last edited:

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Most likely because your HTML and JS is fucked.

Code:
<input <?= $maintenance ? 'checked' : ''; ?> type="checkbox" id="maintenance" />
PHP:
$(document).ready(function() {
   // .on(event, fn) instead of .event(fn)
   $('#maintenance').on('change', function() {
     $.ajax({
       url: 'system/js/maintenance.php',
       type: 'POST',
       /*
        * Both are deprecated
        *
        * success: fn,
        * error: fn
        */
     }).done(function(res) {
       // ...
     }).fail(function(res) {
       // ...
     });
   });
});
Unsure if this will fix it.
Nope not worked.
 

Users who are viewing this thread

Top