Previous Page

Process and Output items checked in a form

Check out an example of this here



Below is the php that will process which checkboxes are checked by the user. Place this code above your form.

<?php

		$aState = $_POST['states']; // variable to store the states checked    
    
	if(isset($_POST['states']))
	{		
			/* the following foreach keeps the checkboxes that the user checks,
               in a checked state after clicking submit                        */
		    foreach ($_POST['states'] as $selectedState)
            $selected[$selectedState] = "checked";
	
		    $N = count($aState); // number of states checked
		
	}
	
	if($_POST["clear_all"]) /* This is the  type="submit"  button with the 
                               name= "clear_all"   near the bottom of the form  */
	{
		if(isset($_POST['states']))
		{
				/* the following foreach clears the checkboxes that the user checks  */
				foreach ($_POST['states'] as $selectedState)
				$selected[$selectedState] = "";
		}
	} 

?>

Below is a the sample form, with many checkboxes, that the php processes. Take notice to the php embedded in the html. This is needed to keep each checkbox selected that the user clicks on.


<form id="form1" name="form1" method="post" action="">
  <table align="center" cellpadding="2" cellspacing="2">

    <tr>
      <td width="8" align="right"><input name="states[]" type="checkbox" id="states1" value="Alabama" <?php echo $selected['Alabama']; ?> />      </td>
      <td width="120" height="17">Alabama</td>
      <td width="8" align="right"><input name="states[]" type="checkbox" id="states2" value="Hawaii" <?php echo $selected['Hawaii']; ?> /></td>
      <td width="120" height="17">Hawaii</td>
      <td width="8" align="right"><input name="states[]" type="checkbox" id="states3" value="Massachusetts" <?php echo $selected['Massachusetts']; ?> /></td>
      <td width="120" height="17">Massachusetts</td>
      <td width="8" align="right"><input name="states[]" type="checkbox" id="states4" value="New Mexico"<?php echo $selected['New Mexico']; ?>  /></td>
      <td width="120" height="17">New Mexico</td>
      <td width="8" align="right"><input name="states[]" type="checkbox" id="states5" value="South Dakota" <?php echo $selected['South Dakota']; ?> /></td>
      <td width="120" height="17">South Dakota</td>
  </tr>
    <tr>

      <td width="48" align="right"><input name="states[]" type="checkbox" id="states6" value="Alaska"<?php echo $selected['Hawaii']; ?>  /></td>
      <td width="120" height="17">Alaska</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states7" value="Idaho" <?php echo $selected['Hawaii']; ?> /></td>
      <td width="120" height="17">Idaho</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states8" value="Michigan" <?php echo $selected['Michigan']; ?> /></td>
      <td width="120" height="17">Michigan</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states9" value="New York" <?php echo $selected['New York']; ?> /></td>
      <td width="120" height="17">New York</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states10" value="Tennessee" <?php echo $selected['Tennessee']; ?> /></td>
      <td width="120" height="17">Tennessee</td>
  </tr>
    <tr>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states11" value="Arizona" <?php echo $selected['Arizona']; ?> /></td>
      <td width="120" height="17">Arizona</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states12" value="Illinois" <?php echo $selected['Illinois']; ?> /></td>
      <td width="120" height="17">Illinois</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states13" value="Minnesota" <?php echo $selected['Minnesota']; ?> /></td>
      <td width="120" height="17">Minnesota</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states14" value="North Carolina" <?php echo $selected['North Carolina']; ?> /></td>
      <td width="120" height="17">North Carolina</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states15" value="Texas" <?php echo $selected['Texas']; ?> /></td>
      <td width="120" height="17">Texas</td>
 </tr>
    <tr>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states16" value="Arkansas" <?php echo $selected['Arkansas']; ?> /></td>
      <td width="120" height="17">Arkansas</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states17" value="Indiana" <?php echo $selected['Indiana']; ?> /></td>
      <td width="120" height="22">Indiana</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states18" value="Mississippi" <?php echo $selected['Mississippi']; ?> /></td>
      <td width="120" height="17">Mississippi</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states19" value="North Dakota" <?php echo $selected['North Dakota']; ?> /></td>
      <td width="120" height="17">North Dakota</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states20" value="Utah" <?php echo $selected['Utah']; ?> /></td>
      <td width="120" height="17">Utah</td>
</tr>

 <tr>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states21" value="California" <?php echo $selected['California']; ?> /></td>
      <td width="120" height="17">California</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states22" value="Iowa" <?php echo $selected['Iowa']; ?> /></td>
      <td width="120" height="17">Iowa</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states23" value="Missouri" <?php echo $selected['Missouri']; ?> /></td>
      <td width="120" height="17">Missouri</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states24" value="Ohio"<?php echo $selected['Ohio']; ?>  /></td>
      <td width="120" height="17">Ohio</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states25" value="Vermont" <?php echo $selected['Vermont']; ?> /></td>
      <td width="120" height="17">Vermont</td>
 </tr>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states26" value="Colorado" <?php echo $selected['Colorado']; ?> /></td>
      <td width="120" height="17">Colorado</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states27" value="Kansas" <?php echo $selected['Kansas']; ?> /></td>
      <td width="120" height="17">Kansas</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states28" value="Montana" <?php echo $selected['Montana']; ?> /></td>
      <td width="120" height="17">Montana</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states29" value="Oklahoma" <?php echo $selected['Oklahoma']; ?> /></td>
      <td width="120" height="17">Oklahoma</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states30" value="Virginia"<?php echo $selected['Virginia']; ?>  /></td>
      <td width="120" height="17">Virginia</td>
</tr>
 <tr>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states31" value="Connecticut" <?php echo $selected['Connecticut']; ?> /></td>
      <td width="120" height="17">Connecticut</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states32" value="Kentucky" <?php echo $selected['Kentucky']; ?> /></td>
      <td width="120" height="17">Kentucky</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states33" value="Nebraska" <?php echo $selected['Nebraska']; ?> /></td>
      <td width="120" height="17">Nebraska</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states34" value="Oregon" <?php echo $selected['Oregon']; ?> /></td>
      <td width="120" height="17">Oregon</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states35" value="Washington" <?php echo $selected['Washington']; ?> /></td>
      <td width="120" height="17">Washington</td>
</tr>

  <tr>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states36" value="Delaware" <?php echo $selected['Delaware']; ?> /></td>
      <td width="120" height="17">Delaware</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states37" value="Louisiana" <?php echo $selected['Louisiana']; ?> /></td>
      <td width="120" height="17">Louisiana</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states38" value="Nevada" <?php echo $selected['Nevada']; ?> /></td>
      <td width="120" height="17">Nevada</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states39" value="Pennsylvania" <?php echo $selected['Pennsylvania']; ?> /></td>
      <td width="120" height="17">Pennsylvania</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states40" value="West Virginia" <?php echo $selected['West Virginia']; ?> /></td>
      <td width="120" height="17">West Virginia</td>
</tr>
   <tr>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states41" value="Florida" <?php echo $selected['Florida']; ?> /></td>
      <td width="120" height="17">Florida</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states42" value="Maine" <?php echo $selected['Maine']; ?> /></td>
      <td width="120" height="17">Maine</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states43" value="New Hampshire" <?php echo $selected['New Hampshire']; ?> /></td>
      <td width="120" height="17">New Hampshire</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states44" value="Rhode Island" <?php echo $selected['Rhode Island']; ?> /></td>
      <td width="120" height="17">Rhode Island</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states45" value="Wisconsin" <?php echo $selected['Wisconsin']; ?> /></td>
      <td width="120" height="17">Wisconsin</td>
  </tr>
  <tr>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states46" value="Georgia" <?php echo $selected['Georgia']; ?> /></td>
      <td width="120" height="17">Georgia</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states47" value="Maryland" <?php echo $selected['Maryland']; ?> /></td>
      <td width="120" height="17">Maryland</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states48" value="New Jersey" <?php echo $selected['New Jersey']; ?> /></td>
      <td width="120" height="17">New Jersey</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states49" value="South Carolina" <?php echo $selected['South Carolina']; ?> /></td>
      <td width="120" height="17">South Carolina</td>
      <td width="48" align="right"><input name="states[]" type="checkbox" id="states50" value="Wyoming" <?php echo $selected['Wyoming']; ?> /></td>
      <td width="120" height="17">Wyoming</td>
 </tr>
</table>
<br />

<table style="margin-left:200px;">
<center>
<tr>
		
       <td>
      	<input type="button" value="SELECT ALL" onclick="checkAll();" style="width:150px; height:30px; font-size:1em;"  />
       </td>

	   <td>
     	 <input  type="submit" name="submit" value="Submit" style="width:175px; height:40px; font-size:1em;"  onclick="return oneChecked()"/>
       </td>
       
       
        <td>
     	 <input  type="submit" name="order"  value="Sort (a-z) and Submit" style="width:175px; height:40px; font-size:1em;" 
          onclick="return oneChecked()"/>
       </td>

       <td>
         <input type="submit" name="clear_all"  value="CLEAR ALL" style="width:150px; height:30px; font-size:1em;"  />
       </td>


</tr>
</center>
</table>

</form>


Below is the php that will output the user's selections from the form. Place this code on your page where you want the output to be. I have mine just below the form for the example.

<?php

 if($_POST["submit"]) /* This executes if the Submit button is clicked since the name of that
 						 button is "submit"                                                  */                                       
 {   
    if(isset($_POST['states']))
	{
 		  echo 'You selected '.$N.' State(s): 
'; $num = 0; // variable for our numbered list, incrementing by 1 after each state outputs in the below FOR statement for($i=0; $i < $N; $i++) { $num++; // starts out adding 1, then outputs the name of the first state. echo($num.'. '.$aState[$i].'
'); /* outputs a numbered list and the actual name of the state from the form that is checked. */ } } } //========================================== For alphabetical order ===================================== else if($_POST["order"]) /* This executes if the Alphabetical Order button is clicked since the name of that button is "order" */ { if(isset($_POST['states'])) { echo 'You selected '.$N.' State(s):
'; $num = 0; // variable to store the number of states selected $array = array(); // new array to hold all states checked for($i=0; $i < $N; $i++) { $array[] = $aState[$i];// add states to the array one at a time } sort($array); // sorts the array in alphabetical order for($i=0; $i < $N; $i++) { $num++; // starts out adding 1, then outputs the name of the first state echo($num.'. '.$array[$i].'
'); } } } ?>