1
earplane
XoopsThemeForm problem
  • 2006/9/21 9:20

  • earplane

  • Just popping in

  • Posts: 35

  • Since: 2002/10/1


I need to create a form dynamically, with checkboxes created in a for loop. Everything works fine on my localhost. However, when I transfer the code to the server I get problems: The form will show the correct number of checkboxes, but they all contain data from the last entry in the list. Code:

$form = new XoopsThemeForm('My form ''myName'$myPathPOST);          
for (
$i=0;$i<count($members);$i++)          
{              
    
$member $members[$i];
    
$name $member->Name();
    echo 
$name // This shows the correct name in both cases
    
$isPresent isPresent($member->ID());    
    
$checkbox = new XoopsFormCheckBox($name$member->ID(), $isPresent);
    
$checkbox->addOption(false" "); 
    echo 
$checkbox// this is OK on the localhost, and empty on the server
    
$form->addElement($sjekkboks);
}
$form->addElement(new XoopsFormButton(""'someName''Save','submit' )); 
$form->Display();


This will show the form with count($members) instances of the checkbox. On my localhost everything is fine, but on the server they're all filled with data from the last member.

Any idea, anyone?

(Note: we're not talking XOOPS members here, the member class is my own).

2
Dave_L
Re: XoopsThemeForm problem
  • 2006/9/21 10:46

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


1. Quote:
echo $checkbox;


This is a better way to look at that variable:

var_dump('checkbox'$checkbox);


2. Quote:
$form->addElement($sjekkboks);


Shouldn't this be:

$form->addElement($checkbox);


3. Try adding an unset at the end of the loop:

...
   
$form->addElement($checkbox);
   unset(
$checkbox);
}

3
earplane
Re: XoopsThemeForm problem
  • 2006/9/21 15:12

  • earplane

  • Just popping in

  • Posts: 35

  • Since: 2002/10/1


Hi Dave_L,

Thanks! The unset() statement did the job. Perfect! But should it really be necessary? I find it strange.

(Sorry about the $sjekkboks, I translated my variable names in a hurry and I forgot one...)

Again, thanks, and cheers,
Earplane

4
Dave_L
Re: XoopsThemeForm problem
  • 2006/9/21 15:49

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


I think the reason is that the first parameter to addElement is a reference:

function addElement([color=ff0000]&[/color]$formElement$required=false){


If you don't unset $checkbox, then the form elements all wind up as references to the same variable, rather than references to distinct variables. Or something like that.

I've encountered the same problem as you when creating form elements in a loop.

Login

Who's Online

432 user(s) are online (331 user(s) are browsing Support Forums)


Members: 0


Guests: 432


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits