Page 1 of 1

PHP Challenged :) - form name array

Posted: Sun Jan 22, 2006 11:04 pm
by rjwebgraphix
Ok folks. This one is a CHALLENGE!!! Well for me anyway. I've been modifying an existing form mailing script to suit the needs of my site. I haven't had to mess with the actual processing of the content part on the form processor until I decided to go with an array for the name of a group of check boxes.

Form is being processed as POST. My HTML mixed with PHP for the checkboxes looks something like this. The PHP is for auto-checking already checked fields on error. THIS WORKS!

Code: Select all

<INPUT TYPE="checkbox" NAME="os_version[winxphome]" VALUE="on"
  <? if ($formfield['os_version']['winxphome'] == "on")
    {
        echo " CHECKED ";
    }
  ?>
>

This is the function that sets up the content to be mailed.

Code: Select all

// This function takes the sorts, excludes certain keys and 
// makes a pretty content string.
function parse_form($array, $sort = "") {
   // build reserved keyword array
   $reserved_keys[] = "MAX_FILE_SIZE";
   $reserved_keys[] = "required";
   $reserved_keys[] = "redirect";
   $reserved_keys[] = "require";
   $reserved_keys[] = "path_to_file";
   $reserved_keys[] = "recipient";
   $reserved_keys[] = "subject";
   $reserved_keys[] = "sort";
   $reserved_keys[] = "style_sheet";
   $reserved_keys[] = "bgcolor";
   $reserved_keys[] = "text_color";
   $reserved_keys[] = "link_color";
   $reserved_keys[] = "vlink_color";
   $reserved_keys[] = "alink_color";
   $reserved_keys[] = "title";
   $reserved_keys[] = "missing_field_redirect";
   $reserved_keys[] = "env_report";
   $reserved_keys[] = "submit";
   if (count($array)) {
      if (is_array($sort)) {
         foreach ($sort as $field) {
            $reserved_violation = 0;
            for ($ri=0; $ri<count($reserved_keys); $ri++)
               if ($array[$field] == $reserved_keys[$ri]) $reserved_violation = 1;

            if ($reserved_violation != 1) {
               if (is_array($array[$field])) {
                  for ($z=0;$z<count($array[$field]);$z++)
                     $content .= $field.SEPARATOR.$array[$field][$z].NEWLINE;
               } else
                  $content .= $field.SEPARATOR.$array[$field].NEWLINE;
            }
         }
      }
      while (list($key, $val) = each($array)) {
         $reserved_violation = 0;
         for ($ri=0; $ri<count($reserved_keys); $ri++)
            if ($key == $reserved_keys[$ri]) $reserved_violation = 1;

         for ($ri=0; $ri<count($sort); $ri++)
            if ($key == $sort[$ri]) $reserved_violation = 1;

         // prepare content
         if ($reserved_violation != 1) {
            if (is_array($val)) {
               for ($z=0;$z<count($val);$z++)
                  $content .= $key.SEPARATOR.$val[$z].NEWLINE;
            } else
               $content .= $key.SEPARATOR.$val.NEWLINE;
         }
      }
   }
   return $content;
}
The problem: The form processor (above) isn't setup to handle a NAME as an array as shown in the HTML/PHP mix above, thus when I recieve the message, it simply shows os_version, but doesn't contain data, where it should somehow show that $os_version[dos] == "on"

I'm at a complete loss with how to do this, unless I skip the whole array concept and go with strait names each one being different, but would have to change other code that I'm using that array to check if an item was checked or not. Wouldn't be too much a deal if I had to do that except would be alot more coding to not use an array here as there are actually 17 different OS's as selectable options and will grow with each release of more OS's that will be supported. (make since?)

I just need it to be able to handle an input name as an array within the above parse_form function if possible.

Thanks for any assistance with this as I'm now at a complete loss.

- RJ

IGNORE

Posted: Mon Jan 23, 2006 4:42 am
by rjwebgraphix
PLEASE IGNORE: Problem has been resolved.

Posted: Mon Jan 23, 2006 6:00 am
by Jenk
FYI: The following is easier to maintain:
The form:

Code: Select all

<form action="page.php" method="post">
    <input type="checkbox" name="os_version[]" value="Win95" />
    <input type="checkbox" name="os_version[]" value="Win98" />
    <input type="checkbox" name="os_version[]" value="Win3.11" />
    <input type="checkbox" name="os_version[]" value="Win2k" />
    <input type="checkbox" name="os_version[]" value="WinXPHome" />
    <input type="checkbox" name="os_version[]" value="WinXPPro" />
    <input type="checkbox" name="os_version[]" value="FreeBSD" />
    <input type="checkbox" name="os_version[]" value="Solaris9" />
<!-- etc.. -->
</form>
page.php:

Code: Select all

<?php

foreach ($_POST['os_version'] as $os_version) {
    switch ($os_version) {
        case 'Win95':
            //do something for win95
            break;
        case 'Win98':
            //do something for win98
            break;
        case 'Win2k':
            //do something for win2k
            break;
 // etc..

       default:
            //do default action
    }
}
?>
:)

Posted: Mon Jan 23, 2006 9:23 am
by josh
I've always done it with selects

Code: Select all

<select name="osversion[]" size="3" multiple>
<!-- any options -->
</select>

Posted: Mon Jan 23, 2006 12:37 pm
by rjwebgraphix
jshpro2 wrote:I've always done it with selects

Code: Select all

<select name="osversion[]" size="3" multiple>
<!-- any options -->
</select>
Selects is good for "Select State", but is not good for "Choose which OS's you use across your 10 PC's or servers, which could be multiple, which is the need for checkboxes.