Adam
Adam

Reputation: 623

Including form data in another page

When I post my form data:

<form action="layouts.php" method="post">
  <input type="text" name="bgcolor">
  <input type="submit" value="Submit" align="middle">
</form>

to a php page, "layouts.php", it returns the string, as expected.:

$bgcolor = $_POST['bgcolor'];
echo $bgcolor; //returns "red"
echo gettype($bgcolor); // returns "string"

But when I include "layouts.php" in another page it returns NULL.

<?php
  include("php/layouts.php");
  echo $bgcolor; //
  echo gettype($bgcolor); //returns "NULL"
?>

How do I pass the variable to another page?

Upvotes: 0

Views: 1417

Answers (3)

Mukesh Chapagain
Mukesh Chapagain

Reputation: 25948

layouts.php

<?php 
session_start();
$bgcolor = $_POST['bgcolor'];
$_SESSION['bgcolor'] = $bgcolor;
?>

new.php

<?php 
session_start();
echo $_SESSION['bgcolor'];
?>

Upvotes: 2

Jem
Jem

Reputation: 769

Give the form two action="" and see what happens. I just tried that on a script of mine and it worked fine.

A more proper way to solve this might exist, but that is an option.

Upvotes: 0

Blender
Blender

Reputation: 298076

You'll have to use a session to have variables float around in between files like that.

It's quite simple to setup. In the beginning of each PHP file, you add this code to begin a session:

session_start();

You can store variables inside of a session like this:

$_SESSION['foo'] = 'bar';

And you can reference them across pages (make sure you run session_start() on all of the pages which will use sessions).

Upvotes: 4

Related Questions