php从表单设置会话变量

时间:2014-05-08 02:12:43

标签: php session session-variables

这是我的主要报告页面,我将会话变量发送到另一个php页面。当我尝试将表单数据发布到会话变量时,我遇到了问题,因为这样查询什么都不返回。我一直在寻找和修补几天但没有结果。

<!DOCTYPE html>
    <?php
      session_start();
     /* this works when the session variables are static*/
     $_SESSION["date"] = '2012-05-01'; 
     $_SESSION["ServiceOrders"] ='Network Error';  
     /* when I try and post the form values the query returns nothing
      $_SESSION["date"] = $_POST['sdate']; 
      $_SESSION["ServiceOrders"] =   $_POST['ServiceOrders']; 
     */

    ?>
    <html>

  <head>   
      <link rel='stylesheet' type='text/css' href='styles.css' />
      <link rel="stylesheet" href="grid.css" type="text/css" />
      <link rel="stylesheet" href="backgroundcss.css" type="text/css" />
<style>
form { width: 700px; }
label { float: left; width: 100px; }
input[type=text] { float: left; width: 200px; }
.clear { clear: both; height: 0; line-height: 0; }
.floatright { float: right; }

</style>

    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
    <script type='text/javascript' src='menu_jquery.js'></script>
    <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>    

<script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });

 $(function() {
     $("#refresh").on("click", function() {
        $("#mydiv").load("http://***.***.***.***/ServiceOReport.php");
        return false;
    })
  })

</script>

  </script>
  </head>

  <body  class="container">
  <br>
<div id='menu'>
<ul>
   <li class='active'><a href='LineChart.php'><span>Home</span></a></li>
   <li class='has-sub'><a href='#'><span>Contacts</span></a>
      <ul>
         <li><a href='allrows.php'><span>Inbound Calls Codes</span></a></li>
         <li class='last'><a href='#'><span>Contact Free Form</span></a></li>
      </ul>
   </li>
   <li class='has-sub'><a href='#'><span>Service Orders</span></a>
      <ul>
         <li><a href='#'><span>Company Service Orders</span></a></li>
         <li class='last'><a href='#'><span>Vendor Service Orders</span></a></li>
      </ul>
   </li>
   <li class='last'><a href='#'><span>Ad Hoc Report Builder</span></a></li>
</ul>

</div>
<br>
<form action="ServiceOReport.php" method="post">
<select name="ServiceOrders">
  <option value="Network Error">Network Error</option>
  <option value="Reverse Rotation Detected">Computer Failure</option>
  <option value="Cover Off">Misc</option>
  <input type="text" name="sdate"id="datepicker" >
  <input type="submit" id ="refresh" value=" Submit "/><br />
</select>
</form>

<div id="mydiv" class="grid_12 omega scroll">

</div>  

<div class="grid_12 ">  
    <div   id="TopBorder"  class="grid_12">
        <footer class="mainFooter">
            <p>Copyright &copy; 2014 <a href="http://test.com">Test</a></p>
        </footer>
    </div>
</div>  

  </body>
</html>

这是我的ServiceOReport.php页面,我在这里获取会话变量并在查询中执行它们。

<html>
  <head>   
      <link rel='stylesheet' type='text/css' href='styles.css' />
      <link rel="stylesheet" href="grid.css" type="text/css" />
      <link rel="stylesheet" href="backgroundcss.css" type="text/css" />
 </head>      

  <body  class="container">
        <?php
 session_start();

        // Make a MySQL Connection
    mysql_connect("localhost", "****", "********") or die(mysql_error());
    mysql_select_db("Test_DB") or die(mysql_error());

    $ServiceOrders= $_SESSION["ServiceOrders"];
    $string=$_SESSION["date"];
    $timestamp = strtotime($string);
    $correctedDate = date("Y-m-d H:i:s", $timestamp);

    $result = mysql_query("Select EquipmentID,StreetNumber,StreetName,City,ZipCode,ZipPlusFour,CreatedDate,ServiceOrderdesc 
                            From CSS_ServiceOrders  
                            Where ServiceOrderdesc = '$ServiceOrders' and CreatedDate > '$correctedDate'
                            order by CreatedDate,StreetName,StreetNumber;") 
    or die(mysql_error());  

    echo '<table class="bordered">
<thead>
    <tr>
        <th scope="col">EquipmentID</th>
        <th scope="col">ZipPlusFour</th>        
        <th scope="col">StreetNumber</th>
        <th scope="col">StreetName</th>
        <th scope="col">City</th>       
        <th scope="col">ZipCode</th>                    
        <th scope="col">CreatedDate</th>
        <th scope="col">ServiceOrderdesc</th>   
    </tr>
</thead>
<tbody>';


    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {

        echo "<tr><td>"; 
        echo $row['EquipmentID'];
        echo "</td><td>"; 
        echo $row['ZipPlusFour'];
        echo "</td><td>";       
        echo $row['StreetNumber'];
        echo "</td><td>";       
        echo $row['StreetName'];
        echo "</td><td>";       
        echo $row['City'];
        echo "</td><td>";       
        echo $row['ZipCode'];
        echo "</td><td>";       
        echo $row['CreatedDate'];
        echo "</td><td>"; 
        echo $row['ServiceOrderdesc'];
        echo "</td></tr>"; 

    } 

    echo "</tbody></table>";
?>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

必须在任何输出之前将session_start()放在页面顶部。

<?php
    session_start();
?>
<!DOCTYPE html>
<?php    

<?php
    session_start();
?>
<html>
  <head>   
      <link rel='stylesheet' type='text/css' href='styles.css' />
      <link rel="stylesheet" href="grid.css" type="text/css" />
      <link rel="stylesheet" href="backgroundcss.css" type="text/css" />
 </head>      

  <body  class="container">
        <?php

答案 1 :(得分:0)

正如@John所说,你需要将session_start()放在文件的开头,主要原因是解释器看到任何非PHP代码或echo后,就会发出标题。否则它不能发送任何东西,但一旦发送标题,它们就不能重新发送。

PHP会话严重依赖于标头,它必须设置并通过cookie详细信息发送页面,以便它知道它必须查找哪个会话。

确保在php标记之前没有空格或空行,并且在session_start()命令之前没有回声。这些很难调试,容易犯错,所以我总是向人们提起它们。