?
Solved

JavaScript checkbox help value PHP AJAX

Posted on 2007-10-21
3
Medium Priority
?
1,912 Views
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:

$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
0
Comment
Question by:jcbodyworks
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 2000 total points
ID: 20119210
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
 
LVL 17

Expert Comment

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

Expert Comment

by:gops1
ID: 20120903
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 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