在会话中存储复选框值,然后重新选择返回的复选框以添加更多 [英] Storing checkbox values in sessions and then re-selecting check boxes checked when they came back to add some more

查看:90
本文介绍了在会话中存储复选框值,然后重新选择返回的复选框以添加更多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户通过单击添加更多书籍链接返回,则我试图通过会话变量传递复选框值。我想向他们显示以前选中的复选框。我在chklist.php页面中尝试过

I am trying to pass my checkbox values through a session variable if the user goes back by clicking add more books link. I want to show them previously selected check boxes are checked. I tried in chklist.php page

if(isset($_SESSION['pro'])){ 
echo  $_SESSION['pro'];} 

显示类似于1_2_5的值,这些值存在于会话数组中。
这是我的复选框的html代码。我大约有24个与product []同名的复选框。

shows values like 1_2_5 which are present in session array. Here is my html code of checkboxes. I have about 24 checkboxes with the same name as product[].

<name="product[]"  type="checkbox" value="1" alt="1607.00" />
<name="product[]"  type="checkbox" value="2" alt="1607.00" />

等等
在这里,我在设置复选框POST后设置会话。

and so on Here I am setting my session after POST of checkboxes .

$_SESSION['pro'] = implode('_', $_POST['product']); 


如何通过单击chkout.php中存在的ADD MORE BOOKS链接返回到firstpage(chklist.php)来选中先前选中的复选框,任何机构都可以编写我需要添加到我的html中的代码。

in nextpage chkout.php. How to make previously selected checkboxes are checked when user came back to firstpage(chklist.php) by clicking ADD MORE BOOKS link present in chkout.php, can any body write the code what i need add to my html.

推荐答案

您可以将产品数组放入会话中,然后作为数组进行访问。

You can put products array into session then access it as an array.

设置时:

$_SESSION["products"] = $_POST["product"];

列出产品时,应检查会话中的值:

When listing products, you should check the values from session:

for($i = 0; $i < 24; $i++) {
    echo '<name="product[]"  type="checkbox" value="'.$i.'" alt="1607.00"';
    if(in_array($i, $_SESSION["products"])) echo ' checked="checked" ';
    echo ' />';
}

这是基本思想和示例代码。

This is the basic idea and example code.

-更新-

根据您的评论:

在表格中,我们将按照以下方式打印产品:

Inside the form, we will print products as follow:

<?php
session_start();
$session_products = array();
if(array_key_exists("products", $_SESSION)) 
{
  if($_SESSION["products"] != null)
  {
    $session_products = $_SESSION["products"];
  }
}

<form method="post" action="newtest.php"> 
    <input name="product[]" type="checkbox" value="1" <?php if(in_array("1", $session_products)) echo "checked='checked'"; ?> alt="1607.00" /> 
    <input name="product[]" type="checkbox" value="2" <?php if(in_array("2", $session_products)) echo "checked='checked'"; ?>  alt="1848.00" /> 
    <input name="product[]" type="checkbox" value="3" <?php if(in_array("3", $session_products])) echo "checked='checked'"; ?>  alt="180.00" /> 
    <input name="product[]" type="checkbox" value="4" <?php if(in_array("4", $session_products)) echo "checked='checked'"; ?>  alt="650.00" />
    and so on upto 24 ...
</form>

在发布表单值的代码中,我们将这些值放入会话中:

Inside the code where the form values are posted, we will put these values into session:

<?php 
include("config.php"); 
session_start(); 

if(isset($_POST))
{ 
    $_SESSION["products"] = $_POST["product"];
}
?>

这篇关于在会话中存储复选框值,然后重新选择返回的复选框以添加更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆