|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2002-04-17 06:16 UTC] sander@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 14:00:01 2025 UTC |
You will find the question on the bottom of this message! The situation: Let's say that I have 2 or more forms and want to store the posted values in a php-include-file. This might look like this: 1. Display the 1st form. The user enters some values and submits the form. 2. The script now stores the values in a file which can later be included by PHP. 3. Display the 2nd form with 2 submit buttons (back and next). The user enters values again, then presses the back button. 4. 1st we include the file we have stored in "2", then display the form, filled out with the values from the included file. Ok, now the problem in detail: The used forms may vary, each time the program executed, so the algorythm for storing the values needs to be kind of flexible. That's the count of values to store is diffenrent for each of the forms. To make it easier, I use similar names for the variables names (var_1, var_2 ... var_n) in all of the forms. Some code for further explanation: (I'm using old style parameters to make it simpler here.) <pre> // File form1.inc.php: <?php include("functions.inc.php"); storeValues($prevpage); include("values1.inc.php"); ?> <form action="index.php" method="post"> <input type="text" name="prevpage" value="1" /> value1: <input type="text" name="var_1" value="<?php echo $var_1; ?>" size="30" maxlength="30" /><br /> value2: <input type="text" name="var_2" value="<?php echo $var_2; ?>" size="30" maxlength="30" /><br /> ... <input type="submit" value="Next"> </form> // end File // File form2.inc.php: <?php include("functions.inc.php"); storeValues($prevpage); include("values2.inc.php"); ?> <form action="index.php" method="post"> <input type="text" name="prevpage" value="2" /> value1: <input type="text" name="var_1" value="<?php echo $var_1; ?>" size="30" maxlength="30" /><br /> value2: <input type="text" name="var_2" value="<?php echo $var_2; ?>" size="30" maxlength="30" /><br /> ... <input type="submit" value="Back" name="back"> <input type="submit" value="Next" name="next"> </form> // end File // File functions.inc.php: <?php function storeValues($page) { $fp = "values$page.inc.php"; if(file_exists($fp)){ $fp_new = "values$page.inc.new"; if($fh = fopen($fp, "r")){ if($fh_new = fopen($fp_new, "w")){ while(!feof($fh)){ $ln = fgets($fh, 1024); // HERE COMES THE TRICK!! if(ereg("var_([0-9]{1,2})", $ln, $var)){ $vvar = "var_{$var[1]}"; // variable variable global $$vvar; // globalize $var_x from previously submitted form $tmp = "$"; $tmp .= "$vvar = ${$vvar};\n"; } fputs($fh_new, $ln); } fclose($fh_new); } fclose($fh); } if(file_exists($fp_new)){ unlink($fp); rename($fp_new, $fp); } } } ?> // end File // File values1.inc.php or values2.inc.php ... <?php $val_1 = "xyz"; $val_2 = "zyx"; ... ?> // end File // File index.php <?php // find the button used to submit if(isset($next)) // button was "next" $page = $prevpage + 1; else if(isset($back)) // button was "back" $page = $prevpage - 1; else $page = 1; include("form$page.inc.php"); ?> // end File </pre> I hope you find the source code useful, because its not the original code I'm working on. Well it's quite similar. Summary: I have a defined variable and set a variable variable to the same name as the defined one, then I'm globalizing it. The question i want to ask, is about the function "storeValues". Please have a look at it and tell me if the "TRICK" I'm using is ok with PHP? I want to make sure that it will still work with future versions of PHP. It looks a bit weird to me... But ok, it's working well with PHP 4.0.6 I did not find any useful documentation about it, so I needed to ask that here. c ya Liquid