vendredi 24 novembre 2017

Toggle Checkbox and send query in background to update database

As soon as someone toggles a checkbox. I want a the value sent to a php page for it to update it in my database.

enter image description here

HTML:

<div class="form-group">
  <div class="tg-optionbox">
    <span>Promotional Video</span>
    <span class="tg-iosstylcheckbox">
     <input type="checkbox" id="video" onclick="toggleClick()" name="video" value="video">
     <label for="video"></label>
      <div id="echo2"></div>
     </span>
   </div>
</div>

JQUERY:

$(document).ready(function(){
  $("input[type=checkbox]").click(function () {
      $.ajax({
          type: "POST",
          url: "get/update-feature.php",
          data: {
              value: $('input:checkbox:checker').val() // as you are getting in php $_POST['action1'] 
          },
          success: function(data) {
              $('#echo2').html(data); 
          }
      });
  });
});

PHP:

<?
$sent_value = $_POST['value'];

if($sent_value == 1){
        //update database
        echo "updated";
}else{
        //update database
}

        ?>

Currently, my jquery doesn't work. How I need it:

  • They click the orange checkbox toggle
  • Jquery recognises this and sends the checkbox value to my php page
  • my php page gets the value and puts it in the database



Aucun commentaire:

Enregistrer un commentaire