gpt4 book ai didi

javascript - 如何在页面刷新之间保留变量的值?

转载 作者:行者123 更新时间:2023-11-29 22:46:35 24 4
gpt4 key购买 nike

在下面的代码中,我想要刷新页面后的 $dischargeDate 变量的值。我的问题是在此代码中包含排序代码,并且对于每个列,单击按用户页面排序的按钮都会刷新,并且 $dischargeDat 的值变为 null 并且会影响输出。

<?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); ?>

最佳答案

如果您想将其存储在客户端上(看起来就是这样),您可以使用 web storage为了那个原因。如果您只需要在页面刷新期间保留变量,请使用 session 存储。如果即使用户关闭窗口并打开新窗口也需要保留它,请使用本地存储。

保存到本地存储:

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),则 get 会给您 null

关于javascript - 如何在页面刷新之间保留变量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29074936/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com