简体   繁体   中英

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.

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.

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>
?>

您可以使用会话,POST数据,GET数据,cookie和数据库数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM