duandun3178 2015-06-16 09:56
浏览 59

如何在不使用多个的情况下在会话数组变量中存储选择框值?

Maybe its a duplicate question but i couldn't find any answer reg. I have a select box whose value is to be stored in session array variable. I tried but not working.

Here is the code: form1.php

<form name="input" action="result.php" method="post">
  <input type="text" name="product" />
  <input type="text" name="name1" />
  <select name="strtpnt" id="strtpnt"  class="inp_bx">  
    <option>-Select-</option>
    <option value="1">Cat</option>
    <option value="2">bat</option>
    <option value="3">rat</option>
    <option value="4">mat</option>
  </select>
  <input type="submit" value="Add" />
</form> 

Here is result.php code:

<?php
session_start();
$strtpnt1 = array();
if(isset($_POST['product'])) {
    $products = isset($_SESSION['products']) ? $_SESSION['products'] : array();
    $products[] = $_POST['product'];
    $_SESSION['products'] = $products;
}
if(isset($_POST['name1'])) {
    $name = isset($_SESSION['name']) ? $_SESSION['name'] : array();
    $name[] = $_POST['name1'];
    $_SESSION['name'] = $name;
}
if(isset($_POST['strtpnt'])) {
    $strtpnt1 = isset($_SESSION['strtpnt1']) ? $_SESSION['strtpnt1'] :array();
    $strtpnt1[] = $_POST['strtpnt'];
    $_SESSION['strtpnt1'][] = $strtpnt1;
}
print_r($_SESSION['products']);
print_r($_SESSION['name']);
print_r($_SESSION['strtpnt1']);
?>

The input box values are stored in session arrray variable but not the select box .

Array ( [0] => 2 [1] => Array ( [0] => 2 ) [2] => Array ( [0] => 2 ) [3] => Array ( [0] => 2 [1] => Array ( [0] => 2 ) [2] => Array ( [0] => 2 ) [3] => Array ( [0] => 2 ) ) )

I want to get the select box value as:

 Array ( [0] => ddd [1] => ddd [2] => sss [3] => sss [4] => ss [5] => ss [6] => ttt )

Any suggestions or help regarding this is more welcome.

  • 写回答

2条回答 默认 最新

  • dongnao2582 2015-06-16 10:22
    关注

    Try changing this:

    $_SESSION['strtpnt1'][] = $strtpnt1;
    

    to this:

    $_SESSION['strtpnt1'] = $strtpnt1;
    

    UPDATE:

    if(isset($_POST['strtpnt'])) {
        $strtpnt1 = isset($_SESSION['strtpnt1']) ? $_SESSION['strtpnt1'] : array();
        array_push($strtpnt1, $_POST['strtpnt']);
        $_SESSION['strtpnt1'] = $strtpnt1;
    }
    

    Edit: Directly push the value from $_POST without storing in any array.

    array_push() is used to append values to an array.

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?