-1

I have the following form which displays a checkbox:

<form>
    <input type="checkbox" class="custom-control-input" id="ch" value="y" required>
    <button id="CT" type="button" >Go</button>
</form>

And my AJAX script:

<script>
//ST
$('#CT').click(function(){
  var fullname = document.getElementById("fn").value;
  var username = document.getElementById("un").value;
  var emailAdd = document.getElementById("em").value;
  var password = document.getElementById("pd").value;
  var check = document.getElementById("ch").value;
  $.ajax({
    type:'POST',url:'ajax/signup.php',
    data:'fn='+fullname+'&un='+username+'&em='+emailAdd+'&pd='+password+'&ch='+check,
    beforeSend:function(){ $('#WAIT').show(); },
    success:function(data){ $('#HERE').html(data); }
  });
});
//SD
</script>

Lastly, here is my ajax/signup.php script:

<?php
$FN = $_POST["fn"];
$UN = $_POST["un"];
$EM = $_POST["em"];
$PD = $_POST["pd"];
$CH = $_POST["ch"];
if(empty($FN) OR empty($UN) OR empty($EM) OR empty($PD) OR $CH != "y"){
    echo""?>
    <script>
    $("form").addClass("was-validated");
    </script>
    <?"";
}
?>

How to check if the checkbox was validated on the client side?

Nobel
  • 17
  • 9
  • 2
    Possible duplicate of [How to read if a checkbox is checked in PHP?](https://stackoverflow.com/questions/4554758/how-to-read-if-a-checkbox-is-checked-in-php) – Nikita Kniazev Sep 09 '18 at 23:34

2 Answers2

0

If I understand your question correctly, you're looking for how to extract the value of the checkbox (as a boolean). This should be doable by calling checked as below:

var check = document.getElementById("ch").checked;

Sami Farhat
  • 1,164
  • 8
  • 12
0

Toggle value for ajax using ternary operator:

document.getElementById("ch").checked ? 'y' : 'n'