Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dealing with form variables in functions

Status
Not open for further replies.

pr0fess0r

Programmer
Jul 10, 2001
25
NZ
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=&quot;post&quot; action=&quot;<?=$_SERVER['PHP_SELF']?>&quot;>
<input type=&quot;hidden&quot; size=&quot;20&quot; name=&quot;submitted&quot; value=&quot;true&quot;>
input size=&quot;30&quot; type=&quot;Text&quot; name=&quot;firstname&quot;<?if(isset($firstname)){print &quot; value=\&quot;&quot;.$firstname.&quot;\&quot;&quot;;}?>>

and so on. This used to work great when I handled the code within the same page - the &quot;submitted&quot; 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 &quot; value=\&quot;&quot;.$firstname.&quot;\&quot;&quot;;}?>
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 &quot;submitted&quot; 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
 
All those variables are already global. In fact, they are superglobal.

I strongly recommend that you access data from an HTML form as $_POST['myfield'] versus $myfield.

Accessing an element of $_POST is self-documenting -- you will know, six months from now, where the value came from.

Also, $_POST is one of PHP's superglobal variables. It is available in all scope contexts.

For more information on $_POST, $_GET, and the other superglobals, read here: Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top