note 33854 added to language.variables.variable
| From: | "�yvind" at rack1 dot php dot net | Date: | Wed, 09 Jul 2003 12:45:10 +0000 |
| Subject: | note 33854 added to language.variables.variable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-51774@lists.php.net to get a copy of this message | ||
I found a nice application to variable variables to be dynamic forms. Say that you have a form for
entering information about persons, but don't know in advance for how many persons your users
want to enter information. Just provide an extra submit-button to add an extra entry in the form.
The previously entered values from the form will be displayed again and an extra form entry will be
provided. There is no limit to how many times the users can add entries. When finally hitting the
submit button, all entries can be handled.
<?php
if ($submit=='submit'){
echo "Handle received data from form here";
}
else{
//setting initial number of rows
if (!isset($nr_entries)){
$nr_entries = 1;
}
//if user pushed "more entries", increment $nr_entries by one
if ($submit=='More Entries'){
$nr_entries++;
}
echo"
<FORM METHOD='post'>\n
<INPUT TYPE='hidden' NAME='nr_entries' VALUE='$nr_entries'>\n
";
for ($i=1; $i<=$nr_entries; $i++){
$temp = "name_$i";
$value = $$temp; // Here is how i reference entered values
echo "<INPUT TYPE='text' name='name_$i'
value='$value'><br>\n";
}
echo"
<INPUT TYPE='submit' NAME='submit' VALUE='More
Entries'><br>
<INPUT TYPE='submit' NAME='submit' VALUE='submit'>
</FORM>
";
}
?>
----
Manual Page -- http://www.php.net/manual/en/language.variables.variable.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33854
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33854&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33854&report=yes