james182 wrote:Should i put "session_start();" after all steps (ie: if($GET["step"]=="1"){ ) or not???
session_start() should be called before you try to reference any variables which you expect to be stored in $SESSION
james182 wrote:what should i use $SESSION['project'] OR session_register("project"); ???
the more up-to-date way is to refer to $SESSION['project']. Doing it the other way can get confusing because if your code has repeated references to the variable $project, it's not clear from reading it that $project is initialized from (and saved to) the current session.
james182 wrote:how do i get the form field data to get stored in a session var ?
STEP 1:
First, you have a form that gathers all the information you need. like this:
// *** FORM1.PHP ***
<form method="post" action="script.php">
<input type="hidden" name="step" value="1">
Please enter a value for my_var<input type="text" name="my_var" value=""><br>
Please enter a value for project<input type="text" name="project" value=""><br>
<input type="submit" name="submit" value="Submit">
</form>
Pay special attention to the METHOD=POST part. That will determine where you find the variables in the script. Pay attention to the ACTION=SCRIPT.PHP part because that determines which script is going to receive your form data. Pay attention the NAME=MY_VAR bit because that determines which variables get sent to SCRIPT.PHP. NOTE: Variable names are case-sensitive...so 'Project' is not the same thing as 'project'.
STEP 2:
Write the php script that will receive the form information. Since my method was 'post' on the form, I'm going to look for any values from the form in the var $_POST.
<?
// *** SCRIPT.PHP ***
// call session_start() to let PHP know that you want to save some vars in $_SESSION
session_start()
// check to see if somebody clicked Submit on the form
// NOTE: the *name* of the submit button was all lower case, its value was capitalized
if ($_POST['submit'] == 'Submit') {
echo 'Step was the hidden input:' . $_POST['step'] . '<br>';
echo 'my_var was a text input and they could type anything:' . $_POST['my_var'] . '<br>';
echo 'Is this the value you were looking for?:' . $_POST['project'] . '<br>';
// now, if you want to preserve the value of project in the session, here is how:
$_SESSION['project'] = $_POST['project'];
// now what? maybe we redirect to the next page of the form?
header('location: form2.php');
exit();
} else {
// fyi, you could output your form right here if you wanted
echo 'thanks for entering page 1...now move on to <a href="form2.php">step 2</a>';
}
?>
STEP 3:
Now for any other page on your site, you should be able to peek in the session variable and project should be there.
IMPORTANT: Sessions can get screwed up pretty easily. A session ID has to get passed around and is usually passed via cookies. You really might want to read about how this stuff works or you could have problems later that completely mystify you.
// *** FORM2.PHP ***
// call session_start() to tell PHP we need those session vars...and we might want to change them or set some more
session_start();
echo "I wonder if project is stored in session....hmmmm<br>";
if (isset($_SESSION['project'])) {
echo 'Hot damn! project is set to ' . $_SESSION['project'] . '<br>';
} else {
echo 'Rats, apparently not. :(<br>';
}
james182 wrote:EDIT: i'm going crazy trying to figure out way its not working.
You might consider googling around for a tutorial if php is confusing to you. I'll help you learn PHP but I'm not going to do your work for you.