Page 1 of 1

multiple checkbox answers for one database field....maybe?

Posted: Thu Mar 05, 2009 4:42 am
by buzzby365
hi there.

i have a competition whereby users are asked to select a maximum of 5 correct things out of a choice of 12. those things need to be displayed in a database where i can see if the things that were selected are right or wrong. i have a value for the checkbox 'ckb' which i needed for the javascript to work (select 5, more than 5 will bring up an alert) but i need to display all 5 results. how to i do this?

i have gotten the javascript to work so people could only select a maximum of 5. that was based on all the check boxes having the same id. now i need to display the 5 results in the database

this is the script that only allows the user to select up to 5 checkbox values

Code: Select all

 
<script type="text/javascript">
function chkcontrol(j) {
var total=0;
for(var i=0; i < document.form1.ckb.length; i++){
if(document.form1.ckb[i].checked){
total =total +1;}
if(total > 5){
alert("Please Select only five")
document.form1.ckb[j].checked = false ;
return false;
}
}
} </script>
 
this is the section of code that deals with the checkboxes. they have the same name but the values are different.

Code: Select all

<table border='0' width='471' cellspacing='0' cellpadding='1' align=center>
<tr bgcolor="#ffffcc"><td width="59"> </td><td width="290" ><b>Choice</b></td></tr>
<tr bgcolor="#f1f1f1"><td align="center"><input type=checkbox name=ckb value="Sweet shop" onclick="chkcontrol(0)";></td><td >In the Sweetshop</td></tr>
<tr bgcolor="#ffffff"><td align="center"><input type=checkbox name=ckb value="Playing Chess" onclick="chkcontrol(1)";></td><td >Playing Chess </td></tr>
<tr bgcolor="#f1f1f1"><td align="center"><input type=checkbox name=ckb value="Sweet Peas Dolls" onclick="chkcontrol(2)";></td><td >With the Sweet Peas Dolls</td></tr>
<tr bgcolor="#ffffff"><td align="center"><input type=checkbox name=ckb value="Hiding between the Hamleys Bears" onclick="chkcontrol(3)" ;></td><td >Hiding between the Hamleys Bears</td></tr>
<tr bgcolor="#f1f1f1"><td align="center"><input type=checkbox name=ckb value="Racing cars on the Scalextric track" onclick="chkcontrol(4)";></td><td >Racing cars on the Scalextric track</td></tr>
<tr bgcolor="#ffffff"><td align="center"><input type=checkbox name=ckb value="Having lunch in the Regal Tea Cafe" onclick="chkcontrol(5)";></td><td >Having lunch in the Regal Tea Cafe</td></tr>
<tr bgcolor="#f1f1f1"><td align="center"><input type=checkbox name=ckb value="Trying on a Hamleys Medieval Dress Costume" onclick="chkcontrol(6)";></td><td >Trying on a Hamleys Medieval Dress Costume</td></tr>
<tr bgcolor="#ffffff"><td align="center"><input type=checkbox name=ckb value="Flying a remote controlled plane" onclick="chkcontrol(7)";></td><td >Flying a remote controlled plane </td></tr>
<tr bgcolor="#ffffff"><td align="center"><input type=checkbox name=ckb value="Playing Nerf Tag with Alice" onclick="chkcontrol(8)";></td><td> Playing Nerf Tag with Alice</td></tr>
<tr bgcolor="#ffffff"><td align="center" ><input type=checkbox name=ckb value="Playing with an Infinite Bubble Gun" onclick="chkcontrol(9)";></td><td >Playing with an Infinite Bubble Gun</td></tr>
<tr bgcolor="#ffffff"><td align="center" ><input type=checkbox name=ckb value="Peeking out behind the Lego Clone Trooper" onclick="chkcontrol(10)";></td><td >Peeking out behind the Lego Clone Trooper</td>
</tr>
</table>
 
now in the database i want to store all 5 results. therefore each row will have the intro info (name, address, email) but it will then have the values of the 5 checkbox values. how can i achieve this?

any helpers on this please?

Re: multiple checkbox answers for one database field....maybe?

Posted: Thu Mar 05, 2009 5:24 am
by BomBas
Didn't fully understand, but from what I did understand I think you should use foreach.

Code: Select all

foreach( $_POST['group'] as $v )
{
// $v = checkboxes' value
mysql_query('INSERT INTO ... VALUES ...');
}

Re: multiple checkbox answers for one database field....maybe?

Posted: Thu Mar 05, 2009 5:35 am
by buzzby365
what i want to do is store the 5 checkbox values. the user has a choice. the checkbox name is 'ckb'. i the database i want to see the name of the person and the 5 values that the person has chosen. each person's choice of checkbox will be different therefore the database needs to show all values for each person. how do i do this.

my query is this so far

Code: Select all

$query = "INSERT INTO competition_easter (fname, lname, housenamenumber, street, county, country, postcode, email, ckb, terms, newsletter) VALUES ('$fname', '$lname', '$housenamenumber', '$street', '$county', '$country', '$postcode', '$email', '$ckb', '$terms', '$newsletter')";
where 'ckb' is the check box value. i need to each (of the 5) ckb value per name

Re: multiple checkbox answers for one database field....maybe?

Posted: Thu Mar 05, 2009 8:16 am
by buzzby365
hi there

the checkboxes relates to a competition that has 12 answers. the value of the checkbox is the worded answer rather than a number. hence

Code: Select all

<td align="center"><input type=checkbox name=ckb value="Sweet shop" onclick="chkcontrol(0)";></td><td >In the Sweetshop</td>
the onclick relates to a javascript that counts how many boxes have been clicked. once over 5 an alert box comes up.

now what i am stuck on is how to get the 5 answers into the database. atm the checkbox name is 'ckb'. is this ok or does it need changing? each person will have 5 answers therefore the database needs to have 5 columns (1 for each answer) but each answer is down to the individual.

how do i code this so the database reflects this?