note 33854 deleted from language.variables.variable by philip

From: Date: Sun, 28 Jan 2007 17:52:13 +0000
Subject: note 33854 deleted from language.variables.variable by philip
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-122297@lists.php.net to get a copy of this message
Note Submitter: �yvind Vestavik, NTNU Norway ---- 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> "; } ?> [Editor Note: Many (new) people do this but it's much easier to simply use arrays instead of variable variables for this task. name="somename[]" in the form. Also this code requires register_globals = on, so, fix that. -Philip]

« previous php.notes (#122297) next »