1
sottwell
Using form class
  • 2004/7/11 5:06

  • sottwell

  • Just popping in

  • Posts: 30

  • Since: 2004/6/23


I need to have a group of identical input text fields. I can easily do this by hand in the template, but how would I get the XOOPS form generator class to do it?

<tr>
        <
td><input type="text" name="ingred[]" size="30"></td>
        <
td><input type="text" name="ingred[]" size="30"></td>
    </
tr>    
    <
tr>
        <
td><input type="text" name="ingred[]" size="30"></td>
        <
td><input type="text" name="ingred[]" size="30"></td>
    </
tr>
    <
tr>
        <
td><input type="text" name="ingred[]" size="30"></td>
        <
td><input type="text" name="ingred[]" size="30"></td>
    </
tr>


It seems like the field's name including brackets makes either XOOPS or Smarty unhappy. I can use it if I only generate one field, but it chokes if I try to create several of them.

$ingred_text = new XoopsFormText(_JYR_INGRED"ingred[]"30100"");

...

$addrecipe_form = new XoopsSimpleForm(_JYR_ADDRECIPE"addrecipeform""addrecipe.php");

// need to loop this somehow/somewhere ????
$addrecipe_form->addElement($ingred_text);


Am I looking in the wrong place? Should I create a group of them somehow in the template? I would prefer to handle them at the generation level than at the display level.

2
Mithrandir
Re: Using form class

What's your code for identical textfield 2, 3, 4 etc.?

Define "chokes" please - it can mean anything from a blank page to a slow loading page to a rebooting server

3
sottwell
Re: Using form class
  • 2004/7/11 10:08

  • sottwell

  • Just popping in

  • Posts: 30

  • Since: 2004/6/23


Sorry...it just doesn't do anything with the element if I try to use a loop to create multiple identical elements. No error messages in any of the debug modes, either.

Anyway, I would like to know the proper procedure for creating a group of identical text input fields. I'm sure it's a matter of ignorance here.

I did it by hand in the template with HTML, and everything works fine, but that's cheating!

<tr>
<
th colspan="2"><{$addrecipeform.elements.ingred.caption}></th>
</
tr>   
<
tr>
<
td><input type="text" name="ingred[]" size="30"></td>
<
td><input type="text" name="ingred[]" size="30"></td>
</
tr>
<
tr>
<
td><input type="text" name="ingred[]" size="30"></td>
<
td><input type="text" name="ingred[]" size="30"></td>
</
tr>
<
tr>
<
td><input type="text" name="ingred[]" size="30"></td>
<
td><input type="text" name="ingred[]" size="30"></td>
</
tr>
<
tr>
<
td><input type="text" name="ingred[]" size="30"></td>
<
td><input type="text" name="ingred[]" size="30"></td>
</
tr>

etc etc...


What I want to do is to create the array of elements somehow:

$ingred_text = new XoopsFormText(_JYR_INGRED"ingred[]"30100"");
.
.
.
$addrecipe_form->addElement($ingred_text);
.
.
.

with some kind of loop somewhere.

4
dheltzel
Re: Using form class
  • 2004/7/11 11:21

  • dheltzel

  • Not too shy to talk

  • Posts: 164

  • Since: 2003/1/8 1


Try something like this (uses Smarty):

<{section name=ix loop=$my_loop_array}>
<tr>
<td>
<input type="text" name="ingred[]" size="30">
</td>
</tr>
<{/section}>

This should generate a text box for each element in $my_loop_array (which you might wantto use for a label of some sort).

In the php file, use this:

if (isset($_REQUEST["ingred"])) {
foreach ($ingred as $ingred_key) {
echo $ingred_key;
}
}



5
Mithrandir
Re: Using form class

Quote:
Sorry...it just doesn't do anything with the element if I try to use a loop to create multiple identical elements. No error messages in any of the debug modes, either.

Anyway, I would like to know the proper procedure for creating a group of identical text input fields. I'm sure it's a matter of ignorance here.

Then paste the code you tried. I cannot help you correct it if you don't give more information than "it just doesn't do anything"

6
sottwell
Re: Using form class
  • 2004/7/11 11:57

  • sottwell

  • Just popping in

  • Posts: 30

  • Since: 2004/6/23


.
.
.
$addrecipe_form = new XoopsSimpleForm(_JYR_ADDRECIPE"addrecipeform""addrecipe.php");

// add elements to form

$addrecipe_form->addElement($category_select);
$addrecipe_form->addElement($recipename_text);

// loop here

for($i=0;$i<16;$i++) {
$addrecipe_form->addElement($ingred_text);
}

$addrecipe_form->addElement($directions_textarea);
.
.
.


and the template HTML:

<table class="outer" cellspacing="1">
        <{foreach 
item=element from=$addrecipeform.elements}>
            <
tr>
                 <
td class="head"><{$element.caption}></td>
                 <
td><{$element.body}></td>
             </
tr>
        <{/foreach}>
</
table>


Still only adds one $ingred_text element to the form.

It looks like you can only add an element once. That's fine with me, so how would I create a group of elements and add the group?

I don't see how the Smarty section would work, since I don't have an array of text elements to work with!

7
Mithrandir
Re: Using form class

Try to replace the line inside the loop with this:
$addrecipe_form->addElement(new XoopsFormText(_JYR_INGRED"ingred[]"30100""));



8
sottwell
Re: Using form class
  • 2004/7/11 12:47

  • sottwell

  • Just popping in

  • Posts: 30

  • Since: 2004/6/23


Nope...still just one text input element.

The Smarty loop works fine like so:

<{section name="ingredients" start=0 loop=9}>
   <
tr>
        <
td><input type="text" name="ingred[]" size="30"></td>
        <
td><input type="text" name="ingred[]" size="30"></td>
    </
tr>
<{/
section}>


But gives me a nasty error like so:
<{section name="ingredients" start=0 loop=9}>
   <
tr>
        <
td><{$addrecipeform.elements.ingred[].body}></td>
        <
td><{$addrecipeform.elements.ingred[].body}></td>
    </
tr>
<{/
section}>


Fatal error: Smarty: [in db:addrecipe_form.html line 20]: syntax error: unrecognized tag: $addrecipeform.elements.ingred[].body (Smarty_Compiler.class.php, line 382) in /Library/WebServer/Documents/xoops/class/smarty/Smarty_Compiler.class.php on line 1824

Displays fine if I get rid of the [] in the element name, except then the database insert fails (of course!). I really don't want to get involved in complex array loading from POST variables.

Tried escaping the brackets, and it skipped the loop altogether. Oh, well...I'll just go with the first loop, while this is a lot of fun learning, I would like to move ahead with this module.

Thanks!



Login

Who's Online

181 user(s) are online (98 user(s) are browsing Support Forums)


Members: 0


Guests: 181


more...

Donat-O-Meter

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

Latest GitHub Commits