[SOLVED] Sending multiple checkbox values

I’m trying to send a collection of data to the database I’m using check boxes below is my code.

 <label>Description</label><br />
            <textarea name="desc" id="desc" rows="8" cols="50"></textarea> <br />
        </div>
        <div class="margin">
          <fieldset>
              <legend style="font-weight: normal; font-size: medium; text-align: left">Standard Services</legend>
              <input type="checkbox" name="stdService[]" value="Sensual Massage" />
              <label>Sensual Massage</label><br />
              <input type="checkbox" name="stdService[]" value="French" />
              <label>French</label><br />
              <input type="checkbox" name="stdService[]" value="Foreplay" />
              <lable>Foreplay</lable><br />
              <input type="checkbox" name="stdService[]" value="GFE" />
              <lable>GFE</lable><br />
              <input type="checkbox" name="stdService[]" value="Toys" />
              <lable>Toys</lable><br />
              <input type="checkbox" name="stdService[]" value="Spanish" />
              <lable>Spanish</lable><br />
              <input type="checkbox" name="stdService[]" value="Swedish" />
              <lable>Swedish</lable><br />
              <input type="checkbox" name="stdService[]" value="Sex" />
              <lable>Sex</lable><br />
              <input type="checkbox" name="stdService[]" value="Ladies" />
              <lable>Ladies</lable><br />
              <input type="checkbox" name="stdService[]" value="Couples" />
              <lable>Couples</lable><br />
              <input type="checkbox" name="stdService[]" value="Bi" />
              <lable>Bi</lable><br />
              <input type="checkbox" name="stdService[]" value="Golden Showers" />
              <lable>Golden Showers</lable><br />
              <input type="checkbox" name="stdService[]" value="Private Strips" />
              <lable>Private Strips</lable><br />
              <input type="checkbox" name="stdService[]" value="Lap Dances" />
              <lable>Lap Dances</lable><br />
              <input type="checkbox" name="stdService[]" value="Topless Witressing" />
              <lable>Topless Waitressing</lable><br />
              <input type="checkbox" name="stdService[]" value="Fantasies" />
              <lable>Fantasies</lable><br />
              <input type="checkbox" name="stdService[]" value="Fetish" />
              <lable>Fetish</lable><br />
              <input type="checkbox" name="stdService[]" value="Dress Ups" />
              <lable>Dress Ups</lable><br />
              <input type="checkbox" name="stdService[]" value="Goes Twice" />
              <lable>Goes Twice</lable><br />
              <input type="checkbox" name="stdService[]" value="Dating" />
              <lable>Dating</lable><br />
              <label>Other:</label>
              <input type="text" name="stdService[]" />
          </fieldset>
        </div>

Process.php

$stdService = $_REQUEST["stdService"];

//Loop for the stdService
$std = "";
$flag = 0;
foreach($stdService as $stdServ){
    $std = $stdServ."' ";
    $flag = 1;
}

if($flag == 1){
    $std = rtrim($std);
}

//Loop for the othService
$oth = "";
$flagOth = 0;
foreach($othService as $othServ){
    $oth = $othServ."' ";
    $flagOth = 1;
}

if($flagOth == 1){
    $oth = rtrim($oth);
}

text input get entered but check box value doesn’t get submitted or my function is not working can some one help me please.

I do believe it’s this line which is killing you. A better option would probably be to give it a new name and process is separately.

<input type=“text” name=“stdService” />

Try using $_POST…

$service = $_POST['stdService'];

or, for example…

foreach ($_POST['stdService'] as $service) {
     echo "You selected: $service <br>";
 }

But as DaveMaxwell said, it’s easier to just name them all independently of each other

Actually, since the loop just replaces instead of appending, if “other” is empty, the value is empty.

you’re right, I just didn’t put any of the other code there.

Thank you guys for the reply’s I will try that and let you guys know. I have gone through lot of examples on the web but all of them use this format to submit data so as u guys say if I remove the last text box it’s gonna be ok ?

Not really - it’ll solve the problem where you’re getting blanks, but that’s it.

This line is going to give you wrong results each time.

$std = $stdServ."' ";

What that will do is overlay the value of the $std with the value of the LAST checked box, which isn’t what I think you want. I’m guessing you want to include ALL of the checked values, in which case, this would be more appropriate.

$std .= (strlen($std) > 0, ", ", "") . "'" . $stdServ."' ";

What this does is APPEND the value of each checkbox with the value in $std. For readability of the final result, I added the strlen check to allow for a comma separation between each checked value (and added the initial quote value to balance out the terminating one).

Then you would just need the additional coding to handle the “other” field.

or since $stdService is an array, just turn this whole code block:

$std = "";
$flag = 0;
foreach($stdService as $stdServ){
    $std = $stdServ."' ";
    $flag = 1;
}

if($flag == 1){
    $std = rtrim($std);
}

into this one line.

 $std =  implode(", ",$stdService);

(don’t reinvent the wheel; there’s already a function in PHP to do what you want :wink: )

3 Likes

bah! Forgot all about implode (don’t do PHP much anymore…)

1 Like

DaveMaxwell and StarLion thank you very much I’m not pro in PHP I’m a newbie still learning stuff don’t know all the functions yet but I’m learning.

as Dave said I want to get all the selected values and put them in to the db as comma separated text line (EX: jhon, beer, food, sleep, sex, … etc) and then retrieve that and show it in the web site. and if you guys wanna have a look this is the site I’m doing http://onlinetestarea.cu.cc/ it’s still in a test domain.

I’ll try out the suggestion by StarLion. And thank you again for the help also I know there’s lot security issues with my code is there any books about mysqli.

StarLion you solved my issue bro Thank I lived in the sme city or country would have bought you a 12 pack m8 thank you very very much. Oh! and it also works with my text at the end pf the options list it doesn’t matter if it’s empty or not.I’m goona put this up in my blog so other can get some help as well.

Thank you very very much and this place rocks far better than stacks.