-3

您好,我对每页开头的以下代码有疑问:

session_start();

if (!isset($_SESSION['a'])){
    ...create the session from some variables...
$_SESSION['a'] = $var;
$var = $_SESSION['a'];
}else{
    $var = $_SESSION['a'];
    $what = "some text";

    if (isset($_POST['one']) ){
        $var = "new input";
            $_SESSION['a'] = $var;
        echo "one was posted";
    }
    if (isset($_POST['two']) ){
            $var = "new input";
            $_SESSION['a'] = $var;
        echo "two was posted";
    }   
}

要发布的表格放在页脚中:

<form action="'.$_SERVER['PHP_SELF'].'" method="post">      
    <input type="submit" id="one" name="one"/><div>set one</div>
</form>
<form action="'.$_SERVER['PHP_SELF'].'" method="post">  
    <input type="submit" id="two" name="two"/><div>set two</div>
</form>

上面代码背后的逻辑似乎有问题。调用页面并发送帖子问题时,它不会回显并且不会将会话更改为新值。

所以如果有人可以帮助我,我真的很感激。

多谢。

更新:

好的,代码完全可以工作到“其他”部分:

else{
    $var = $_SESSION['a'];

    if (isset($_POST["one"]) ){
        echo "post one was send";
    }
    if (isset($_POST["two"]) ){
        echo "post en was send";
    }

echo "session already exists.";
}

这部分只显示回声:会话已存在。整个后置短语不起作用。根据某些变量,页脚将被嵌入。因此 html 中的全部内容将被回显:

echo '...
      <ul class="drop_down">
    <form action="'.$_SERVER['PHP_SELF'].'" method="post">      
        <input type="submit" id="one" name="one value="one"/><div>set one</div>
    </form>
    <form action="'.$_SERVER['PHP_SELF'].'" method="post">  
        <input type="submit" id="two" name="two" value="two"><div>set two</div>
    </form>
      </ul>
     ...';
4

3 回答 3

0

改变你的形式

   <form action="<?=$_SERVER['PHP_SELF']?>" method="post">      
   <input type="submit" id="one" name="one" value="one"/><div>set one</div>
   </form>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">  
   <input type="submit" id="two" name="two" value="two"/><div>set two</div>
</form>

值需要发送..


编辑#1:


在我的系统中,这工作正常..

        <?php
        session_start();

        if (!isset($_SESSION['a'])){
           $var='hi';
        $_SESSION['a'] = $var;
        $var = $_SESSION['a'];
        }else{
            $var = $_SESSION['a'];
            $what = "some text";

            if (isset($_POST['one']) ){
                $var = "new input";
                    $_SESSION['a'] = $var;
                echo "one was posted";
            }
            if (isset($_POST['two']) ){
                    $var = "new input";
                    $_SESSION['a'] = $var;
                echo "two was posted";
            }   
        }
        ?>
        <form action="<?=$_SERVER['PHP_SELF']?>" method="post">      
            <input type="submit" id="one" name="one"/><div>set one</div>
        </form>
        <form action="<?=$_SERVER['PHP_SELF']?>" method="post">  
            <input type="submit" id="two" name="two"/><div>set two</div>
        </form>

编辑#2


再试一次...

        if (!isset($_SESSION['a'])){
           $var='hi';
        $_SESSION['a'] = $var;
        $var = $_SESSION['a'];
        }else{
            $var = $_SESSION['a'];
            $what = "some text";

            if (isset($_POST['one']) ){
                $var = "new input1";
                    $_SESSION['a'] = $var;
                echo "one was posted";
                print_r($_SESSION);
            }
            if (isset($_POST['two']) ){
                    $var = "new input2";
                    $_SESSION['a'] = $var;
                echo "two was posted";
                print_r($_SESSION);
            }   
        }
        ?>
        <form action="<?=$_SERVER['PHP_SELF']?>" method="post">      
            <input type="submit" id="one" name="one"/><div>set one</div>
        </form>
        <form action="<?=$_SERVER['PHP_SELF']?>" method="post">  
            <input type="submit" id="two" name="two"/><div>set two</div>
        </form>

session是正确打印

于 2012-12-21T13:05:27.737 回答
0

这部分需要澄清。你到底想做什么?

if (!isset($_SESSION['a'])){
...create the session from some variables...
$_SESSION['a'] = $var;
$var = $_SESSION['a'];
}else{
$var = $_SESSION['a'];
$what = "some text";

试试这个:

    <?PHP
session_start();
if (isset($_POST['one']) )
    {
      $_SESSION['a'] = "langauage one";
      echo "one was posted";
    }
    elseif (isset($_POST['two']) )
        {
            $_SESSION['a'] = "langauge two";
            echo "two was posted";
        }   
        else
            {
                if (!isset($_SESSION['a']))
                    {
                        $_SESSION['a'] = "default langauge";
                    }

            }

这是 SELECT 的示例

    <?PHP
    session_start();
    if (isset($_POST['language']) )
    {
      $_SESSION['a'] = $_POST['language'];
      echo "Language has ben set to ".$_POST['language'];
    }
    else
        {
            if (!isset($_SESSION['a']))
                {
                    $_SESSION['a'] = "default langauge";
                }

        }
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<select title="Please choose your Language" name="language">    
     <option value="langauage one">langauage one</option>
     <option value="langauge two">langauge two</option>
</select>
</form>
于 2012-12-21T13:20:21.127 回答
0

形式:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">      
    <input type="submit" id="one" name="one" value='one'/><div>set one</div>
</form>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">  
    <input type="submit" id="two" name="two"value='two' /><div>set two</div>
</form>

php:

<?php

    session_start();

    if (!isset($_SESSION['a'])){
        $var = 1;
        $_SESSION['a'] = $var;
        //$var = $_SESSION['a']; this is unnecessary 
    }else{
        $var = $_SESSION['a'];
        $what = "some text";

        if (isset($_POST['one']) ){
            $var = "one";
            $_SESSION['a'] = $var;
            echo "one was posted";
        }
        if (isset($_POST['two']) ){
            $var = "two";
            $_SESSION['a'] = $var;
            echo "two was posted";
        }   
    }

?>
于 2012-12-21T13:34:42.500 回答