0

我正在使用 PHP 进行分页,并且有下一页和上一页提交按钮,因此如果用户在第 1 页检查 2 个值,我将其存储在会话变量中,当他返回时,我使用in_array并回显该值。但是如果用户取消选中,我如何取消设置数组中的值?如果用户未选中该复选框,则无法取消设置会话数组中的值。

我想我的解释很清楚,但如果有些人需要代码而不是这里

if(!empty($_POST['rec_num'])) {
        if(empty($_SESSION['checks_selected_for_records'])) {
               //If session array is empty than directly add all record numbers in
               //this session
           $_SESSION['checks_selected_for_records'] = $_POST['rec_num'];
       }
        //If a new value is seen and is not in array than add it            
       foreach ($_POST['rec_num'] as $check_rec_num) {
        if(!in_array($check_rec_num, $_SESSION['checks_selected_for_records'])) {
           array_push($_SESSION['checks_selected_for_records'], $check_rec_num);
       }
   }
} 

//Handle Redirects For Pagination
if (isset($_POST['next_page']) || isset($_POST['last_page']) || isset($_POST['first_page']) || isset($_POST['previous_page'])) {
        if(isset($_POST['next_page_link'])) {
            redirect_to($_POST['next_page_link']);
        } elseif (isset($_POST['last_page'])) {
            redirect_to($_POST['last_page_link']);
        } elseif (isset($_POST['first_page'])) {
            redirect_to($_POST['first_page_link']);
        } elseif (isset($_POST['previous_page'])) {
            redirect_to($_POST['previous_page_link']);
        }
    }
4

2 回答 2

1

使用 array_search 从数组中删除一个值

if(($key = array_search($del_val,  $_SESSION['checks_selected_for_records'])) !== false) {
   unset($_SESSION['checks_selected_for_records'][$key]);
 }
于 2013-02-13T15:59:53.520 回答
0

就像是:

<form action="" method="POST">
  <input type="checkbox" name="mycheckbox">
</form>

<?php
  $_SESSION['mycheckbox'] = isset($_POST['mycheckbox']);
// OR
  if( ! isset($_POST['mycheckbox']) ) {
    unset($_SESSION['mycheckbox']);
  } else {
    $_SESSION['mycheckbox'] = $_POST['mycheckbox'];
  }
于 2013-02-13T16:01:08.077 回答