1 Replies - 1021 Views - Last Post: 13 December 2012 - 12:50 PM

#1 chrisp200  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 82
  • Joined: 18-November 08

Checkbox value always returning 'on'

Posted 13 December 2012 - 10:49 AM

I have a form on my website that looks like this:

 

   <div class="contactForm" id="contactForm">
                    <label for="name">First name</label>
                    <div id="name_error"></div>
                    <input type="text" name="name" id="name" />
                    
                    <label for="lastname">Last name</label>
                    <div id="lastname_error"></div>
                    <input type="text" name="lastname" id="lastname" />
                    
                    <label for="email">Email Address</label>
                    <div id="email_error"></div>
                    <input type="text" name="email" id="email" />
                    
                   <label for="company">Company</label>
                   <div id="company_error"></div>
                   <input type="text" name="company" id="company" />
                       <br />
                   <h4 style="color:#FFCD29; padding: 10px 0 10px 0;">I would like to receive: </h4>
                   <div id="casestudy_error"></div>
                   <input type="checkbox" id="checkbox" value="yes" style="float:left;" />
                   <label for="casestudy" style="float:left;">Case study</label>
                   <div id="checkbox_error"></div>
                     <br />
                   <div id="methodology_error"></div>
                   <input type="checkbox" id="checkbox2" value="yes" style="float:left;"/>
                   <label for="methodology" style="float:left;">Methodology</label>
                   <div id="checkbox_error"></div>
                      <br /><br />
                   
                    <input type="submit" value="Submit" name="Submit" onclick="emailform.errorCheck();" /><br />
                </div>




For my form validation I am using javascript


	formSubmission: function (){
		this.sendPage('contactForm', '_email/case_study.php', 'email='+document.getElementById('email').value+'&name='+document.getElementById('name').value+'&lastname='+document.getElementById('lastname').value+'&checkbox='+document.getElementById('checkbox').value+'&checkbox2='+document.getElementById('checkbox2').value+'&company='+document.getElementById('company').value);

checkCheck:function(){
		var checkbox = document.getElementById('checkbox').value;
		var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
		if(checkbox.match(illegalChars) || checkbox.length < 1){
			document.getElementById('checkbox_error').innerHTML = '<p class="required">You must check one of the options<p>';
			this.errorFree[0] = false;
		}else{
			this.errorFree[0] = true;
			document.getElementById('checkbox_error').innerHTML = '';
		}
	},
	check2Check:function(){
		var checkbox2 = document.getElementById('checkbox2').value;
		var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
		if(checkbox2.match(illegalChars) || checkbox2.length < 1){
			document.getElementById('checkbox_error').innerHTML = '<p class="required">You must check one of the options<p>';
			this.errorFree[0] = false;
		}else{
			this.errorFree[0] = true;
			document.getElementById('checkbox_error').innerHTML = '';
		}
	},




When the form is submitting the value of the checkbox is always 'on'

I don't have a lot of javascript experience. Any suggestions are appreciated!

This post has been edited by Dormilich: 13 December 2012 - 12:46 PM
Reason for edit:: fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Checkbox value always returning 'on'

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,250
  • Joined: 08-June 10

Re: Checkbox value always returning 'on'

Posted 13 December 2012 - 12:50 PM

the checkCheck() and check2Check() functions are superfluous. checkboxes always have a fixed value (the one you provide, unless you change it by JS). checking or unchecking a checkbox doesn’t change that.

whether a checkbox is checked or not can by tested through the checked property of the checkbox.

and as far I can read, the checkboxes’s values are "yes", not "on".

This post has been edited by Dormilich: 13 December 2012 - 12:50 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1