0

.I have three php pages:

page1.php page2.php page3.php

on page1.php i have this code:

<form id="try" method="post" action="page2.php">
Batch: <input id="batch" name="batch" type="text"/><br />
Dept: <input id="dept" name="dept" type="text"><br />
<input type="submit" />
</form>

on page2.php i am able to use the values inserted on page1.php by simply calling them using $_POST['batch']; and $_POST['dept'];

but what i want to do next is to pass the values of batch and dept from page2.php to page3.php. or maybe from page1.php to page2.php since i think it's just the same.

.Help pls! Thanks in adv

@kjy112 - i'm confused, since i am using

<form method="post"> 

should i be starting my session on page2.php by using the following:

session_start();
$_SESSION['batch'] = $_POST['batch'];
$_SESSION['dept'] = $_POST['dept'];

and then use

session_start();
$batch = $_SESSION['batch'];

to use it on page3.php?

3
  • it should work if you set it in the session. as long as you make sure you session_start() Commented Mar 8, 2011 at 20:41
  • .alright another question, what do you mean by session_start() must be called before outputting anything to the browser? will this also work if my purpose is to use batch and dept on SQL queries? Commented Mar 8, 2011 at 20:56
  • i am not familiar using session w/ batch and dept on SQL queries (what do u mean by dept?). As far as session_start, To use cookie-based sessions, session_start() must be called before outputing anything to the browser. Basically make sure you call that before you use session and make sure it's on top of your page. Commented Mar 8, 2011 at 23:03

5 Answers 5

3

Per @Crayon Violent: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

You'll need to use PHP SESSION you can get/set like this:

// page1.php

session_start();
$_SESSION['myvar'] = 'test';

//page2.php

session_start();
$myvar = $_SESSION['myvar'];
echo $myvar; //should be test;

//page3.php
session_start();
echo $_SESSION['myvar']; //should give u test still
Sign up to request clarification or add additional context in comments.

2 Comments

one thing to note is that session_start() must come before any output, even whitespace on all pages you want to use it on (unless you want to employ output buffering which you shouldn't be doing regardless)
could you please check out my question again, i have a following question. tnx!
1

make use of session variables.

Comments

0

you can save the $_POST values to a $_SESSION variable:

$_SESSION['POST'] = $_POST;

1 Comment

.does this mean i can use something like session_start(); $_SESSION['batch'] = $_POST['batch']; $_SESSION['dept'] = $_POST['dept'];
0

One way to move values from one script to another, and without worrying about which script is accessed in what order, is to use a session.

When your scripts start, you open the session. This gets you access to the $_SESSION superglobal, like $_POST. You can write to the $_SESSION array in one script and read it out in another. This is all handled on the server, so you can store any data you want into the session without worrying about the user seeing this data. It's very useful and is often used with multi part form, sites with logins, and to track user choices over as many pages as the user accesses.

Comments

0

just answering questions in comments

what do you mean by session_start() must be called before outputting anything to the browser?

mean it's best practice to put session_start() directly after <?php like <?php session_start(); and make sure that php starting tag is not after <html> tag.

will this also work if my purpose is to use batch and dept on SQL queries?

yes, of course, just set all your parameters (sql, dept) in $_SESSION[''] variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.