php - 如何在 php 中记住 GET 和 POST 值?
问题描述
请检查以下代码:
在 filter.php 中
<?php
//Getting the cat_id value 25 from category.php
$cat_id = ((isset($_GET['cat']))?sanitize($_GET['cat']):'');
echo 'Cart id filter page is:'.$cat_id; //25
?>
<form action="FrontPageSearchFilter.php" method="post">
<input type="hidden" name="cat" value="<?=$cat_id;?>">
<input type="submit" value="search" class="btn btn-xs btn-primary">
</form>
在 search.php 中
$cat_id = (($_POST['cat'] != '')?sanitize($_POST['cat']):'');
echo 'Cart id search page is:'.$cat_id; // 25
我的问题是,当我单击类别时,它在 filter.php 中给出值 25
但是当我单击提交按钮时,它会忘记该值并将正确的值 cat_id 传递给 search.php 即:25 但在 filter.php 中,提交按钮后 cat_id 值变为零。
在这里,即使在按下提交按钮之后,我也想记住该值,直到类别没有改变。
请告诉我我做错了什么。谢谢你。
解决方案
localStorage 您还可以使用更现代的技术,例如 localStorage。
//First Grab the value of the form before it submits
var yourObject = $('#your-form').serializeArray();
//Stringify your object since that is how localstorage will save the value
localStorage['variablename'] = JSON.stringify(yourObject)
//Then retrieve that information which is a string and parse it to JS object so you could put those values in the default form.
JSON.parse(localStorage['yourObject']);
每次用户提交他们输入的那些先前的值时,您都不必弄乱您的任何 PHP 提交表单。
推荐阅读
- c# - User.FindFirst(ClaimTypes.NameIdentifier) - 来自 Angular 的 NullReferenceException 但在 Postman 中可以吗?
- c# - UseSystemPasswordChar 的奇怪行为
- javascript - 使用ajax动态加载页面
- android - React Native 错误:RNHTMLtoPDF 错误:无法创建文件夹结构
- r - 将日期添加到列表中的每个元素
- javascript - 使用 Typescript 输入 Redux Store
- arrays - 执行 pgsql 时出现问题,抱怨循环
- angular - Angular 6 或 10 部分动态 html 绑定
- firebase - 使用 Firebase / Expo 查询到 Cloud Firestore
- mysql - MySQL 触发器语法,用于根据用户 ID 进行排序