我不能在一个页面中显示两个会话消息

时间:2012-06-15 07:09:06

标签: php session

我一直在一个页面中显示两个会话消息,评分。每次刷新都会显示一条消息,显示喜欢和不喜欢的内容。提交评级表后显示的其他消息,问题是我提交评级表后,显示评级的消息未显示。为什么会这样?我不能在一个页面中使用两个会话?感谢

编码

<?php
if(!empty($_SESSION['error_msg'])){?>
    <div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>

    <?
    session_unset($_SESSION['error_msg']);
}
?>

if(!empty($_SESSION['rate_msg'])){
            echo $_SESSION['rate_msg'];
            session_unset($_SESSION['rate_msg']);
        }
  ?>
  <form name="rating_form" action="" method="post">
      <input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
      <input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
      <div style="text-align: center">
          <input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
      </div>
  </form>

5 个答案:

答案 0 :(得分:1)

抱歉,我认为我的代码有点令人困惑 我的实际代码看起来像这样

<div class="top_model">
                    <h1>Model Ratings</h1>
                        <?php
                           if(!empty($_SESSION['error_msg'])){?>
                                <div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>

                                <?
                                session_unset($_SESSION['error_msg']);
                            }
                        ?>
                      <ul class="homemodel_thumbs">
                          <div style="text-align: right">

                              </div> 

                          <?php
                            if(!empty($ratingArr)){
                                foreach($ratingArr as $ratings){

?>

                          <li><img src="<?php echo base_url();?>images/gallery/large/<?php echo $ratings['gallery_image'];?>" alt="<?php echo $ratings['gallery_image'];?>" class="center_image"/>
                              <p>&nbsp;&nbsp;</p>
                              <?php
                              if(!empty($_SESSION['rate_msg'])){
                                        echo $_SESSION['rate_msg'];
                                        //session_unset($_SESSION['rate_msg']);
                                    }
                              ?>
                              <form name="rating_form" action="" method="post">
                                  <input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
                                  <input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
                                  <div style="text-align: center">
                                      <input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
                                      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                      <input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
                                  </div>
                              </form>

                          </li>    

                             <?php    
                                }
                            }
                            ?>
                       </ul>

答案 1 :(得分:1)

这个问题解决了。我使用php会话的两个会话消息,而另一个我使用代码点火器会话。对不起,如果我没有提到我正在使用ci。无论如何,感谢大家的帮助:)

答案 2 :(得分:0)

显示后,您将取消评分消息:

session_unset($_SESSION['rate_msg']);

这意味着除非您再次重新填充表单,否则在您回发表单后它不可用。

删除它,我怀疑它会起作用。

答案 3 :(得分:0)

您有多余的?>

<?php
     if(!empty($_SESSION['error_msg'])){?>
        <div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
<?
     session_unset($_SESSION['error_msg']);
     }

    if(!empty($_SESSION['rate_msg']))
    {
        echo $_SESSION['rate_msg'];
        session_unset($_SESSION['rate_msg']);
    }
?>

答案 4 :(得分:0)

你没有为最后一个语句打开php标签,(或者不需要关闭它)它被视为html

<?php
if(!empty($_SESSION['error_msg'])){?>
    <div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>

    <?php
    session_unset($_SESSION['error_msg']);
}

<击>&GT;?

if(!empty($_SESSION['rate_msg'])){
            echo $_SESSION['rate_msg'];
            session_unset($_SESSION['rate_msg']);
        }
  ?>

修改

<?php
// < HERE IS PHP >
if(!empty($_SESSION['error_msg'])){?>
    <div style="text-align: center"><?php echo $_SESSION['error_msg'];?></div>
    <!-- HERE IS HTML -->
    <?
    // < HERE IS PHP >
    session_unset($_SESSION['error_msg']);
}
?> <!-- CLOSED PHP - NOW IT'S HTML - YOU SHOULD REMOVE IT -->

<!-- THIS IS NOT EXECUTED - BECAUSE IT'S HTML NOW -->
if(!empty($_SESSION['rate_msg'])){
            echo $_SESSION['rate_msg'];
            session_unset($_SESSION['rate_msg']);
        }
  ?> <!-- THIS IS NOT PHP TAG BECASUE THERE IS NOT OPEN TAG -->
  <form name="rating_form" action="" method="post">
      <input type="hidden" name="g_id" value="<?php echo $ratings['gallery_id'];?>"/>
      <input type="hidden" name="g_image" value="<?php echo $ratings['gallery_image'];?>"/>
      <div style="text-align: center">
          <input type="radio" name="rating" value="Hot" onclick="document.rating_form.submit();">Hot</input>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          <input type="radio" name="rating" value="Not" onclick="document.rating_form.submit();">Not</input>
      </div>
  </form>