57 users online (1 members and 56 guests)  


  Results 1 to 7 of 7

Related

  1. checkbox madness    Forum: HTML Forum
    Replies: 0
  2. How to Decode the data from Database using database handler?    Forum: Search Engine Optimization - SEO - Forum
    Replies: 1
  3. Return Form Answers - Perl    Forum: CGI Perl Forum
    Replies: 8
  1. #1
    buzzby247's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2009
    Posts
    4

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

    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:
     <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:
    <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?

    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:
    $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 display each (of the 5) ckb value per name

    any help on this one please?

  2. #2
    buzzby247's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2009
    Posts
    4

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

    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:
    <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?

  3. #3
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

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

    Question: Are you viewing the answers directly in the database, or are you viewing them on a php page?

  4. #4
    buzzby247's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2009
    Posts
    4

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

    the answers will be viewed in the database by myself (admin). only i can compare the answers to what has been submitted. its a compo so know one will be told if they got it right or wrong. just a notice to say 'thanks for taking part'

  5. #5
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

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

    The easiest way to go is to put a comma-delimited list in the field in the database. To do this, you need to make the following changes/additions to your code:

    Change the input names to say name=ckb[] so that the results will be placed in an array.

    Then before the query is created, put in the following line:
    Code:
    $ckb = implode(",",$_POST['ckb']);

  6. #6
    buzzby247's Avatar
    New User

    Status
    Offline
    Join Date
    Mar 2009
    Posts
    4

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

    hi there,

    thanks for that. now the thing is that even tho it works, the condition of only being able to pick 5 frok the selection of 12 doesnt. how then do i put that stipulation in?

    when the name=ckb the javascript validation worked. now the name=ckb[] the validation doesnt. personally i prefer to use php validation. but i dont know how to write a php validation for only selecting up to 5 checkboxes. any help on this please?

  7. #7
    jthayne's Avatar

    Status
    Offline
    Join Date
    Aug 2008
    Location
    Texas
    Posts
    508

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

    Change
    Code:
    document.form1.ckb[i].checked
    to
    Code:
    document.form1["ckb[]"][i].checked
    wherever it is in the code (2 lines?)