Page 1 of 1

Form processor

Posted: Mon Jan 12, 2009 2:47 am
by Lolla
I am using a form processor to process a contact form.

On the contact form I have the following code:

<input type="hidden" name="variable_order" value="name|town|gender|cell|email">

and then

<table>
<tbody>
<tr><th align="left">Name:</th><td><input type="text" maxlength="20" size="20" name="name" ></td></tr>
<tr><th align="left">Cell:</th><td><input type="text" name="cell" size="12" maxlength="12"></td></tr>
<tr><th align="left">Email:</th><td><input type="text" name="email" size="20"></td></tr>
<tr><td></td></tr>
<tr><td colspan="2">M<input type="radio" name="gender" value="M"></td></tr>
<tr><td colspan="2">F<input type="radio" name="gender" value="F"></td></tr>
<tr><td>Hair Colour:</td><td class="contact-field"><select id="buysell" name="hair">
<option value="brown">brown</option>
<option value="blonde">blonde</option>
<option value="red">red</option>
</select></td></tr>
<tr><td>Lived:</td><td>George <input type="checkbox" name="town" value="George"></td></tr>
<tr><td></td><td>CT<input type="checkbox" name="town" value="CT"></td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Submit" name="submit"></td></tr>
</tbody>
</table>

the problem i have is that the values of the textboxes and radiobuttons etc get saved into the array in the variable "variable order". but of course the checkboxes can have more than one value.

how can i send all the values of the checked checkboxes through to the form processer using "variable order"?

any help is appreciated.

Re: Form processor

Posted: Tue Jan 13, 2009 1:46 am
by paqman
A nice feature of html forms is you can set a field to be an array simply by adding '[]' to the end of the name. For your checkboxes you'll need to change the name from 'town' to 'town[]'. That way each part is sent through as part of an array. Try this:

Code: Select all

 
<?
//check that your form has been submitted
if(isset($_POST["variable_order"]))
{
     //break variable_order into an array
     foreach(explode("|", $_POST["variable_order"]) as $order)
     {
          //it's an array, maybe the checkboxes
          if(is_array($_POST[$order]))
          {
               echo $order.": <br>";
               foreach($_POST[$order] as $arraypart)
               {
                    //do something with the array
                    echo "&nbsp;".$arraypart."<br>";
               }
          }
 
          //it's just a regular value
          else
          {
               //do something with the data, eg:
               echo $order.": ".$_POST[$order]."<br>";
          }
     }
}
 
 
//show form
else
{
     ?>
     <form method="post" action="form.php">
     <input type="hidden" name="variable_order" value="name|town|gender|cell|email">
     <table>
     <tbody>
     <tr><th align="left">Name:</th><td><input type="text" maxlength="20" size="20" name="name" ></td></tr>
     <tr><th align="left">Cell:</th><td><input type="text" name="cell" size="12" maxlength="12"></td></tr>
     <tr><th align="left">Email:</th><td><input type="text" name="email" size="20"></td></tr>
     <tr><td></td></tr>
     <tr><td colspan="2">M<input type="radio" name="gender" value="M"></td></tr>
     <tr><td colspan="2">F<input type="radio" name="gender" value="F"></td></tr>
     <tr><td>Hair Colour:</td><td class="contact-field"><select id="buysell" name="hair">
     <option value="brown">brown</option>
     <option value="blonde">blonde</option>
     <option value="red">red</option>
     </select></td></tr>
     <tr><td>Lived:</td><td>George <input type="checkbox" name="town" value="George"></td></tr>
     <tr><td></td><td>CT<input type="checkbox" name="town" value="CT"></td></tr>
     <tr><td colspan="2" align="center"><input type="submit" value="Submit" name="submit"></td></tr>
     </tbody>
     </table>
     </form>
     <?
}
 
?>