I was wondering why my code, which can be trimmed down to the following structure, isn't printing what I think should be printed. Basically I have two methods and two corresponding html pages in the same php controller class. I want to retrieve the value of the POST request (html text input form) from mypage.html and print that in secondpage.html to which we are redirected when the user clicks 'submit' button after typing in their input in the text box.
我想知道为什么我的代码,可以修剪到以下结构,不打印我认为应该打印的。基本上我在同一个php控制器类中有两个方法和两个相应的html页面。我想从mypage.html检索POST请求(html文本输入表单)的值,并在用户在文本框中输入输入后单击“提交”按钮时将其重定向到secondpage.html。
Update: I just read about PHP sessions and my program does start sessions. But should I use sessions even if the user input date is saved in the database?
更新:我刚刚阅读了PHP会话,我的程序确实启动了会话。但即使用户输入日期保存在数据库中,我是否应该使用会话?
<? php
$var; // initializing global variable;
class MyClass {
public function mypage () {
global $var;
$var = $_POST['form_name'];
}
public function secondpage () {
global $var;
print_r($var);
}
}
2 个解决方案
#1
2
The simplest would be to store the value in the session, like
最简单的方法是将值存储在会话中,例如
<? php
class MyClass {
public function mypage () {
$_SESSION['var'] = $_POST['form_name'];
}
public function secondpage () {
print_r($_SESSION['var']);
}
}
This expects the session to be started by calling session_start()
somewhere else, but you said the session is started.
这需要通过在其他地方调用session_start()来启动会话,但是您说会话已启动。
But having the data saved in database, why don't you read it back in the secondpage()?
但是将数据保存在数据库中,为什么不在第二页()中读回?
#2
0
Using a global
is awkward in a class. It feels wrong. Maybe try a static
variable instead:
使用全局在课堂上很尴尬。感觉不对劲。也许尝试一个静态变量:
<?php
class MyClass
{
public static $var;
public function mypage()
{
self::$var = 'test';
}
public function secondpage()
{
print_r(self::$var);
}
}
$test = new MyClass();
$test->mypage();
// This will print test
$test->secondpage();
// This will also print test
echo MyClass::$var;
?>
If you are literally trying to save from one page to a whole new page, you do need a session.
如果你真的试图从一个页面保存到一个新的页面,你需要一个会话。
#1
2
The simplest would be to store the value in the session, like
最简单的方法是将值存储在会话中,例如
<? php
class MyClass {
public function mypage () {
$_SESSION['var'] = $_POST['form_name'];
}
public function secondpage () {
print_r($_SESSION['var']);
}
}
This expects the session to be started by calling session_start()
somewhere else, but you said the session is started.
这需要通过在其他地方调用session_start()来启动会话,但是您说会话已启动。
But having the data saved in database, why don't you read it back in the secondpage()?
但是将数据保存在数据库中,为什么不在第二页()中读回?
#2
0
Using a global
is awkward in a class. It feels wrong. Maybe try a static
variable instead:
使用全局在课堂上很尴尬。感觉不对劲。也许尝试一个静态变量:
<?php
class MyClass
{
public static $var;
public function mypage()
{
self::$var = 'test';
}
public function secondpage()
{
print_r(self::$var);
}
}
$test = new MyClass();
$test->mypage();
// This will print test
$test->secondpage();
// This will also print test
echo MyClass::$var;
?>
If you are literally trying to save from one page to a whole new page, you do need a session.
如果你真的试图从一个页面保存到一个新的页面,你需要一个会话。