1

I have this form:

<form name="settings" action="../ajax/account/account_ajax.php?token=<?php echo $token; ?>" 
      class="form-horizontal" method="post">

    <input type="hidden" name="set_settings" value="1">
    <div class="row">
        <div class="col-md-5">
            <label class="checkbox-custom">
                <input name="earning_sound"
                <?= ($settings->earning_sound) ? 'checked="checked"' : ''; }?> 
                value="1"  type="checkbox">
                <i class="fa fa-fw fa-square-o checked"></i>
                Earning Notification Sound
            </label>
            <button type="submit" class="btn btn-primary ">
                <i class="fa fa-edit"></i> Save
            </button>
        </div>
    </div>

</form>

How I can save with value 1 when I check and how I can save with value 0 when I uncheck!

xanadev
  • 751
  • 9
  • 26
John Doe
  • 29
  • 1
  • 7

3 Answers3

0

Get the values of checkbox with JS:

var checkboxValue = document.getElementById("myCheck").checked // return true or false

And give id for your checkbox.

stefo91
  • 618
  • 6
  • 16
0

In case of checkbox the value only gets post if the checkbox is checked. So u need to check whether the checkbox field value is set in PHP using below code while saving it to database.

$checkboxValue = isset($_REQUEST['earning_sound']) ? 1 : 0;
Kawaljeet Singh
  • 357
  • 1
  • 5
0
        <form name="settings" action="../ajax/account/account_ajax.php?token=<?php echo $token; ?>" class="form-horizontal" method="post">
            <input type="hidden" name="set_settings" value="1">
            <div class="row">
                <div class="col-md-5">
                    <label class="checkbox-custom">
                        <input name="earning_sound" class="earning_sound" type="checkbox" value="<?php echo ($settings->earning_sound == 1)? '1' : '0' ?>" <?php echo ($settings->earning_sound == 1)? 'checked' : '' ?>  />
                        <i class="fa fa-fw fa-square-o checked"></i>
                        Earning Notification Sound
                    </label>
                    <button type="submit" class="btn btn-primary "><span class="fa fa-edit"></span> Save</button>
                </div>
            </div>
        </form>

// this will help you to save the value as 1 when checked or 0 when unchecked

//add the following code to your js file

$(".earning_sound").click(function() {
    $(this).val(this.checked ? 1 : 0);          
});