Jump to content

Multiple select Check Boxes


Fallen_angel

Recommended Posts

I am needing to make a section in a form that allows for multiple check boxes to be selected and then and most importantly it has to be writen to a database .  basicly what i need to do is as follows

the user is asked a question , which they can select up to 3 checkboxes out of a total of 15 which they feel describe their experiance best , the three selections whatever they may be are writen into the fields data1, data2, data3, in the database ( regardless of thier name value  on the forum)

Now I have figured out one way I could do this however I am positive that it is the worst solution for me ,  what the idea was ( and again this is something I really don't wanna do ) is to have a field in the database for each of the 15 selections for the different multiple choice questions and then writing a 1 or 0 to each field ( 0 if unchecked , 1 if checked ) this ofcourse is not prefered because I will have to check each and every feild when doign a search over the database , and also it's going to make my table over 100 collums wide  so yeah DEFINATLY not the option I want to go with ,

what I was thinking though is that there should be some way for me to do with with a php array or something ,

I would really really apreciate it if somone could assist me in doing this or point me in the direction of a tutorial that can show me








Link to comment
Share on other sites

I am not sure how to do it in check boxes...
but, I do know how to do it in a dorpdown menu.

you do....

Favorite Ice Cream:
<select name=icecream>
<option value=chocolate>Chocolate</option>
<option value=vanilla>Vanilla</option>
<option value=strawberry>Strawberry</option>
</select>

Favorite Color Sprinkles:
<select name=sprinkles>
<option value=chocolate>Chocolate</option>
<option value=rainbow>Rainbow</option>
</select>

Holder:
<select name=holder>
<option value=cone>Cone</option>
<option value=cup>Cup</option>
</select>

and, then, to insert you would just insert $_POST['icecream'], or, '$_POST['holder']' into the column.

example, you pick, Chocolate, Rainbow, Cone.
it will insert 'Chocolate' into the Ice Cream column..
then rainbow into the sprinkles, and cone into the holder.


i know you want checkboxes, but, there are some advantages to drop downs.. such as you don't need to add a check to see if something isn't selected..
and, they look a bit nicer.
Link to comment
Share on other sites

Thanx for your help but that not exactly what I was looking to do ,  basicly I would like to be able to do
one of two things , the preferable is to check which boxes are ticked and then slot them into data1 data2 and data3 in the database
my other alternative and less prefered is to put them all into one field and seperate then values with a /
the script bellow pretty much does what i want except it echo's the responce in an alert when you click the input button what I need this script to do is run the script as I submit the form , and then pass the value's over to the processing page so that  I can assign them to a variable and slot em into the database

using your example I would liek my users to be able to select a mix of chocolate and strawberry icecream not just one or the other which is the part that has got me a bit stumped


I will split it up so that I don't get a forum error this time

[code]        function getSelected(opt) {
            var selected = new Array();
            var index = 0;
            for (var intLoop = 0; intLoop < opt.length; intLoop++) {
              if ((opt[intLoop].selected) ||
                  (opt[intLoop].checked)) {
                  index = selected.length;
                  selected[index] = new Object;
                  selected[index].value = opt[intLoop].value;
                  selected[index].index = intLoop;
              }
            }
            return selected;
        }

        function outputSelected(opt) {
            var sel = getSelected(opt);
            var strSel = "";
            for (var item in sel)     
              strSel += sel[item].value;
            alert("Selected Items:\n" + strSel);
        }
        [/code]
       
        and then the form
     
        [code]
          <FORM method="post" action="test2.php" NAME="ColorSelector">
        <INPUT TYPE=CHECKBOX NAME="color" VALUE="Red">Red
        <INPUT TYPE=CHECKBOX NAME="color" VALUE="Navy" CHECKED>Navy
        <INPUT TYPE=CHECKBOX NAME="color" VALUE="Black">Black
        <INPUT TYPE=CHECKBOX NAME="color" VALUE="White" CHECKED>White
        <INPUT TYPE=BUTTON VALUE="Selected Check Box Items"
            ONCLICK="outputSelected(this.form.color);">
            <input name="submit" type="submit" value="Submit Report">
        <P>
      </FORM>
      [/code]
     
      I have also tried to set ONCLICK="outputSelected(this.form.color); on the submit button , however this ofcourse didn't work because I had no idea how the variable was passed or if it was at all :(
     
    sorry for the delay I was havign huge problems replying , which turned out to be the script tages in my script
Link to comment
Share on other sites

you mean like you have questions like this:

Favorite Ice Cream:
Vanilla [check box here]
Chocolate [check box here]
Strawberry [check box here]

etc. etc.

if so you could do something like this:

[code]
<form action="process.php" method="POST">
Favorite Icecream:<br>
<input type="checkbox" name="icecream[]" value="Vanilla">Vanilla<br>
<input type="checkbox" name="icecream[]" value="Chocolate">Chocolate<br>
<input type="checkbox" name="icecream[]" value="Strawberry">Strawberry<br>
<br>
Favorite Sprinkles:<br>
<input type="checkbox" name="sprinkles[]" value="Rainbow">Rainbow<br>
<input type="checkbox" name="sprinkles[]" value="Chocolate">Chocolate<br>
<br>
<input type="submit" value="Send Data" name="submit">
</form>
[/code]

then in your php page named process.php have something like this:

[code]
<?php

if(isset($_POST['submit'])){ //check if submit has been pressed

$icecream = $_POST['icecream']; //gets icecream value
$sprinkles = $_POST['sprinkles']; //gets sprinkles value

if($icecream == ""){
echo "You dont have a favorite icecream!";
}else{
echo "Favorite Icecream(s):<ul>";
foreach($icecream as $flavor){
echo "<li>".$flavor."</li>";
}
echo "</ul>";
}

if($sprinkles == ""){
echo "You dont have favorite sprinkles!<br>";
}else{
echo "Favorite Sprinkle(s):<ul>";
foreach($sprinkles as $type){
echo "<li>".$type."</li>";
}
echo "</ul>";
}

}else{ //no submit pressed
echo "No data was received. Please go back!";
}

?>
[/code]

is that something like what your after?
Link to comment
Share on other sites

yes that looks like it would be able to do what i need however I don't quite understand how I could get it to display as a single string instead of a looping back to find what I want  ,

how can I set 
                  [code] foreach($icecream as $flavor){
                    echo "<li>".$flavor."</li>";
                    }  [/code]   

tong just a single simple variable , so that I can just put that when it coems time to insert into the database it is nothign more complex than

[code]INSERT INTO reports (data1) VALUES ('$javascriptcheckboxvalues')[/code]

my database is ofcourse more comprehencive with mreo collums but that gives the idea of what i want to do

Link to comment
Share on other sites

well when it receives the data from the form it will be in the format like this:
$_POST['icecream'] will look something like Chocolate,Strawberry
$_POST['sprinkles'] will look something like Rainbow

so you can just insret the values straight in or you can insert each individual from the foreach statement.
Link to comment
Share on other sites

thankyou so so much  I have given it a quick shot with no luck but  will try and play with it as soon as I am home  as soon as I get home :)

just to confirm what I need to do my form looks like this

[code]
  <FORM method="post" action="test2.php" NAME="ColorSelector">
        <INPUT TYPE=CHECKBOX NAME="icecream[]" VALUE="Red">Red
        <INPUT TYPE=CHECKBOX NAME="icecream[]" VALUE="Navy" CHECKED>Navy
        <INPUT TYPE=CHECKBOX NAME="icecream[]" VALUE="Black">Black
        <INPUT TYPE=CHECKBOX NAME="icecream[]" VALUE="White" CHECKED>White
        <input name="submit" type="submit" value="Submit Report">
        <P>
      </FORM>
[/code]


and my processign page now looks liek this

[code]<?php

include "connect.php" ;

$value=$_POST['icecream'];


  $sql = "INSERT INTO test (data1) VALUES ('$value')";

  $result = mysql_query($sql);
 
if(query) {
Print "Your Information has been saved to the database " ;
}
?>[/code]

is that correct ? or do i still need the  foreach($icecream as $flavor) part aswell ?

when I tried to do it with exactly whats above it just gave the value closest to the submit button and ignored the rest of them

thanx again for your assitance :)


Link to comment
Share on other sites

alright. well...sorry but i lacked a bit b4 and i think my coding was a tad wrong. above will work, remember you can change the name icecream if you want but you gotta keep the []. alright. now.

if you want to insert it as this: Red,Navy,Black,White or whatever you have to do this:
[code=php:0]
$value = implode(",", $_POST['icecream']);
[/code]

sorry i didnt point that out before.
Link to comment
Share on other sites

Sorry to bother you all about this script again , but i was hoping to add one more functional feature to it

I wanted to make it so that only 2 of the check boxes can be selected  would my best option for this be to use javascript ? or is there a way I can do it with php  ? I have been trying to do it with a javascript but  I am not sure that I am doign it right or if it will work  because I am using the [] at the end of the name and have to keep them all the same  for scripts like the one found at http://webmaster.lycos.co.uk/tips/987521434/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.