【问题标题】:php session displaying previous session valuesphp会话显示以前的会话值
【发布时间】:2013-06-06 21:13:30
【问题描述】:

我正在从 php 调用控制台应用程序,它给了我一些输出。执行控制台应用程序后,我必须在 div 中显示输出。我已将控制台应用程序的输出作为 SESSION 传递到另一个页面以显示它。我也在我的网页中使用了 ajax 和 javascript,这使它变得更加复杂。现在,当我打印输出时,div 中的输出是 SESSION 的先前值。我的代码如下:

主页:

<!DOCTYPE html>
<html>
    <head>

                    $('#file').live('change', function()    
                    { 
                    $("#preview").html('');
                    $("#preview").html('<img src="images/loader.gif" alt="Uploading...."/>');
                    $("#imageform").ajaxForm(
                    {
                    target: '#preview'
                    }).submit();

                    <script type="text/javascript">
                    function myfunction()
                    {
                    $("#display").html('');
                    $("#display").html('<img src="images/loader.gif" alt="Uploading...."/>');
                    $("#retrieveimageform").ajaxForm(
                    {
                    target: '#display'
                    }).submit();

                     $('#tags').html('<?php include 'Tagsdisplay.php';?>');  
 %I want to include this only after it goes through the retrieveimage1.php page
 where the console application is called and the session is created but this is
 not happening the tags div is shown before the console application is called 
 and the other results are displayed on the display div
                     $('#tags').show();
                    };


                    </script>

        <title></title>
    </head>
    <body>
        <div id="maindiv">
                <div id="banner">
                <?php
                include 'Header.php';

                ?>
                 </div>
                <div id="wrapper">

                        <div id="imageupload">
                    <form action="uploadimage.php" method="post"
                         enctype="multipart/form-data" id="imageform">
                         <label for="file">Upload your image:</label>
                       <input type="file" name="file" id="file"><br>

                    </form>
                        </div>
                       <div id='preview'>  
                        </div>
                    <div id='tags'>

                    </div>

                       <div class="clear"></div>

                    <form action="retrieveimage1.php" method="post"
                           id="retrieveimageform">

                       ................................   

                         <input type="submit" name="search" value="Search" id="btnSearch" onclick="myfunction()">

                    </form>
     <div id="display"></div>

                 </div>

调用控制台应用并创建会话的retrieveimage.php:

          session_start();
          $start_time=  microtime(true);
        if (isset($_SESSION['img']))
        {   
                $image=$_SESSION['img'];
                $_SESSION['coarselbl1']=" ";
                $_SESSION['coarselbl2']=" ";
                $_SESSION['coarselbl3']=" ";
                $_SESSION['finelbl1']=" ";
                $_SESSION['finelbl2']=" ";
                $_SESSION['finelbl3']=" ";
                $_SESSION['finelbl4']=" ";
                $_SESSION['category']=" ";
                    if($_POST['cat']=='handbag')
                            {
                                 $_SESSION['category']="Bag";
                                 $cwt=$_POST["slider1"];
                                $fwt=$_POST["slider2"];
                                $twt=$_POST["slider3"];
                                $swt=$_POST["slider4"];

                                $addr="handbags31_fourth.exe $image $cwt $fwt $swt $twt";
                                  exec($addr,$data);

                                  $_SESSION['coarselbl1']=$data[2];
                                  $_SESSION['coarselbl2']=$data[3];
                                  $_SESSION['coarselbl3']=$data[4];
                   }

显示标签的TagsDisplay.php:

<?php
        session_start();
        echo $_SESSION['category']."<br/>";
   if($_SESSION['category']=="Bag")
    {
        echo "Coarse Color:   ".$_SESSION['coarselbl1'].",".$_SESSION['coarselbl2'].",".$_SESSION['coarselbl3']."<br/>";
        unset($_SESSION['coarselbl1']);
        unset($_SESSION['coarselbl2']);
        unset($_SESSION['coarselbl3']);


    }


?>

这里的问题是当我点击搜索按钮时,myfunction 被调用并且标签 div 显示在显示 div 之前,并且打印的值是上一个会话值。我希望标签 div 仅在显示 div 显示并分配新会话之后出现。我怎样才能做到这一点?

【问题讨论】:

  • 为什么需要取消设置每个会话?
  • 我取消了它,因为我虽然它可以解决问题,但它并没有因为它采用以前的会话值。

标签: php javascript session session-variables


【解决方案1】:

修复 jquery 行

$('#tags').html('<?php include 'Tagsdisplay.php';?>'); 

到这里:

$('#tags').html('<?php include "Tagsdisplay.php";?>'); 

(注意引号)因为您甚至没有包括取消设置前一个会话值的文件。

【讨论】:

  • 谢谢,但问题仍然没有解决,现在 div 是空白的,当我单击按钮时会显示 div,会话尚未设置。如何更改它以便仅在调用retrieveimage.php 后显示标签div?
  • 对不起,它并不总是空白,当我运行它 2 3 次时,我会显示上一个 SESSIOn 值
猜你喜欢
  • 1970-01-01
  • 2018-12-03
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 2011-06-21
  • 2012-08-08
  • 2020-08-27
  • 1970-01-01
相关资源
最近更新 更多