SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error when looping to make group radio buttons using HTML_QuickForm

    PHP 5.1.2
    Apache 2.x
    On a RedHat box

    OK, I've been trying to nail down a solution to this problem for about 3 days now and nothing I've tried seems to work. I'm creating a survey and want to ask a question and have the user select only 1 answer. Obviously this should be done with a radio button group.

    To minimize code, I created an array with the potential answers to the questions then wanted to loop through that array to generate each call for the radio button. Below is my code, then the error I receive after submitting the form.

    PHP Code:
        $form = new HTML_QuickForm("edwards");
        
        
    //Question 2
        
    $form->addElement('html''<tr><td>&nbsp;</td></tr><tr><td colspan="2"><h4>2. What was your main reason for continuing your education? (Choose one.)</h4></td></tr>');
        
    $q2_values = array('Advance my career'
                            
    'Career change'
                            
    'Personal fulfillment');
        foreach(
    $q2_values as $key=>$val) {
    //Line 31 below
            
    $q2[$key] = &HTML_QuickForm::createElement('radio',
                            
    null,                // name for radio button, assigned by group
                            
    '&nbsp;&nbsp;&nbsp;'// text to display before button
                            
    $val.'<br />',        // text to display after button
    //Line 35 below
                            
    $val);                // the value returned
        
    }
        
    $form->addGroup($q2,'q2','',' ');
        
    $form->addElement('text''q2_text''<input type="radio" name="q2" value="Other" /> Other:', array('size'=>40));
        
    $form->addRule('q2''You must answer Question 2.''required''''client');
        

        
    //Submit button
        
    $form->addElement('submit''submitted''Submit Survey');

        
        
    //Process/Display
        
    if ($form->validate()) {
            echo 
    'Form validated!<p>';
            
    $form->process('process_survey');
        } else {
            
    //echo 'Form not yet valid!<p>';
            
    $form->display();
        } 
    The error I get is:
    Fatal error: Cannot create references to/from string offsets nor overloaded objects in /apps/home/edwards/public_html/cgi-bin/survey2007/survey2.shtml on line 35

    A colleague suggested making it without the ampersand : $q2[$key] = HTML_QuickForm::createElement
    Which instead throws the error: Catchable fatal error: Object of class HTML_QuickForm_radio could not be converted to string in /apps/home/edwards/public_html/cgi-bin/survey2007/survey2.shtml on line 31

    Can I accomplish this without hard-coding each radio button? Thanks for any help you all can provide.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,237
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)

    radios

    I don't have too much experience using QF, but this is how I added radios to my form. It works for me, I hope it helps.
    PHP Code:
    $this->radio[] = &HTML_QuickForm::createElement('radio'nullnull'Yes''Y');
    $this->radio[] = &HTML_QuickForm::createElement('radio'nullnull'No''N');
    $this->addGroup($this->radio'showYesNo''Show email address?'); 

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem with my code was in this code:
    PHP Code:
    $form->addGroup($q2,'q2','',' '); 
    QuickForm did not like the fact that I wanted to name my radio button 'q2' and use the variable name '$q2' to input the question's answer values. If I use a different variable name to input my answers it takes no problem.

    This error may have been caused by a conflict when submitting the form since our server has Magic Quotes enabled, which would make all post vars for that radio group come back in $q2 before being redefined later in the QuickForm code block.

    Whatever the case, just be careful of your variable naming conventions!


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
  •