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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing variables through a serverside redirect 2

Status
Not open for further replies.

monkle

Programmer
Feb 11, 2004
132
US
I'm building a registration page. I need to run validation checks on all of the input before saving it to the ms sql database. I'd like to seperate the functionality from the display, like this:

Data Entry Screen.php // front end, where user inputs data

Data Validation Screen.php // back end, validates data
entered by user. If valid,
redirect to script that
inserts user data, else

Redirects back to input screen
Marks invalid fields
and Fills fields with entered data

For the redirect, I am currently using this:
header("Location: ../register.php");
exit;


I know how to do this with GET or with SESSION variables, but I would like to know if it's possible to do this another way. (eg, POST, etc.) I haven't had any luck finding another way, if there is one.

Thank you for your time
 
Perhaps not using a redirect at all....

If you create a script in the general form of:

if (isset($_POST['some variable from the form']))
{
if (validate_input() == TRUE)
{
redirect_to_next_page();
}
else
{
display_form_with_fields_populated_and_error_messages();
}
}
else
{
display_blank_form();
}

Just make sure that each form output by the script is set to submit back to the script itself.

If the script is run without submitting data (ie., the script is run the first time), it merely outputs the form for the user to fill in.

If the script detects a form field from the form when run (the "if (isset($_POST..." clause), then it attempts to validate the input.

If the input can be validated, the script directs (using a "Location:" header) the browser to the next section of your site.

If the input cannot be validated, the script displays the form again, with the fields populated using data the user just submitted, with error messages.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
It took me a minute to work through exactly how I was going to run the logic, but I think I have it now. Thank you very much for helping me get it.

5 pages:

page 1: control file
page 2: html form file
page 3: validation file
page 4: sql insert file
page 5: confirmation

user hits page 1 by default.
If form data is blank, include html form file (page 2). Else if form data exists, include validation file (page 3).
Then if validation failed, display form file again with data entered and invalid fields marked.
Else if validation passed, include sql insert file, and if sql insert succeeded, redirect to a welcome screen. If sql insert fails, handle errors accordingly.

I think this is more or less what you were saying. If not, feel free to comment. I will post my success here when I'm done, and/or any changes I had to make. Thanks again.
 
Oh, one quick question. Is there a good reason to use this:

if (isset($_POST['variable']))
{

Instead of this:

if ($_POST['variable'])
{
 
You may be saying the same thing, but I think we have a metaphor shift between us.

I don't think of a PHP script in terms of pages. I think of a PHP script in terms of a script outputting dynamically-generated streams of HTML.

So when I am gathering user input, I think in terms of a series of scripts, each handling in its entirety a single "block" of user input. Script "get_user_info.php" will produce a form into which the user can input his name and address, validate and process that data, then send the user's browser to the next input-gathering and processing script in the series. Each script produces forms that submit user data back to the script itself.

In specific to this question:[ul][li]if the script is run with no input, the script outputs an HTML stream that produces an unpopulated HTML form.[/li][li]if the script is run with input data (the script knows by looking for a form input field name that it earlier produced as output), it then attempts to validate the input[/li][ul][li]If the input validates correctly, the script inserts the page into the database and outputs an HTTP header that redirects the browser to another script[/li][li]if the input cannot be validated, the script produces an HTML stream that reproduces the form with fields prepopulated and gives the user error message(s).[/li][/ul][/ul]



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
you can also try

Code:
$something = $_POST['somevar'];
$someother = $_POST['somvar2'];

header("location:mypage.php?var1=$something&var2=$someother");


Bastien

Cat, the other other white meat
 
sleipner214, I think that you are correct in saying that there is a metaphore shift between us. Another way to put what I was saying, is that I am dynamically generating the script itself, as well as the html form.

This is actually a re-write. The way I had it before, all of everything was in one file that submitted back to itself, and operated based on a series of if checks and variables. The reason for the re-write is that the file became large enough to be a nightmare to manage.

bastienk, I considered the possibility of appending all of the form data in a header redirect and using GET. Also, the way I had it before was using the $variable = $_POST['variable'] method. It's necessary to use that method if you are going to be passing variables in through POST and GET both. My concern is that it is much cleaner to use only POST, as only one set of variable setting is necessary, instead of having two sets of variabe setting, one for each type. Also, GET leaves you with ugly url's. Yes, it allows users to bookmark, but I see no reason to allow users to bookmark a partially saved registration session.

The reason for splitting distinct sections of the script into seperate files and including them is to make cleaner, easier to manage code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top