Jump to content

Sessions and passing variables


dazz_club

Recommended Posts

Hello,

 

I'm creating a questionnaire to try and get better at understanding sessions but I've stupidly fallen at the first hurdle.

 

My problem is that I can't successfully pass variables from one page to another. You see, I'm trying to split my questionnaire into smaller questions, rather than having really long list of questions.

 

Here is what the php code in my first page looks like;

 

session_start();
$_SESSION['contact_us'] = $contact_us;
$_SESSION['response_time_email'] = $response_time_email;

//checks to see if form is submitted and a question (tick box) has been checked
if(isset($_POST['submitted'])) {
if(!isset($_POST['contact_us'])) {

                //if check box empty display
	$must_complete = "<div id=\"fail\"><p>Please select a contact method; <strong>email, tel, fax or post</strong></p></div> ";

}else{
                 //if checked take them to the next page
	if(isset($_POST['contact_us'])){

	//$confirm = 'yes '.$contact_us.'';
	header('Location:page2.php');
	}
}
}

 

the two variables $contact_us and $response_time_email are there to store the variables and then pass them to the next page.

 

Here is the php code in my second page, that receives it

 

session_start();
$contact_us = $_POST['contact_us'];
$response_time_email =  $_POST['response_time_email'];

//if previous button is submitted go to previous page
if(isset($_POST['previous'])) {

header('Location:index.php');

}

 

I think I could have it all mixed up here. If anyone can point in the right direction I would be grateful.

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/197866-sessions-and-passing-variables/
Share on other sites

In your first page you set the session vars, good. In your next page you convert POST to $contact_us. Bad, if you want what was in the session var. On your second page you need to do the reverse of the first page:

$contact_us=$_SESSION['contact_us'];

instead of using POST, that is just for posted form data.

 

 

HTH

Teamatomic

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.