How to pass Post (variable) info to a second page - php

I have an HTML order form that collects info and then on submission passes that to a PHP form which sends an email. Then I have the PHP form forwarding to a PHP confirmation page.
I am using the POST method on my HTML form.
I am forwarding to the PHP confirmation page with header() after it sends the email.
Everything is working fine, but I need to know how to pass the variables along to the confirmation page. I can use them on the email page, but they are dropped on the page forwarding. I'm sure it's easy, but I have a lot to learn yet. :-) Do I have to create a file to store the variables, or can I pass them along?
I know I could display the confirmation page with the PHP page that sends the email (I did have it setup that way) but we have had users bookmark that page which will resend the order every time they visit that bookmark. So I'm separating the pages so if they bookmark the confirmation page it will at least not resend an order.
Make sense? Thanks for your help!

You can keep the parameter sent in POST in session variables. This way on your second page, you can still access them.
For example on your first page :
<?php
session_start();
// ... //
$_SESSION['value1'] = $_POST['value1'];
$_SESSION['value2'] = $_POST['value2'];
header('Location: youremailpage.php');
die();
// ... //
?>
And on your second page :
<?php
session_start();
// ... //
$value1 = $_SESSION['value1'];
$value2 = $_SESSION['value2'];
// ... //
?>

Related

set commands for the condition "I'm redirected to another page" (php)

I'm using php
I have a process form for a comment page that when you click on submit, you will be redirected to the main page of the website
when I redirected to index.php from my processform.php, I want to see an alert in my index that "Your comment was saved!" (It needs that my index page understand that I'm coming from processform.php)
How can I do this?
you should use something like sessions.
set session on submit form. and on index.php check if session has set with the special key. then show the alert that you want.
on form submit and success :
// Start the session
session_start();
// Set session variables
$_SESSION["processform"] = "Your comment was saved!";
on index.php
if(isset($_SESSION["processform"]){
// do alert
}

How to redirect to the same php page after successfully logging

I have an assignment whereby I provide an html login form on a page called index.php and upon entering the correct username and password you will be redirected to the same index.php page but now the login form will disappear and be replaced with html links to three other php pages.
In other words the html links are not visible before successfully logging only after. All of this happens on the same php page (index.php). Any suggestions would be greatly appreciated.
First of all a brief knowledge to session variable.
1. You can Set the Session using $_SESSION['username']="your-username-from-html-post";
2. This is a global variable i.e. it will be available for use to all files throughout your website.
3. To delete session you can use unset($_SESSION['username']);
4. Session are very useful to make a login form in PHP and HTML as they provide a universal format to check if the user is logged in or not. By the Way- the session variable will be for your website only and for the same computer only on which user has logged in not for other computers(obviously)
Now your Answer
You can write in your index.php like this
If (isset($_SESSION['your-session-variable'])) {
echo 'YOUR LINK 1';
echo 'YOUR LINK 2';
echo 'YOUR LINK 3';
} else {
//insert your form code here
}
Replace $_SESSION['your-session-variable'] with your own Session Name
What this does is It check is the $_SESSION variable is set by isset
If answer is true it will print your links else it will print your html code!

passing varables' value from one form to another in php

I have three forms - payment.php , payment1.php and paydb.php . payment.php contains the front end form.payment1.php contains the back end of the form of payment.php. whereas we are shifting to paydb.php from payment1.php. Now I'm filling the form by entering member number in payment.php which is retrieved in a variable $member_no in payment1.php .Now I want to get the value of member_no in paydb.php . How to do that ?
After receiving $member_no in payment1.php redirect to paybd.php with a get array
using
header('Location: http://www.example.com/paydb.php?member_no=$member_no');
then receive $_GET['member_no'] number and assign to a variable
example:
$member_no = $_GET['member_no']
The first thing is make sure you are not passing sensitive information where the public can see it. Such as in a URL.
As soon as you get the member's number... store it in a session variable.
You can probably do this when they log in.
session_start();
$_SESSION['member_no'] = $member_no;
OR on the first payment page (assuming 'member_no' is the name of the form element being passed) like this...
$_SESSION['member_no'] = $_POST['member_no'];
Now that session will persist as long as the visitor has their browser open and you don't have to worry about passing it from page to page.
You can use that session on any subsequent page simply by calling it.
<?php echo $_SESSION['member_no'] ?>
Without showing this information to the public.
ALWAYS make sure you place this at the top of any page where you want to use session variables.
if (!isset($_SESSION)) {
session_start();
}

how to assign post globals on a redirected page?

I have a login form which sends 3 post values from username, password and submit button. But my form processor has 3 pages one is validation.php which validates the field second is read.php which checks the posted values against db and third is login.php which is a result of login success. All redirect to each other respectively on success. Problem here is that when I try to access the user posted values from form in read.php (redirected page) not validate.php (action page) I get an error of undefined index.
I really don't see why you are doing all those redirects, but if you want to make the data more persistent you could use a session variable, because the $_POST superglobal is only set for the current request.
firstfile.php
<?php
session_start();
$_SESSION['posted_data'] = $_POST;
other file
<?php
session_start();
var_dump($_SESSION['posted_data']);
However as already stated you may really want to reconsider doing all the requests.
UPDATE
Besides the fact that you will loose your data you are also doing multiple (unneeded) requests to simply sumbit the form. The only redirect that should happen is to the successpage when you have done all you work. See this for more information: http://en.wikipedia.org/wiki/Post/Redirect/Get
If you are look to keep you code clean you could always just include the other files or go for an OOP approach.
You should do one page only that will do all the work. That doesn't seem too complicated of a script, so I would advise putting everthing together on one page.
You did not provide any code so I'll show you a general example. I just typed it without rereading so it's not pure PHP syntax, just the spirit:
<?php
$login=$_POST['login'];
$pwd=$_POST['pwd'];
$dbcheck = mysql_fetch_array(mysql_query("SELECT COUNT(1) FROM table WHERE user =$login and pwd = $pwd"))
if($dbcheck[0] > 0) {
//Login success
//Setup your session variables, cookies, etc
//Then you can do your redirect here
} else {
//Page for wrong login
}

PHP Session Variables Not Set

Im having a strange problem.
I have a registration script for a car show's website I manage and for no reason the older code is broken. I know it worked before, but now does not.
I am setting session variables correctly. I am saving an entire Array of variables to the session and I know the session works once the form is submitted as the form reloads to a new page and I display the registration info for confirmation. However, the script then continues to a PayPal page where it takes the user through a payment process. It then redirects back to the site where the registration info is submitted to the MySQL database upon the completion of the payment process.
HOWEVER, when the users return to the confirmation page where the data is submitted, the session variable is completely blank. I have echo'ed all the variables and there's nothing there.
To test where its breaking, I put a link on the page you're taken to after filling out the form linking directly to the confirmation page where the info is dumped to the SQL server. When you link the link, the info is still there. However, if you were to take that same URL and paste it into the address bar (aka NOT using the link), the variables are blank.
So at this point, I am lost as to why the variables are fine when you link and click to the page, but not when you past the same url into the address bar and not click the link. Session is started on EVERY page and all are on the same domain name.
Any help?!
===== EDIT =====
Test Code (to give a rough idea)
How I am setting the session variables. The session_start(); function is called at the loading of every page on this site for continuity across other pages:
$storedVars = array(1 => $name, $email, $state, $year, $make, $model, $generation, $color, $now, $paymentType);
$_SESSION['registration']=$storedVars;
On the confirmation page after PayPal where the information gets sent to the database, the following code is run (again, session_start(); already called):
$storedVars = $_SESSION['registration'];
$name = $storedVars[1];
$email = $storedVars[2];
$state = $storedVars[3];
$year = $storedVars[4];
$make = $storedVars[5];
$model = $storedVars[6];
$generation = $storedVars[7];
$color = $storedVars[8];
$now = $storedVars[9];
The 10th array variable, paymentType, is not needed at this point as its only used for the page after the form is submitted)
When those variables are echo'ed on the confirm page after following through the steps on paypal OR pasting the right URL into the address bar after filling out the form, nothing is there.
HOWEVER..
When you click the link on the data validation page that follows after the form is submitted, the variables are still there.
Your last comment makes me believe that your session was set at http://yoursite.com/ and the return url you are sending to PayPal is http://www.yoursite.com/ (or vice versa). "www" is really a subdomain to your base domain and considered to be a different host as far as your browser is concerned.
Long story short, use the following for the return url when setting up your token with paypal:
$_SERVER["HTTP_HOST"] . "/Path/To/PaypalReturnPage.php"

Categories