也许是一个重复的问题,但我找不到任何答案。我有一个选择框,其值将存储在会话数组变量中。我试过但没有工作。
以下是代码: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>
这是result.php代码:
<?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']);
?>
输入框值存储在会话arrray变量中,但不存储在选择框中。
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 ) ) )
我希望将选择框值设为:
Array ( [0] => ddd [1] => ddd [2] => sss [3] => sss [4] => ss [5] => ss [6] => ttt )
欢迎任何有关此事的建议或帮助。
答案 0 :(得分:0)
尝试更改此内容:
$_SESSION['strtpnt1'][] = $strtpnt1;
到此:
$_SESSION['strtpnt1'] = $strtpnt1;
<强>更新强>
if(isset($_POST['strtpnt'])) {
$strtpnt1 = isset($_SESSION['strtpnt1']) ? $_SESSION['strtpnt1'] : array();
array_push($strtpnt1, $_POST['strtpnt']);
$_SESSION['strtpnt1'] = $strtpnt1;
}
编辑:直接从$_POST
推送值,而不存储在任何数组中。
array_push()
用于将值附加到数组。
答案 1 :(得分:0)
请尝试[考虑您与kRiz的讨论]
if(isset($_POST['strtpnt'])) {
$strtpnt1 = isset($_SESSION['strtpnt1']) ? $_SESSION['strtpnt1'] :array();
if(!array_key_exists($_POST['strtpnt'],$_SESSION['strtpnt1']))
{
$strtpnt1 = array_push($strtpnt1,$_POST['strtpnt']);
}
$_SESSION['strtpnt1'] = $strtpnt1;
}