sticky repeating forms

7 views
Skip to first unread message

ulysses

unread,
Mar 20, 2008, 5:28:16 PM3/20/08
to Web Forms 2.0 Implementation
Maybe, someone already knows the answer. Does anyone know how to
redisplay input values in a repeating form after a submit?
Essentially, how can I make the input values stick? I'm using PHP to
redisplay input forms.

Sample:

<form id="claim" method="post" enctype="multipart/form-data"
action="<? $_SERVER['PHP_SELF']; ?>">
<table>
<tr repeat="template" id="row" repeat-start="5" repeat-min="1" repeat-
max="7">
<td><input type="text" name="amount_[row]" value="<?php echo
$_POST['amount_[row]']; ?>"></td>
<td><button type="remove">Delete</button></td>
</tr>
<button type="add" template="row">Add Row</button>
</table>
</form>

Thanks.

Weston Ruter

unread,
Mar 21, 2008, 2:48:49 AM3/21/08
to webf...@googlegroups.com
What you can do is something like this:


<form id="claim" method="post" enctype="multipart/form-data"
action="<? echo $_SERVER['PHP_SELF']; ?>">
<table>
<?php
$repetitionIndexes = array();
foreach($_POST as $key => $value){
    if(preg_match("{^amount_(\d+)}", $key, $matches))
         $repetitionIndexes[] = (int)$matches[1];
}
?>
<?php foreach($repetitionIndexes as $i): ?>

    <tr repeat="<?php echo $i ?>" repeat-template="row">
        <td><input type="text" name="amount_<?php echo $i ?>" value="<?php echo $_POST['amount_' . $i]; ?>"></td>

        <td><button type="remove">Delete</button></td>
    </tr>

<?php endforeach; ?>

<tr repeat="template" id="row" repeat-start="5" repeat-min="1" repeat-
max="7">
<td><input type="text" name="amount_[row]" value="<?php echo
$_POST['amount_[row]']; ?>"></td>
<td><button type="remove">Delete</button></td>
</tr>
<button type="add" template="row">Add Row</button>
</table>
</form>



ulysses

unread,
Mar 21, 2008, 2:04:23 PM3/21/08
to Web Forms 2.0 Implementation
Weston,

Super. Amazing script. It works great. For the life of me I don't
think I could come up with this code. Thank you so much.

I modified the script a little bit (see code below) since it wasn't
behaving the way I envisioned it. Everytime I submitted a post, it was
adding 5 new rows and it would hang if I exceeded the number of rows
set to maximum of 7. I ended up setting the repeat-start="1". I also
added code to ignore if the post is blank since it was adding a new
row everytime I posted. I wish I could set 5 rows at the outset and
add 1 row after each submit, but this is great.

Thanks so much for your help. It's much appreciated.

Ulysses

<form id="claim" method="post" enctype="multipart/form-data"
action="<? echo $_SERVER['PHP_SELF']; ?>">
<table>
<?php
$repetitionIndexes = array();
foreach($_POST as $key => $value){
if(preg_match("{^amount_(\d+)}", $key, $matches))
$repetitionIndexes[] = (int)$matches[1];
}

?>
<?php foreach($repetitionIndexes as $i): ?>
<?php if ($_POST['amount_' . $i]=="") { } else {?>
<tr repeat="<?php echo $i ?>" repeat-template="row">
<td><input type="text" name="amount_<?php echo $i ?>" value="<?
php
echo $_POST['amount_' . $i]; ?>"></td>
<td><button type="remove">Delete</button></td>
</tr>
<?php } ?>
<?php endforeach; ?>
<tr repeat="template" id="row" repeat-start="5" repeat-min="1" repeat-
max="7">
<td><input type="text" name="amount_[row]" value="<?php echo
$_POST['amount_[row]']; ?>"></td>
<td><button type="remove">Delete</button></td>
</tr>
<button type="add" template="row">Add Row</button>
</table>
</form>


Reply all
Reply to author
Forward
0 new messages