douxing6434 2014-05-08 02:12
浏览 43

php从表单设置会话变量

This is my main report page where I am sending session variables to another php page. I am running into issues when I try and post the form data to the session variables because then the query returns nothing. I have been searching and tinkering for a few days now but no results.

<!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>

This is my ServiceOReport.php page where I am taking the session variables and executing them in the query.

<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条回答 默认 最新

  • dqjcb132285 2014-05-08 02:14
    关注

    You must put session_start() at the top of the page before any output.

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

    and

    <?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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值