Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Handling checkboxes

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a simple form which includes a series of checkboxes for user input -
<form name="form" action="processAddStudent.jsp" method="post">
...
...
input type="checkbox" name="english" value="1">English
input type="checkbox" name="maths" value="1">Maths
input type="checkbox" name="science" value="1">Science
input type="checkbox" name="technology" value="1">Technology
...
-------------------
Notice I have assigned a value of "1" to all my checkboxes - however im not sure if this is correct. I want a value of 1 to be assigned to the variables english, maths, science technology only when they have been checked - indicating that 1 - means true (they study that subject) and 0 means false (they don't study that stubject). I then want to pass these assigned int values to my process page, which will retrieve them and add them to a database which has fields english, maths, science, technology all of type int?
Does anyone know how to handle the checkbox part?
Do I determine if it has been checked on the orginal html form, or on the process page? And how do I do this?
Many thanks in advance!
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you post this it will say on or off for the values. On is when they are checked.
Eric
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"When an INPUT type=checkbox element is selected, a name/value pair is submitted with the FORM. The default value of INPUT type=checkbox is on."
So, you will get parameters on request only for one that checked. Unchecked are not submitted at all.
You also could do something like:
<input type=checkbox name="ch_math" onchange="document.getElementById(this.name).value=(this.checked)?1:0;">
<input type=hidden id="ch_math" value="0" name="math">
 
I didn't say it. I'm just telling you what this tiny ad said.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic