Passing PHP Session Value

匆匆过客 提交于 2019-12-11 15:01:15

问题


I'm attempting to pass a value from one WordPress page to another through a PHP Session. I have a dropdown list on both pages, which are identical except for the option values. I would like the selected option on the 2nd page at page load to reflect the chosen value submitted on the first page.

I've looked into a lot of questions but haven't been able to find exactly what I need. Any help would be greatly appreciated!

Page 1:

<?php
    if(!isset($_SESSION)) { 
        session_start(); 
        echo $_SESSION['state'];
        $var_value = $_SESSION['state'];
    } 
?>

<form>
    <input type=hidden name="retURL" value="page2.php">
    <select id="state" name="state">
        <option>State 1</option>
        <option>State 2</option>
        <option>State 3</option>
    </select>
    <input type="submit" name="submit" value="Submit">
</form>

Page 2:

<?php
    session_start();
    if(isset($_POST['state'])){
        $_SESSION['state'] = $_POST['state'];
    }
?>

<select>
    <option <?php if($var_value == 'State 1') echo 'selected'; ?> value="http://customizedlink">State 1</option>
    <option <?php if($var_value == 'State 2') echo 'selected'; ?> value="http://customizedlink">State 2</option>
    <option <?php if($var_value == 'State 3') echo 'selected'; ?> value="http://customizedlink">State 3</option>
</select>

回答1:


WordPress core does not use PHP sessions. The WordPress platform is totally stateless and provides no support for the use of sessions outside of the cookie that keeps a user logged in.

WordPress core does not use PHP sessions, but sometimes they are required by your use-case, a plugin or theme.

You can use WordPress Native PHP Sessions plugin that implements PHP’s native session handlers, backed by the WordPress database.

see Using the PHP Session in WordPress




回答2:


You've forgotten to tell us how you're getting from Page 1 to Page 2. But, let's assume that when you SELECT in Page 1, Page 1 is called again. In that case, you need to move your $_SESSION['state'] = $_POST['state']; assignment from Page 2 to Page 1. It must occur before you move to Page 2.

Also, Page 2 is using something called $var_value which isn't set anywhere in your examples. Did you mean to execute $var_value = $_SESSION['state']; somewhere above the SELECT in Page 2?

If none of this leads you to an answer, please provide more information about the context of your code.


Try this. Your Page 1 FORM element, when submitted, calls Page 1. Therefore...

<?php
    if(!isset($_SESSION)) { 
        session_start(); 
        if( isset($_POST['state']) ){
            $_SESSION['state'] = $_POST['state'];
        }
        echo "Page 1: ".$_SESSION['state'];
    } 
?>

<form method="post">
    <input type=hidden name="retURL" value="page2.php">
    <select id="state" name="state">
        <option>State 1</option>
        <option>State 2</option>
        <option>State 3</option>
    </select>
    <input type="submit" name="submit" value="Submit">
</form>

Page 2

<?php
    session_start();
    echo "Page 2: ".$_SESSION['state'];
    $var_value = $_SESSION['state'];
?>

<select>
    <option <?php if($var_value == 'State 1') echo 'selected'; ?> value="http://customizedlink">State 1</option>
    <option <?php if($var_value == 'State 2') echo 'selected'; ?> value="http://customizedlink">State 2</option>
    <option <?php if($var_value == 'State 3') echo 'selected'; ?> value="http://customizedlink">State 3</option>
</select>

You should see the correct value for $_SESSION['state'] in Page 2 (after running Page 1) IF I have understood the intent of your code correctly.



来源:https://stackoverflow.com/questions/45680649/passing-php-session-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!