首页 > 解决方案 > 如何在 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 值变为零。

在这里,即使在按下提交按钮之后,我也想记住该值,直到类别没有改变。

请告诉我我做错了什么。谢谢你。

标签: phplaravel

解决方案


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 提交表单。


推荐阅读