简体   繁体   English

在3个PHP页面之间传递变量

[英]Carrying Variables Between 3 PHP Pages

I am creating a set of 3 pages, each with a dropdown menu. 我正在创建一组3页的页面,每个页面都有一个下拉菜单。 The values from the previous dropdown menus will populate the next dropdown menu. 前一个下拉菜单中的值将填充下一个下拉菜单。 I understand that I must use session variables to hold the value of each dropdown menu into later pages. 我了解我必须使用会话变量将每个下拉菜单的值保存到以后的页面中。 As a simple test, I echo the variables to see if they have been carried over -- and that's where the problem is. 作为一个简单的测试,我回显这些变量以查看它们是否已被继承-这就是问题所在。

I have three different files: choose_cc.php, choose_uni.php, and choose_major.php The value from the dropdowm menu in choose_cc.php does get echoed in choose_uni.php -- however the value from choose_cc.php does NOT get carried over into choose_major.php -- despite me storing it into a session variable. 我有三个不同的文件:choose_cc.php,choice_uni.php和choose_major.phpchoose_cc.php中dropdowm菜单中的值会在choose_uni.php中得到回显,但是choose_cc.php中的值不会被带入choice_major.php-尽管我将其存储到会话变量中。

the flow of pages is like this; 页面的流程是这样的;

choose_cc.php --> choose_uni.php --> choose_major.php Each php file has it's own form. choice_cc.php-> choice_uni.php-> choice_major.php每个php文件都有自己的形式。 The problem lies in when I try to call the value from choose_cc.php into choose_major.php, i have issues. 问题出在我尝试从choose_cc.php调用值到choose_major.php时,我遇到了问题。

The name of the form on choose_cc.php is choose_cc . choice_cc.php上表单的名称为choose_cc The name of the form on choose_uni.php is choose_uni . choice_uni.php上表单的名称为choose_uni

So for example, in choose_uni.php, I retrieve the value from the dropdown menu on the previous page (choose_cc.php) like this: $_SESSION['choose_cc'] = trim($_GET['choose_cc']); 因此,例如,在choose_uni.php中,我从上一页的下拉菜单(choose_cc.php)中检索值,如下所示:$ _SESSION ['choose_cc'] = trim($ _ GET ['choose_cc']); //fetches cc from previous page $user_cc = $_SESSION['choose_cc']; //从上一页获取抄送$ user_cc = $ _SESSION ['choose_cc']; echo $user_cc; 回声$ user_cc;

and when I echo it as I did above, it works! 当我像上面一样回显它时,它就起作用了! Okay perfect! 好的,完美!

But when I head onto choose_major.php, I try retrieving the value again from the form, but to no avail like this; 但是,当我进入choose_major.php时,我尝试再次从表单中检索该值,但无济于事。

echo $_SESSION['choose_uni']; //this works
echo $_SESSION['choose_cc']; //this doesn't work

I have made sure to store to do session_start() on the beginning of each page as well. 我已经确保在每个页面的开头都存储要执行的session_start()

Please help me out! 请帮帮我! this is driving me insane! 这让我发疯!

Create a new "see.php" with this: 使用以下命令创建一个新的“ see.php”:

session_start(); 
print_r($_SESSION);

and execute it after each choose_cc.php, choose_uni.php and choose_major.php to take a look the session you have after you run your programs. 并在每个choose_cc.php,choice_uni.php和choose_major.php之后执行它,以查看运行程序后的会话。

Simple as this: 就这么简单:

a) add session_start(); a)添加session_start(); at the beginning of ALL the involved pages. 在所有相关页面的开头。 Some weird stuff happens sometimes if you put a space before it, or even a new line. 如果您在空格之前添加空格,甚至换行,有时也会发生一些奇怪的事情。 So be sure it is the very first thing in your script. 因此,请确保它是脚本中的第一件事。

<?php
session_start();
?>

b) if needed, check the variable to save into session for not empty(); b)如果需要,检查要保存到会话中的变量是否不为Empty(); That way you can be sure the session variable contains something, unless of course you want explicitly to be empty. 这样,您可以确保会话变量包含某些内容,除非您当然希望明确为空。

c) then load the session variable. c)然后加载会话变量。 You can temporary check it with a var_dump(); 您可以使用var_dump()临时检查它;

<?php
session_start();
if (!empty(trim($_GET['choose_cc'])))
{
    $_SESSION['choose_cc'] = $_GET['choose_cc'];
}
var_dump($_SESSION['choose_cc']);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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