简体   繁体   English

单击按钮设置PHP $ _SESSION变量

[英]Setting PHP $_SESSION variable on button click

I'm trying to set a PHP $_SESSION variable when a user clicks a record in a modal table. 当用户单击模式表中的记录时,我试图设置PHP $ _SESSION变量。

The modal table is set up by doing a while loop through records in a SQL Server table. 通过对SQL Server表中的记录进行while循环来设置模式表。 It appears like follows: 如下所示:

    Value   |  Link
---------------------------
    0       |   <a href= changeVar.php?ficheNum=0></a>
    1       |   <a href= changeVar?ficheNum=1></a>
    2       |   <a href= changeVar?ficheNum=2></a>
    ...

When one of the links in the table is clicked it runs this code: 单击表中的链接之一后,它将运行以下代码:

<?php
$ficheNum=$_REQUEST['ficheNum'];
$_SESSION['ficheCount'] = $ficheNum;

header("Location: test.php"); 
?>

The test.php page is simply a page of inputs that are populated with values from a database table. test.php页面只是一个输入页面,其中填充了数据库表中的值。 It also has the modal table that was shown above. 它还具有上面显示的模式表。 The PHP on test.php is as follows. test.php上的PHP如下。

$fiche_array = array();

$sel_query = "SELECT fiche
              FROM test.dbo.[TLM.Fiche_Globale]
              ORDER BY fiche ASC;";
$sel_result = sqlsrv_query($con, $sel_query) or die( print_r( sqlsrv_errors(), true));
while($sel_row = sqlsrv_fetch_array($sel_result)) {
    array_push($fiche_array, $sel_row['fiche']);
}

if(isset($_POST['new']) && $_POST['new']==1)
{
    $_SESSION['ficheCount'] ++;
}else if(isset($_POST['new2']) && $_POST['new2']==1)
{
    $_SESSION['ficheCount'] = $_SESSION['ficheCount'] - 1;
}else if(isset($_SESSION['ficheCount']) && $_SESSION['ficheCount'] > 0)
{
    $_SESSION['ficheCount'] = $_SESSION['ficheCount'];
}else{
    session_start();
    $_SESSION['ficheCount'] = 0;
}

The first two statements in the if/else statement are to operate next and previous record buttons. if / else语句中的前两个语句用于操作下一个和上一个记录按钮。 They work fine. 他们工作正常。 The final statement is the code that runs if none of statements 1-3 are triggered. 最后的语句是如果没有触发语句1-3则运行的代码。 The third statement is supposed to trigger if the user is being redirected back to the page from from the changeVar.php page. 如果从changeVar.php页面将用户重定向回页面,则应该触发第三条语句。 However, this never triggers. 但是,这永远不会触发。 Or, it triggers and doesn't do anything -- I'm not sure. 或者,它触发并且不执行任何操作-我不确定。

I've tried manually testing the changeVar page: 我试过手动测试changeVar页面:

<?php
$ficheNum=$_REQUEST['ficheNum'];
$_SESSION['ficheCount'] = $ficheNum;

echo $_SESSION['ficheCount'];
?>

It sets the variable to the correct number but, when I return to the test.php page it always defaults back to the ficheCount that was set before as if I never set the variable in the changeVar.php page. 它将变量设置为正确的数字,但是,当我返回到test.php页面时,它总是默认返回到之前设置的ficheCount,就好像我从未在changeVar.php页面中设置变量一样。

If anyone can see what I'm doing wrong here it would be greatly appreciated. 如果有人在这里看到我在做什么错,将不胜感激。

cheers, 干杯,

In order to use PHP sessions, you always need to run the command session_start(); 为了使用PHP会话,您总是需要运行命令session_start(); at the start of each PHP file that uses sessions. 在每个使用会话的PHP文件的开头。 http://php.net/manual/en/function.session-start.php http://php.net/manual/en/function.session-start.php

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

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