SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Form Checkbox

  1. #1
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form Checkbox

    I have a checkbox element in a form i have created which can have multiple values....making it an array.

    I am confused as to #1 how to echo its values, and #2 how to save the arrays value into a database.

    It would be nice to convert the array into a string and save it that way.

    WHat might make this a little more frustrating for some of you, I am useing the Pear::quickform class to generate the form. I do like useing the quickform class, however, it doesn't offer very good documentation for those of us that don't read directions very well, and don't have a solid knowledge of PHP/MySql.

    here is the checkbox code i have to this point:
    PHP Code:
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''QB'null'QB');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''RB'null'RB');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''FB'null'FB');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''TE'null'TE');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''OG'null'OG');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''OT'null'OT');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''C'null'C');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''TE'null'TE');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''WR'null'WR');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''S'null'S');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''CB'null'CB');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''DE'null'DE');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''DT'null'DT');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''MLB'null'MLB');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''OLB'null'OLB');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''P'null'P');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''K'null'K');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''Return Man'null'Return Man');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''KOR Team'null'KOR Team');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''KO Team'null'KO Team');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''Punt Team'null'Punt Team');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''Punt Ret Team'null'Punt Ret Team');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''Field Goal/Block unit'null'Field Goal/Block unit');
            
    $form->addGroup($checkbox'ichkPos''Positions Played:', array('&nbsp;','&nbsp;','&nbsp;''<br />'));
            
    $form->addGroupRule('ichkPos''Please check at least one box'null1); 
    I have tried a coule of things.

    this:
    PHP Code:
    $pos1 $form->getSubmitValue('ichkPos');
                    
                    echo (
    $pos1); 
    returns "array"

    this:
    PHP Code:
    $pos1 $form->getSubmitValue('ichkPos');
                    
                    
    $text=implode($pos1);
                    echo (
    $text); 
    returns 1's depending on how many boxes I have checked. for instance, If i select 3 of the boxes, the output is "111". 6 selected kgets me "111111"

    what do you think?

  2. #2
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    w/o being familiar with pear::quickform i'll hazard a guess that your doing it wrong.

    First I think, from seeing your results of trying to output the array I'd say that your creating checkboxes that are all named differently, meaning that what you really want is this:
    PHP Code:
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''ichkPos'null'TE'); 
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''ichkPos'null'OG'); 
    Not what you currently have.
    To see the contents then try this temporarily:
    PHP Code:
    $pos1 $form->getSubmitValue('ichkPos'); 
    print_r($pos1); 
    normally to just print the contents of an array you would do:
    PHP Code:
    foreach ($pos1 as $value)
    {
    print 
    $value;

    HTH for now

    Additionally I dont think your using the addGroup() right. Probably just take it out for now.
    Last edited by Mandibal; Mar 16, 2004 at 14:57. Reason: added comments
    Erh

  3. #3
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I Used what you told me:

    PHP Code:
    <?php
    // Include QuickForm class
    require_once ("HTML/QuickForm.php");
            
            
    // Instantiate the QuickForm class
            
    $form = new HTML_QuickForm('Selecttest','POST','checkTest.php');

            
    // Add a header to the form
            
    $form->addHeader('Header Title Here.');
            
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''ichkPos'null'QB');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''ichkPos'null'RB');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''ichkPos'null'LB');
            
    $checkbox[] = &HTML_QuickForm::createElement('checkbox''ichkPos'null'FB');
            
    $form->addGroup($checkbox'ichkPos''Positions Played:');
            
            
    // Add a submit button
            
    $form->addElement('submit','submit','Accept Changes','class="createAccount"');
              if ( 
    $form->validate() ) {
                    
    $pos1 $form->getSubmitValue('ichkPos'); 
                    
    print_r($pos1);                            
            } else {
                
    // If not submitted, display the form
                
    $form->display();
            }
     
     
    ?>
    and this is what it returned:

    Code:
    Array ( [ichkPos] => 1 )
    i have no idea what that means. Can you help?

  4. #4
    SitePoint Wizard Busch's Avatar
    Join Date
    Jan 2004
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Raskolnikov,

    I am having the same problem as you did. Did you find a solution and if so could you let me know what you did?

    Thanks
    Busch

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Land of the Dead
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Solution?

    I always set the values up in their own array --

    Code:
    $cvAge[] = 'Pre-K';
    $cvAge[] = 'Elementary';
    $cvAge[] = 'Middle School';
    $cvAge[] = 'High School';
    And then use a function to build the checkbox elements --

    Code:
    function MakeCheckGroup(&$form, $name, $label, $values)
    {
    	foreach ($values as $i => $value)
    	{
    	        $group[] = &$form->createElement('checkbox', $i, null, $value);
    	}
            $form->addGroup($group, $name, $label, '<br>');
    }
    Then I call it when I build the form like so --

    Code:
    MakeCheckGroup($form, 'chkTargetAge', 'Target Age', $cvAge);
    If the user checks anything off, an array will come through $_POST or $_GET that looks like --

    chkTarget[2] = 1
    chkTarget[3] = 1
    etc...

    If you wanted to build a string, you could do something like this --

    Code:
    foreach ($_POST['chkTargetAge'] as $i => $check)
    {
    	$vals[] = $cvAge[$i];
    }
    
    join(',', $vals);
    Let me know if that didn't make sense.
    Learn how to astral project -- http://mysticweb.org


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •