check multiple checkbox checked using array values

Posted on 2011-03-24
Last Modified: 2012-05-11
below is the code for multiple checkbox, which is working perfectly the way i want.. like if you select doesnt matter other check box gets unchecked... and when you check other checkboxes doesnt matter checkbox gets unchecked... so its like doesnt matter v/s other checkbox... this is just explanation of my code...
Now what new is i want...

I have the edit page.. where i fetch the values from database.. for marital status which is having 4-5 checkbox.. its  values are coming in the form of array... like

Doesnt matter, Never Married, Divorced
So my question is how do i check the respective checkboxes based on the value i fetch from database and matching it with checkbox.. and only problem i have is my values for all checkboxes are coming  ..separated by comma... so how to split and do the task...
below is the current code..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var clearTextWhenOtherSelected = true;
function list(c,n,z)
    document.getElementById('data_marital_part').value = '';
    clearTextWhenOtherSelected = false;
    if(s.indexOf(n)<0) s+=','+n;

  if (s.substring(2) == z) s=s.substring(2);

function txtrep() {
var newtext = "Second text here";
document.getElementById('data_marital_part').value = newtext;
clearTextWhenOtherSelected = true;

<form name="InstantSMS1">
<h1>Marital Status :</h1><br />
<input name="mStatus" type="checkbox" id="doesntmatter" value="Doesn't Matter" onclick="r=document.getElementsByName('mStatus');for(var i=0;i<r.length;i++) if(r[i]!=this)r[i].checked=r[i].checked&&!this.checked;list(this,'Do Not Matter');txtrep()" />Doesn't Matter<br />
<input name="mStatus" type="checkbox" value="Never Married" onclick="if(this.checked) document.getElementsByName('mStatus')[0].checked=false;list(this,'Never Married');" />Never Married<br /> 
<input name="mStatus" type="checkbox" value="Divorced" onclick="if(this.checked) document.getElementsByName('mStatus')[0].checked=false;list(this,'Divorced');" />Divorced<br /> 
<input name="mStatus" type="checkbox" value="Widowed" onclick="if(this.checked) document.getElementsByName('mStatus')[0].checked=false;list(this,'Widowed');" />Widowed<br /> 
<input name="mStatus" type="checkbox" value="Separated" onclick="if(this.checked) document.getElementsByName('mStatus')[0].checked=false;list(this,'Separated');" />Separated<br /> 
<input name="mStatus" type="checkbox" value="Annulled" onclick="if(this.checked) document.getElementsByName('mStatus')[0].checked=false;list(this,'Annulled');" />Annulled<br /> 
<br />
<br />
<input type="text" name="maritalstatusdata" id="data_marital_part" value="" />

Open in new window

Question by:global_expert_advice
  • 4
LVL 40

Accepted Solution

RQuadling earned 500 total points
ID: 35211010
Why are you using a checkbox rather than a radio group?

Checkboxes are normally for discrete/unconnected elements.


[ ] Do you want $1,00,000?
[ ] Do you eat dogs?

Radio groups are for things like...

Marital Status
(*) Doesn't matter
( ) Never married
( ) Divorced
( ) Widowed
( ) Separated
( ) Annulled
( ) Living in sin and happy as a pig in shit!

For a radio button to be in a "group", the name must be the same.

Try this code as an example. Just save it as form.html and load it - even from your local drive - no web server required.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Checkbox's vs Radio Groups</title>
<form method="get">
<form name="InstantSMS1">
<input name="likes[money]" type="checkbox" id="money" value="1" /> Do you like $1,000,000?<br />
<input name="likes[dogs]" type="checkbox" id="dogs" value="1" /> Do you eat dogs?<br />

<input name="maritalStatus" type="radio" id="ms1" value="Doesn't Matter" checked="checked" /> Doesn't Matter<br />
<input name="maritalStatus" type="radio" id="ms2" value="Never married" /> Never married<br />
<input name="maritalStatus" type="radio" id="ms3" value="Divorced" /> Divorced<br />
<input name="maritalStatus" type="radio" id="ms4" value="Widowed" /> Widowed<br />
<input name="maritalStatus" type="radio" id="ms5" value="Separated" /> Separated<br />
<input name="maritalStatus" type="radio" id="ms6" value="Annulled" /> Annulled<br />
<input name="maritalStatus" type="radio" id="ms7" value="Living in sin and happy as a pig in shit!" /> Living in sin and happy as a pig in shit!<br />

<input type="submit" />
If you fill the form in, and click submit, you'll see something like this on the URL.<br />
<pre>?likes%5Bdogs%5D=1&maritalStatus=Never+married</pre><br />
If you are using PHP, then your $_GET would looke like ... <br />
<pre>$_GET['likes']['dogs'] = 1
$_GET['maritalStatus'] = 'Never married'</pre>

Open in new window


Author Comment

ID: 35211048
radio button is not required... as i want to pass the checkbox values using concatenating to string format... like ,2,3,4,5

also my question is how to make the checkbox checked by matching its value with the string value... it can be matched and done.. if the fetching value is single... like 2 but values are 2,3,4,5
so if checkbox value is 3 ... and if it matched with fetched value... it should be checked...
LVL 17

Expert Comment

ID: 35229374
Possibility here it depends upon how are you getting the data from the database, I mean the server end and how you are handling it.

I suggest implementing it using the server technology rather then at the client side, a easy approach. I its agreeable then pls mention the server technology you are using.
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.


Author Comment

ID: 36420452
I've requested that this question be deleted for the following reason:

no specific answer, i solved it myself thanks

Author Comment

ID: 36504600

my question was realed to javascript function for checking and unchecking with few options, but the expert comments where not specific, infact i provided full coding too but they didnt able to understand and  may i m not able to explain things. Thats the reason i asked to  close this question

Author Closing Comment

ID: 36504761

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SVG Star 4 18
Calculate days between two dates 7 38
Text in block not displaying properly 2 17
Datepicker in PHP 9 21
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

747 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

10 Experts available now in Live!

Get 1:1 Help Now