Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple Checkbox Values into String

Status
Not open for further replies.

christophorus

Technical User
Apr 3, 2006
9
US
I am trying to collect the values from multiple checkboxes into a string. If the user selects lets say 3 of the choices, how do i format them into a string like "AK,CA,CO,". Do I need to create an array first?


<form name="states" method="POST" action="<? $_SERVER['PHP_SELF']; ?>">

<input type="checkbox" name="state1" value="Nationwide">
Nationwide</p>
<input type="checkbox" name="state2" value="AK,">
AK
<input type="checkbox" name="state3" value="AL,">
AL
<input type="checkbox" name="state4" value="AR,">
AR
<input type="checkbox" name="state5" value="AZ,">
AZ
<input type="checkbox" name="state6" value="CA,">
CA
<input type="checkbox" name="state7" value="CO,">
CO
<input type="checkbox" name="state8" value="CT,">
CT
<input type="submit" name="Submit" value="Get Data">
</form>
 
call each checkbox "checkbox[]"

each selected checkbox will, on submission, be in the array $_POST['checkbox']

to get into a string use implode()
 
Seeing as you will get an array, you might consider keeping the data that way.

Code:
$_POST['messages']['noneselected'] = "<p><strong>None selected</strong></p>";

if (count($_POST['checkbox']) > 0)) {
echo "<ul>";
  for ($i = 0; $i < count($_POST['checkbox']); $i++) {
    echo "<li>{$_POST['checkbox'][$i]";
  }
echo "</ul>";
}
else {
echo $_POST['messages']['noneselected'];
}

ps. take this as psuedocode

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top