我有冗长的搜索表单,我正在尝试将表单值保留在会话中。我使用codeigniter分页,所以我需要将在搜索表单中输入的值存储在会话中(我试图避免将它们存储在数据库中)
这个问题困扰了我几天,我无法解决它。这是代码。为了澄清,我为我的代码的每一行提供了注释:
// my submit button named search_button has been clicked
if ($this->input->post('search_button')) {
// all the values from the form are stored in array
$formValues = $this->input->post(NULL, TRUE);
// i assign the values of $formValues array to session
$this->session->set_userdata('formValues', $formValues);
// i store the values in local variable
$mySessData = $this->session->userdata('formValues');
// i count the values of the $formValues to be sure how many fields i get back
// reason for that is that my form is built dynamically
// returns 19 if the form has been submitted.
// when i submit the form, i get 19
echo "Count of form values is now: " . count($formValues);
// i print the $formValues array, to be sure that it return the form values
// and yes, it does if the form has been submitted.
// see the $formValues returns section bellow to see what is returned
echo "<pre>";
print_r($formValues);
echo "</pre>";
// i print my local variable to ensure that variable $mySessData is not empty
// see the section $mySessData retruns (1) to see returned values
echo "Confirm that sess data has been set up";
echo "<pre>";
print_r($mySessData);
echo "</pre>";
} else {
// else, means that form has not been submitted but that controller has been called from the next link
// in the pagination links. This always return empty array, that is nothing...
echo "<pre>";
print_r($mySessData);
echo "</pre>";
}
如果表单已提交,$ formValues将返回以下内容:
Array
(
[addtypeid] =>
[isnew] =>
[orderby] =>
[geographicareaid] =>
[catid] => 1
[catid2] =>
[manufacturerid] =>
[modelid] =>
[yearofmanufacturing_from] =>
[yearofmanufacturing_to] =>
[hoursused_from] =>
[hoursused_to] =>
[horsepowers_from] =>
[horsepowers_to] =>
[price_from] =>
[price_to] =>
[colorid] =>
[isdamaged] =>
[search_button] => Submit‚
)
我的变量$ mySessData返回表单已提交的相同数据:
Array
(
[addtypeid] =>
[isnew] =>
[orderby] =>
[geographicareaid] =>
[catid] => 1
[catid2] =>
[manufacturerid] =>
[modelid] =>
[yearofmanufacturing_from] =>
[yearofmanufacturing_to] =>
[hoursused_from] =>
[hoursused_to] =>
[horsepowers_from] =>
[horsepowers_to] =>
[price_from] =>
[price_to] =>
[colorid] =>
[isdamaged] =>
[search_button] => Submit‚
)
最后,如果我点击分页链接
$config['base_url'] = site_url('/searchresults/display/'.$limit.'/');
,指向相同的控制器和相同的方法,很好......我得到一个空数组。
任何帮助将不胜感激。
问候,约翰
答案 0 :(得分:0)
我认为如果在表单中使用method =“get”会更好。在搜索表单的情况下,使用get而不是post发布表单更为可取,因为您不需要维护任何会话,也不需要在数据库中存储值。你可以随时使用
轻松访问一个游戏 $this ->input->get('fild_name');
您可以通过这种方式优化代码,而不需要维护会话变量,因为您的网址中始终会有搜索结果。使用会话另一个问题是取消设置会话变量。
我个人认为创建一个会话变量只是为了保持分页不是一个好主意。而且在搜索表单中不需要安全性,所以你可以使用“get”insted“post”发送你的表单