-4

i am trying to use toggle buttons to save response in db as yes or no. for some reason the only response i am getting is 'on'. even when i switch off the button. i tried searching for problem and got a match but the problem was asked for android platform.now i am stuck with no answer there where similar questions but none of them is useful for me at this moment. sharing the code down below.Thanks in advance for those who are going to suggest or provide a solution.i am using class handicap to save data into variable inside JQUERY and then send that variable to AJAX page to perform db operation.i am not sharing CSS for toggle as i don't think that is required right now. if u need any additional info, do inform me.this input is inside a form with method POST. i am using a submit button with id that is calling this JQUERY. html part

<div class="switch">
    <input id="cmn-toggle-4" class="cmn-toggle cmn-toggle-round-flat handicap" type="checkbox" name="handicap">
    <label for="cmn-toggle-4"></label>
    </div>

jquery

     $("#save-medical-1").click(function () {
        var m11 = $(".handicap").val();   
    alert(m11);
        $.ajax({
          url: "ajexupdate.php",
          type: "POST",
          data: {smsgs11: m11},
          dataType: 'text',
          cache: false,
          success: function (e) {
            //           alert(e);  
            $("#user_medical_form").html(e);
            $("#medidetail").modal('hide');
            $('body').removeClass('modal-open');
            $('.modal-backdrop').remove();

          }
        });
        return false;
      });
Archelese
  • 21
  • 9
  • To be honest, I can't work out what your question is. You say something is returning `on` but you haven't provided anything that would do so, nor do you seem to have anything to say what a return of `on` should do, or what another value would do. – Jonnix Jun 07 '17 at 08:31
  • why do you need ajax if you have a POST ? – t-n-y Jun 07 '17 at 08:31

2 Answers2

0

You can get value using ":checked" using jquery. eg.

if($("#cmn-toggle-4").is(":checked")){
m11="yes";
}
else{
m11="no";
}

and send it through ajax.

  • i added a alert aftes saving value in m11 . alert is always showing on no yes or no even after adding this checked code. – Archelese Jun 07 '17 at 08:46
  • thanks a lot. at that time, i didn't noticed, even when the code was sending 'on' state the, value was changes in DB and saved as 'yes' or 'no'. so this worked for me thanks a lot. – Archelese Jun 07 '17 at 09:23
-1

By writing a php command you are setting the initial value of that input into m11. You have to catch the client side value of input instead:

your code:

var m11 = '<?php echo $_POST['handicap']; ?>'; // always returns the initial value

Correct clien-side code:

var m11 = $(this).val();
Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82