JavaScript checkbox help value PHP AJAX

I need help getting the value of a checkbox in a form I have. The HTML code for the checkbox is:

<INPUT TYPE="CHECKBOX" NAME="agreement" id="agreement" VALUE="iagree">

The JavaScript code is:

      var obj15 = document.getElementById("agreement");
      var agreement = obj15.value;

The PHP code to validate this is:

$agreement=$_POST['agreement'];
if ($agreement=="")
{
echo 'document.getElementById("emailsent").innerHTML = "You must agree to our Legal Notice and Privacy Statement before submission.";';
return;
}

Now, the issue is that I cant get the JavaScript right to get the value of the checkbox. I need some help with this. Thanks. Also, if when you help me, please let me know what the value of false would be, is it an empty string?.... "" ? Thanks
jcbodyworksAsked:
Who is Participating?
 
steelseth12Connect With a Mentor Commented:
The value of the checkbox is always the value you define. VALUE="iagree"

Now to check if it is selected or not you need to check if it is checked. If it is checked it will return true , if it is not checked it will return false.

Here is a little example of how this would work


<script language="javascript">
            
       function check() {
        
        var obj15 = document.getElementById("agreement");
      var agreement = obj15.checked;
        
        if(agreement == true) {
        
              alert("Checkbox is checked");
            
      }else{
      
            alert("Checkbox is NOT checked");
      }
        
      
            }

</script>
<INPUT TYPE="CHECKBOX" NAME="agreement" onChange="check()" id="agreement" VALUE="iagree">
0
 
gops1Commented:
Have you used the same id multiple times --> (id="agreement" ). Verify, if that is the case try giving unique ids and check them
0
 
gops1Commented:
Another thing is your javascript code a part of the file that is called by Ajax. In that case you should move your javascript code to some other js file.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.