user3704470
user3704470

Reputation: 9

Passing Variables Between Scripts in PHP

I have a simple question, with maybe not so simple of an answer. I want to be able to set a variable in one script and pass that value and variable to another script. Without passing it through the url, and having the ability to pass an array.

So I have index.php in there I have a variable

<?php
$myvariable = '';
<form action=editRecord.php>
do some form actions here and submit moving to editRecord.php
</form>
?>

Now in editRecord.php

<?php
header('Location: go back to index.php);
run functions and edit the Mysql DB
//Ok now here is where I want to assign a value to $myvariable and pass it back to index.php
?>

Is this possible? I am sorry for the amateurish question, but I am very green when it comes to php.

Thank You.

Upvotes: 0

Views: 159

Answers (3)

chresse
chresse

Reputation: 5805

you can set it in the $_SESSION variable:

<?php
session_start();
$_SESSION["myVar"] = "blabla";
....

Of course you can store an array() in this variable too.

Upvotes: 1

keiv.fly
keiv.fly

Reputation: 4015

You can use sessions, POST data, GET data, cookies and database data.

Upvotes: 0

John Conde
John Conde

Reputation: 219814

Just pass that information in the querystring. Then you can access it via $_GET. If it doesn't exist, just set the value to an empty string or whatever default value you want it to have.

// editRecord.php
<?php
// do db dtuff
header('Location: index.php?myvariable=somevalue');
?>

// index.php
<?php
$myvariable = (isset($_GET['myvariable'])) ? $_GET['myvariable'] : '';
<form action="editRecord.php" >

</form>
?>

You can also use session variables:

// editRecord.php
<?php
session_start();
// do db stuff
$_SESSION['myvariable'] = 'somevalue';
header('Location: index.php');
?>

// index.php
<?php
session_start();
$myvariable = (isset($_SESSION['myvariable'])) ? $_SESSION['myvariable'] : '';
<form action="editRecord.php" >

</form>
?>

Upvotes: 0

Related Questions