Hi all
I am trying to keep my code tidy by having form processing happen within functions on a new site I am building. I used to just stick a lot of PHP at the top of each page to process forms but that was messy.
So now at the top of my file I have
include("global/functions.php"
;
$formresult=process_profile_update();
Then later on my form code is something like:
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" size="20" name="submitted" value="true">
input size="30" type="Text" name="firstname"<?if(isset($firstname)){print " value=\"".$firstname."\"";}?>>
and so on. This used to work great when I handled the code within the same page - the "submitted" variable told my form handling code the form had been submitted, and if there were no errors the page would redirect to a thank you page, otherwise the page would display again and the code
<?if(isset($firstname)){print " value=\"".$firstname."\"";}?>
would repopulate the form with the data the user originally entered (so they wouldnt have to enter it again).
Now what happens is the function checks for the "submitted" variable and if not set, does nothing and the page loads as usual. If set, the function processes and parses the form data.
The return value from the function is the error message which is displayed on the page as well
Now, I am calling a function, which means the PHP variables created by using
while (list($key,$value) = each($_REQUEST)) {
$$key = $value;
}
on the form are created within in the function and wont exist outside it, so when the form reloads values like $firstname are empty and the form doesnt repopulate
I guess I could make those variables global within the function so the page could reuse them (somehow?), but is there a better way to do this? I can post full code if required
many thanks in advance
I am trying to keep my code tidy by having form processing happen within functions on a new site I am building. I used to just stick a lot of PHP at the top of each page to process forms but that was messy.
So now at the top of my file I have
include("global/functions.php"
$formresult=process_profile_update();
Then later on my form code is something like:
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" size="20" name="submitted" value="true">
input size="30" type="Text" name="firstname"<?if(isset($firstname)){print " value=\"".$firstname."\"";}?>>
and so on. This used to work great when I handled the code within the same page - the "submitted" variable told my form handling code the form had been submitted, and if there were no errors the page would redirect to a thank you page, otherwise the page would display again and the code
<?if(isset($firstname)){print " value=\"".$firstname."\"";}?>
would repopulate the form with the data the user originally entered (so they wouldnt have to enter it again).
Now what happens is the function checks for the "submitted" variable and if not set, does nothing and the page loads as usual. If set, the function processes and parses the form data.
The return value from the function is the error message which is displayed on the page as well
Now, I am calling a function, which means the PHP variables created by using
while (list($key,$value) = each($_REQUEST)) {
$$key = $value;
}
on the form are created within in the function and wont exist outside it, so when the form reloads values like $firstname are empty and the form doesnt repopulate
I guess I could make those variables global within the function so the page could reuse them (somehow?), but is there a better way to do this? I can post full code if required
many thanks in advance