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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

steelseth12Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.