SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
-
Apr 25, 2007, 10:37 #1
- 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> </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
' ', // 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();
}
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.
-
Apr 25, 2007, 13:03 #2
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 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', null, null, 'Yes', 'Y');
$this->radio[] = &HTML_QuickForm::createElement('radio', null, null, 'No', 'N');
$this->addGroup($this->radio, 'showYesNo', 'Show email address?');
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Apr 26, 2007, 08:29 #3
- 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','',' ');
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