JavaScript checkbox help value PHP AJAX

Posted on 2007-10-21
Last Modified: 2008-01-09
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:

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

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
Question by:jcbodyworks
    LVL 20

    Accepted Solution

    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");
                alert("Checkbox is NOT checked");

    <INPUT TYPE="CHECKBOX" NAME="agreement" onChange="check()" id="agreement" VALUE="iagree">
    LVL 17

    Expert Comment

    Have you used the same id multiple times --> (id="agreement" ). Verify, if that is the case try giving unique ids and check them
    LVL 17

    Expert Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now