/    Sign up×
Community /Pin to ProfileBookmark

Form Processing

What is the best way to process a form when you want to display the results on a different page & keep the session variables?

I have a form that I’m trying to process and if the user does not enter required information in certain fields they are kept at the same page with the error displayed about which required fields they need to fill out before they can proceed.

So, I’m trying to post the info from edit.php to results.php. My form action on edit.php is edit.php & then I’m using header() in an if/then statement if all variables are filled out.

But when I try to redirect with header() my session variables don’t convert, nor does the $_POST variables convert.

Is there a better way to do this without having to query the database to restore all my variables or is this the best way to process the form?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@DexterMorganMay 30.2010 — by 'variables' I assume you mean the form fields that the user needs to fill out?

So, I'm trying to post the info from edit.php to results.php. My form action on edit.php is edit.php[/QUOTE]

shouldnt the form action be results.php if you are going from edit.php?

The way I usually do this is check that all of the fields have been passed over to results.php with code in results.php.

So in the results.php have:

[code=php]
if( isset($_POST['test']) ){
//do what you wanna do
}else{
header('location:test.php');
}
[/code]
Copy linkTweet thisAlerts:
@quark76authorMay 30.2010 — I have action set to the same page for a user error loop, such as you didn't not fill out username.

Then, if they successfully enter all the required fields, I will send them to results, via header(location:results.php). But, when header is called, it doesn't have any of the $_POST nor $_SESSION variables.

So, how do I get my variables back into the session? Or should I handle my user error messaging differently?
Copy linkTweet thisAlerts:
@tirnaMay 30.2010 — submitting a php page to itself is fine but without seeing your code, it's hard to tell exactly why your session vars. are not seen in results.php

But the first obvious thing to check is that you have

[CODE]
session_start();
[/CODE]


at the top of edit.php and results.php

If you have, and if pain persists ?, then maybe post the code in both php files so we can see what is going on.
Copy linkTweet thisAlerts:
@quark76authorJun 01.2010 — submitting a php page to itself is fine but without seeing your code, it's hard to tell exactly why your session vars. are not seen in results.php

But the first obvious thing to check is that you have

[CODE]
session_start();
[/CODE]


at the top of edit.php and results.php

If you have, and if pain persists ?, then maybe post the code in both php files so we can see what is going on.[/QUOTE]


Ok. It looks like I wasn't passing any variables with the header () function nor was I using $_GET.

For example:

test.php having

[code=php]header("location:result.php?var1=$var1&var2=$var2");[/code]

then result.php having

[code=php]$var1 = $_GET['var1'];
$var2 = $_GET['var2'];[/code]


Then I can query the DB for the rest of my variables.

I hope this helps someone else with form processing.
×

Success!

Help @quark76 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...