如何在页面刷新之间保持变量值?

时间:2015-03-16 10:47:37

标签: javascript php jquery mysql html5

在下面的代码中,我想要刷新页面后的$dischargeDate变量的值。 我的问题在于包含了这个代码排序代码,并且每个用户页面点击排序都会刷新,$dischargeDat的值变为空,它会影响输出。

<?php
include_once("session_check.php");
?>
<html>
<head>
  <title>Date Wise Report</title>
  <link rel="stylesheet" type="text/css" href="css/first.css" />
  <link rel="stylesheet" type="text/css" href="css/NewCss.css"  /> 
  <link href="font-awesome-4.2.0/font-awesome-4.2.0/css/font-awesome.css" rel="stylesheet">
  <link rel="stylesheet" href="css/jquery-ui.min.css" />
  <link rel="stylesheet" href="css/jquery-ui.theme.min.css" />
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <script type="text/javascript" src="js/jquery-ui.min.js"></script>
  <script type="text/javascript">
  $(document).ready(function() {
    $('#searchTerm_id').keyup(function() {
      if ($(this).val().length > 3)
        searchTable($(this).val());
      else if ($(this).val().length < 3)
        searchTable("");

    });
  });

  function searchTable(inputVal) {
    $.ajax({
      type: "GET",
      url: "report_admitdatewise.php",
      data: ({
        search_keyword: inputVal
      }),
      success: function(success) {
        window.location.href = "report_admitdatewise.php?search_keyword=" + inputVal;
      }
    });
  }

  /////datepicker code
  $(document).ready(function() {

    $('#reportdate_id').datepicker({
      dateFormat: 'dd/mm/yy',
      changeMonth: true,
      changeYear: true,

      yearRange: '1900:2060',
      ///maxDate: new Date()
    });
    $("#datawtable").hide();
    $("#show").click(function() {
      $("#datawtable").show();

    });

  });
  /*function formsubmit()
  {
    if(true)

     {
      document.getElementById('form_id').submit();
     }
  }*/

  </script>
</head>
<body >
  <div class="row-fluid">
    <div class="area-top clearfix">
      <div class="header">
        <h3 class="title"><i class="fa  fa-info-circle"></i>&nbsp;&nbsp;Date Wise Report</h3>
      </div>
    </div>
  </div>
  <div class="box">

    <div class="container">

      <form id="form_id" action="report_admitdatewise.php" method="GET" >
          <input type="text" id="reportdate_id" name="admitDate" placeholder="Select date"   >   
          <input type="submit" id="show"value="Display Records"  />

          <label id="search_lbl_id">Search:</label>
          <input id="searchTerm_id" Type="text"  class="search_box" placeholder="Search" 
                 value = <?php echo $_GET['search_keyword'] ?>>

      </form>
      <!------------datewise search code---------->

        <?php
        include("db.php");
        $sortingCode = "";
        if (isset($_REQUEST['sort_element']) && $_REQUEST['sort_element'] != "") {
          $sort_element = " ORDER BY ".$_REQUEST['sort_element']." ";
        }
        if (isset($_REQUEST['sort_type']) && $_REQUEST['sort_type'] != "") {
          $sort_type = " ".$_REQUEST['sort_type']." ";
        }
        $sortingCode = "$sort_element $sort_type";

        ?>

        <table class="draw_table" id="datawtable" border="1" width="100%" cellspacing="0" cellpadding="0">
          <th>
            <a href="report_admitdatewise.php?sort_element=admit_pat_id&sort_type=<?php echo 
              ($_REQUEST["sort_element"] == "admit_pat_id"  && $_REQUEST["sort_type"] == "asc") ? "desc" : "asc"; ?>">Patient ID
              <?php if ($_REQUEST["sort_element"] == "admit_pat_id" ) {  if($_REQUEST["sort_type"] == "desc" ) { ?>
              <img class="sorting" src="images2/downarrow.png" alt="asc">
              <?php } else { ?>
              <img class="sorting" src="images2/uparrow.png" alt="desc">
              <?php } } ?>
            </a>
            </th>  
          <th>
            <a href="report_admitdatewise.php?sort_element=admit_pat_name&sort_type=<?php echo 
              ($_REQUEST["sort_element"] == "admit_pat_name"  && $_REQUEST["sort_type"] == "asc") ? "desc" : "asc"; ?>">Patient Name
              <?php if ($_REQUEST["sort_element"] == "admit_pat_name" ) {  if($_REQUEST["sort_type"] == "desc" ) { ?>
              <img class="sorting" src="images2/downarrow.png" alt="asc">
              <?php } else { ?>
              <img class="sorting" src="images2/uparrow.png" alt="desc">
              <?php } } ?>
            </a>
          </th>
          <th >
            <a href="report_admitdatewise.php?sort_element=admit_pat_doctor&sort_type=<?php echo 
              ($_REQUEST["sort_element"] == "admit_pat_doctor"  && $_REQUEST["sort_type"] == "asc") ? "desc" : "asc"; ?>">Doctor Name
              <?php if ($_REQUEST["sort_element"] == "admit_pat_doctor" ) {  if($_REQUEST["sort_type"] == "desc" ) { ?>
              <img class="sorting" src="images2/downarrow.png" alt="asc">
              <?php } else { ?>
              <img class="sorting" src="images2/uparrow.png" alt="desc">
              <?php } } ?>
            </a>
          </th>
          <th>Department Name</th>
          <?php
            $where_clause =''; 
            $datewise=$_GET['admitDate'];
            $date1 = str_replace('/', '-', $datewise);
            //$dischargeDate= '';
            $dischargeDate= date('Y-m-d', strtotime($date1));
            if($_GET['admitDate']!='')
            {
              $where_clause = "where admit_pat_date='$dischargeDate'";
            }
            $searchKeyword =  $_GET['search_keyword'];

            if ($_GET['search_keyword'] != '')
            {  
              $where_clause = " where admit_pat_id like '%" . $searchKeyword . "%' or admit_pat_name like '%" .$searchKeyword  . "%' or admit_pat_doctor like '%" . $searchKeyword  . "%' or admit_pat_dept like '%" .$searchKeyword . "%'";  
            }

            if(isset($_GET['page']))
            {
              $page=$_GET['page'];
              $page=mysql_real_escape_string($page);
            } else {
              $page=1;
            }
            $per_page = 10;

            $page1=($page*10)-$per_page;
            $result=mysql_query("SELECT admit_pat_id,admit_pat_name,admit_pat_doctor,admit_pat_dept, admit_pat_date FROM admit_patient_details  $where_clause $sortingCode limit $page1,$per_page")or die(mysql_error());

            /*if(mysql_num_rows($result)==0)
            {
                echo "<script>alert('No Record found for this date')</script>";
                  //return false;
            }*/
            while($row=mysql_fetch_array($result))
            {

            ?>
           <tr>
              <td><?php echo $row['0'];?></td>
              <td><?php echo $row['1'];?></td>
              <td><?php echo $row['2'];?></td>
              <td><?php echo $row['3'];?></td>
           </tr>

           <?PHP  
             }
           ?>
           <tr>
             <td colspan="4" >
               <div class="link">
               <?php

                   $result =mysql_query("select admit_pat_id,admit_pat_name,admit_pat_doctor,admit_pat_dept, admit_pat_date from admit_patient_details  $where_clause $sortingCode ");
                   $count = mysql_num_rows($result);
                   $pages = ceil($count/$per_page); 

                   if($page != 1)
                   {

                     echo "<a href='report_admitdatewise.php?page=1&search_keyword=$searchKeyword'><img src='images2/First.png'></a>";
                     $prev = $page - 1; 

                     echo "<a href='report_admitdatewise.php?page=$prev&search_keyword=$searchKeyword'><img src='images2/Prev.png'></a>";
                   }

                   if($page != $pages)
                   {
                     $next = $page + 1; 
                     echo "<a href='report_admitdatewise.php?page=$next&search_keyword=$searchKeyword'><img src='images2/Next.png'></a>";

                     echo "<a href='report_admitdatewise.php?page=$pages&search_keyword=$searchKeyword'><img src='images2/Last.png'></a>";
                   }
               ?>
          </td>
        </tr>
      </table> 

  </div>
</body>
<?php mysql_close($con); ?>

1 个答案:

答案 0 :(得分:0)

如果您想将其存储在客户端上(如图所示),您可以使用web storage。如果您只需要在页面刷新期间保留变量,请使用会话存储。如果您需要保留它,即使用户关闭窗口并打开一个新窗口,也可以使用本地存储。

保存到本地存储:

localStorage.setItem("keyname", "string to store");

localStorage是支持网络存储的浏览器的全局预定义。)

从本地存储获取:

var value = localStorage.getItem("keyname");

请注意,网络存储存储字符串。要存储复杂数据,或者只是(大部分)保留数据类型,您可以使用JSON:

// Setting
localStorage.setItem("keyname", JSON.stringify(valueToStore));

// Getting
var value = JSON.parse(localStorage.getItem("keyname") || "null");

...如果值不存在(或者您存储null),那么获取后将会显示null