Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSam Butler
3,141 PointsHow to make $_POST variables global after form redirect?
I'm trying to use the $_POST information submitted through a standard form (name, email address, details) to build the content on the page after a redirect. Here is my code:
<?php
//creating variables from form data submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_STRING));
$details = trim(filter_input(INPUT_POST,"details",FILTER_SANITIZE_SPECIAL_CHARS));
//Redirecting after succesful completion of form
header("location:index.php?status=complete");
//Error message if form is failed to complete
if ($name == "" || $email == "" || $details == "") {
echo "Please fill in required fields";
exit;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="CSS/stylesheet.css">
</head>
<body>
<div class="header" style="background-color: white" href="index.php">
<header>
<a href="index.html"><h1>Test site</h1></a>
</header>
</div>
</body>
<div class="wrapper">
<?php
//Using echo and var_dump to test content from form submission as
//defined in the variables in the topmost $_POST conditional******
if (isset($_GET['status']) && $_GET['status'] == "complete") {
echo "you successfully completed the form!";
echo $name;
var_dump($name);
} else { ?>
<form method="post" action="index.php">
<label for="name">Name</input><br>
<input type="text" id="name" name="name"></input><br>
<label for="email">Email</input><br>
<input type="text" id="email" name="email"></input><br>
<label for="details">Details</input><br>
<input type="text" id="details" name="details"></input><br>
<input type="submit"></input>
</form>
<?php } ?>
</div>
</body>
</html>
When I run this, I get the warning:
Notice: Undefined variable: name
on the page that I redirect to following form submission (relevant code section marked with ***** above)
I understand that this is probably because when I first set $name
, $email
, and $details
in the topmost php section of the code, those variables are defined locally.
How can I create variables out of the $_POST
data from the index.php form to be used after the page redirect, and on any other pages on the site?
1 Answer
Matthew Lang
13,483 PointsAs Ben Payne said, your best approach here is to use the superglobal $_SESSION. I recommend you read this brief tutorial and google 'PHP Sessions', or watch some videos on youtube. Also don't forget you're on Treehouse. Pretty sure there is some courses on Sessions throughout the beginner/intermediate PHP Courses (I can't remember, but there probably is as sessions are very useful).
Ben Payne
1,464 PointsThanks Mathew. I would definitely recommend using the $_SESSION
as well.
Ben Payne
1,464 PointsBen Payne
1,464 PointsYou would have to put the data you want in the query string or in the session. These would be available in the
$_GET
or$_SESSION
super-globals respectively.