Gabriel Luciano
Gabriel Luciano

Reputation: 19

Using $_SESSION to carry a php variable from one page to the next?

I created an app to receive and tally votes. I am trying to make the voter's selection carry over to a confirmation page so that I can display the name of the candidate for whom they voted on the confirmation page.

I am trying to use $_SESSION on the variable of the selected candidate from the voter's submission, and then call the variable on the confirmation page, however I continue to get undefined variable error.

voterSubmit.php:

<?php
$selectedCandidate = $_POST['candidateid'];

session_start();
$selection = $_SESSION[$selectedCandidate];


//Redirect to results page
header("Location: views/confirmation.php");

confirmation.php

<?php

session_start();
$_SESSION[$selectedCandidate] = $selection;

include "views/confirmation.php";

confirmation view:

<?php include "../partials/header.php"; ?>

<h1>Thanks For Your Vote!!</h1><br>
 //This is where the error occurs (on my selection variable):
<h2>You voted for <?=$selection?> 

<a href="results.php">View Results</a>


<?php include "../partials/footer.php"; ?>  

I want the name of the selected candidate to appear on the confirmation page by way of the $selection variable. However, all I receive on the front end is an "undefined variable" error. I also would like to note that instead of using the $selectedCandidate variable in my session, I have also tried grabbing the name directly by just using the name of the radio button selection as such:

$_SESSION['candidateid'] = $selection 

I also would like to mention that i have tried the reverse:

on confirmation.php:

session_start();
$selection = $_SESSION[$selectedCandidate];

on voteSubmit.php:

session_start();
$_SESSION[$selectedCandidate] = $selection;

Upvotes: 0

Views: 122

Answers (5)

Anadkat Bhavik
Anadkat Bhavik

Reputation: 111

You have done 2 mistakes in your code: 1. Session should be defined in starting of page it means you have to define it like this:

<?php 
session_start();
//php code goes here.
?>

2.wrong initialization of session variable .you should do it like this:

<?php
$_SESSION['selectedCandidate']=$selectedCandidate;
?>

Upvotes: 0

Vishakha Gupta
Vishakha Gupta

Reputation: 1

There are 2 issues in your code :

  1. session_start() defined in the wrong place.
  2. Wrong way of assigning value to your session variable.

Answer :

  1. session_start() should be the first thing defined after your php tag.
  2. Correct way to declare session variables is : $_SESSION["selectedCandidate"] = $selectedCandidate; where, $selectedCandidate is the value to be assigned to your session variable, named selectedCandidate.

Upvotes: 0

elbrant
elbrant

Reputation: 791

voterSubmit.php looks ok... but I don't understand why you have include "views/confirmation.php"; in the confirmation.php file.

<?php

session_start();
$_SESSION[$selectedCandidate] = $selection;

include "views/confirmation.php";

Try coding your HTML/PHP this way:

<?php
/* confirmation.php */

session_start();
$_SESSION[$selectedCandidate] = $selection;

require_once("../partials/header.php");

//This is where the error occurs (on my selection variable):
echo <<<_STRT
    <h1>Thanks For Your Vote!!</h1><br>
    <h2>You voted for $selection</h2>      
    <p><a href="results.php">View Results</a></p>
_STRT;

require_once("../partials/footer.php");
?>

I essentially eliminated a lot of start/stop code. The start of your PHP recalls the session and variable. Then I go into the HTML portion to provide the results to your site visitor. Give it a shot. Comment on it if it doesn't resolve your issue so we can rethink it.

Upvotes: 0

Jeff Mattson
Jeff Mattson

Reputation: 393

I think session_start() needs to be the first line after the php tag.

$_SESSION is an array. So you need to assign it values like:
$_SESSION['keyname'] = $value;

voterSubmit.php:
<?php
session_start();
$_SESSION['selectedCandidate'] = $_POST['candidateid'];

//Redirect to results page
header("Location: views/confirmation.php");

confirmation.php
<?php
session_start();
$selection = $_SESSION['selectedCandidate'];

include "views/confirmation.php";

Upvotes: 0

EvE
EvE

Reputation: 750

Your using the $_SESSION variable incorrectly.

Try:

voterSubmit.php:

<?php
$selectedCandidate = $_POST['candidateid'];

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


//Redirect to results page
header("Location: views/confirmation.php");

confirmation.php

<?php

session_start();
$selection = $_SESSION['selectedCandidate'];

include "views/confirmation.php";

Upvotes: 1

Related Questions