Post 是否可以通过复选框将子数组作为数组传递到另一个页面?
我是php新手,所以如果我问一些基本的问题,请原谅我。我已经在谷歌上搜索了几天了,尤其是在这个论坛上,我找到了我以前的大部分答案,但是我找不到任何关于这个问题的答案,所以我必须问一下。这是我的第一个问题Post 是否可以通过复选框将子数组作为数组传递到另一个页面?,post,multidimensional-array,checkbox,Post,Multidimensional Array,Checkbox,我是php新手,所以如果我问一些基本的问题,请原谅我。我已经在谷歌上搜索了几天了,尤其是在这个论坛上,我找到了我以前的大部分答案,但是我找不到任何关于这个问题的答案,所以我必须问一下。这是我的第一个问题 // this array is coming from MySQL db as a result. It's a list of user's friends and it could contain dozens or hundreds of friends. Now he wants to
// this array is coming from MySQL db as a result. It's a list of user's friends and it could contain dozens or hundreds of friends. Now he wants to put them in different groups.
$array = array(
array("John", "Doe", "1"),
array("Peter", "Citizen", "2")
...
);
// a page is created with the result. Each record has a checkbox that the user can select.
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
foreach($array as $item){
$nItem = $item;
?>
<input type="text" name="fname" value="<?php if(isset($nItem)){echo $nItem[0];} ?>" readonly="readonly" />
<input type="text" name="lname" value="<?php if(isset($nItem)){echo $nItem[1];} ?>" readonly="readonly" />
<input type="text" name="uid" value="<?php if(isset($nItem)){echo $nItem[2];} ?>" readonly="readonly" />
<input type="checkbox" name="val[]" value="<?php if(isset($nItem)){echo $nItem;} ?>" /> // I want to send this $nItem array as it is to the action page and read its keys and values there.
<br>
<?php
}
?>
<input type="submit" name="submit" value="Submit">
</form>
<br />
<?php
// if I tick both checkboxes and submit I get the following:
if(isset($_POST['val'])){
$val = $_POST['val'];
echo var_dump($val), '<br />'; // array(2) { [0]=> string(5) "Array" [1]=> string(5) "Array" }
echo count($val), '<br />'; // 2
print_r($val); // Array ( [0] => Array [1] => Array )
echo("val is {$val[0]}"); // val is Array
foreach($val as $key => $value){
echo "Key and Value are: ".$key." ".$value, '<br />'; // Key and Value are: 0 Array
} // Key and Value are: 1 Array
}
?>
-然后我得到-
0 John Doe 1
-但那不是我想要的。我希望操作页面上的实际数组可以迭代。我想既然它已经是一个数组了,那就不难了,但我错了
谁能告诉我怎么做吗
非常感谢
<?php foreach($array as $item): ?>
<input type="text" name="fname" value="<?php echo $item[0]; ?>" readonly="readonly" />
<input type="text" name="lname" value="<?php echo $item[1]; ?>" readonly="readonly" />
<input type="text" name="uid" value="<?php echo $item[2]; ?>" readonly="readonly" />
<input type="checkbox" name="val" value="<?php echo htmlentities(serialize($item)); ?>" />
<br>
<?php endforeach; ?>
提交后,取消序列化数组$\u POST['val']。
根据您对该阵列所做的操作,该方法不太适合用于注射剂
与其使用包含所有信息的数组,不如对每条记录使用id。
名为val的复选框具有相应id的值
<input type="checkbox" name="val" value="<?php echo $item['id']; ?>" />
提交后,查询属于该id的记录,并使用返回的数组进行处理
<input type="checkbox" name="val" value="<?php echo $item['id']; ?>" />