php 是否可以从 HTML 表单(复选框)发布多个“值”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10355330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to have multiple "values" posted from a HTML form (checkboxes)?
提问by Esten
I am developing an ordering page in PHP for some products. The user needs to be able to select multiple types of products, so checkboxes will be necessary in the HTML form.
我正在用 PHP 为某些产品开发一个订购页面。用户需要能够选择多种类型的产品,因此 HTML 表单中需要复选框。
I've established an array for the checkboxes via the "name" attribute.
我已经通过“名称”属性为复选框建立了一个数组。
My problem is, I want to display what the user has selected on a confirmation page, so I'd like the "value" of those checkboxes to ultimately return a product name AND a product price. As far as I can tell I'm going to be stuck with one value only, so I've attempted to get around this:
我的问题是,我想在确认页面上显示用户选择的内容,因此我希望这些复选框的“值”最终返回产品名称和产品价格。据我所知,我只会被一个值困住,所以我试图解决这个问题:
<?php
//variables for the array and the count
$parray = $_POST['product'];
$pcount = count($parray);
//arrays for each product's information; i've only listed one for simplicity
$br = array("Big Red", 575);
/*The idea for this was for each product ordered to add a line giving the product information and display it. When the $key variable is assigned, it is stored simply as a string, and not an array (yielding [0] as the first letter of the string and [1] as the second), which I expected, but my general idea is to somehow use $key to reference the above product information array, so that it can be displayed here*/
if (!empty($parray))
{
for ($i=0; $i < $pcount; $i++)
{
$key = $parray[i];
echo "<tr><td height='40'></td><td>" . $key[0] . "</td><td>" . $key[1] . "</td></tr>";
}
}
?>
Is there anyway to make my $key variable actually act as if it is the array's name it is set to? If not, is there any good way to do this?
有没有办法让我的 $key 变量实际上就像它设置的数组名称一样?如果没有,有什么好的方法可以做到这一点?
回答by Uchendu Nwachuku
So in your HTML table, you'll need to render each checkbox like this:
所以在你的 HTML 表格中,你需要像这样呈现每个复选框:
<input type="checkbox" name="selectedIDs[]" value="$key" />
where $keyis the item number.
$key商品编号在哪里。
Then, in your PHP, you'll have a $_POST["selectedIDs"]variable, which is an array of all the item numbers that were checked.
然后,在您的 PHP 中,您将拥有一个$_POST["selectedIDs"]变量,它是一个包含所有已检查项目编号的数组。
Assuming you've got a product list array like this:
假设你有一个这样的产品列表数组:
$products = array( 1 => array("Big Red", 575), 2 => array("Spearmint", 525));
You can then print a list of the selected products using a loop like this:
然后,您可以使用如下循环打印所选产品的列表:
for($i = 0; $i < count($_POST["selectedIDs"]); $i++) {
$key = $_POST["selectedIDs"][$i];
$product = $products[$key];
echo "<tr><td height='40'></td><td>" . $product[0] . "</td><td>" . $product[1] . "</td></tr>";
}
The only real difference between this and what you wrote is that my $productsarray is two-dimensional, and my $keyis used to grab the relevant product from the $productsarray.
这和你写的唯一真正的区别是我的$products数组是二维的,我$key是用来从$products数组中抓取相关产品的。
回答by CodeCaster
You can give the valueof the options the value of the ID of the corresponding item. Then you'll receive an array of ID's, which you can then again map to the $brarray, where you look up the name and price of an item by its ID. You could use the array key as ID, so:
你可以给value的的optionS中的相应项目的ID值。然后您将收到一个 ID 数组,然后您可以再次将其映射到该$br数组,在该数组中您可以通过其 ID 查找商品的名称和价格。您可以使用数组键作为 ID,因此:
<!-- Form: -->
<input type="checkbox" name="product[]" value="1" />
<input type="checkbox" name="product[]" value="..." />
<input type="checkbox" name="product[]" value="15" />
[...]
<?php
$product = array();
$product[1] = array('name' => "Big Red", 'price' => 575);
$product[...] = array('name' => "...", 'price' => ...);
$product[15] = array('name' => "Small Green", 'price' => 475);
foreach ($_POST['product'] as $pId)
{
echo $products[$pId]['name'] . " costs " . $products[$pId]['price'];
}
?>
If of course your array originates from a database, you can use the table's ID's as values for the checkboxes. You could then implode the $_POST['product'] (of course after escaping it) with a comma, and use it in a SELECT ... WHERE ID IN ($productIds)query.
当然,如果您的数组源自数据库,您可以使用表的 ID 作为复选框的值。然后,您可以使用逗号对 $_POST['product'] (当然在转义它之后)进行内爆,并在SELECT ... WHERE ID IN ($productIds)查询中使用它。
回答by GDP
If i'm understanding you correctly, you'll have checkboxes something like this:
如果我正确理解你,你会有这样的复选框:
<input type="checkbox" name="product_id[]" value="1">Product #1<br/>
<input type="checkbox" name="product_id[]" value="2">Product #2<br/>
<input type="checkbox" name="product_id[]" value="3">Product #3<br/>
That should post whatever is checked as an array to your server.
这应该将任何检查为数组的内容发布到您的服务器。
回答by GDP
gen_paper1.php
gen_paper1.php
<div style="overflow: auto; height: 500px; border: 1px solid;">
<form action="" method="post">
<fieldset style="font-size: 15px; font-weight: bolder;">
<h4><u>Your Existing Header's are listed below</u> </h4>
<hr / >
<?php
$xhdr = mysql_query("SELECT * from s_user_header ORDER BY 'ASC'");
while($rheader = mysql_fetch_array($xhdr)){
$h = $rheader['h_content'];
$hid = $rheader['h_id'];?>
<script>
$(document).ready(function(){
//$('input[type=checkbox]').val('<?php echo $hid ; ?>').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
//$('#c').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
$('').tzCheckbox({labels:['<?php echo $h; ?>','<?php echo $h; ?>']});
});
</script>
<table>
<td><input type="checkbox" id="c" name="u_hdr[]" value="<?php echo $hid; ?>"></td>
<td style="font-weight: bolder"><?php echo $h."<br />"; ?></td>
</table>
<?php } ?>
</fieldset>
</div>
gen_paper2.....3..... i need all this values in further page, so have taken hidden variables
gen_paper2.....3 ..... 我需要在下一页中的所有这些值,所以采用了隐藏变量
<input type="hidden" name="hstdid" value="<?php echo $stdid; ?>">
<input type="hidden" name="hsubid" value="<?php echo $subid; ?>">
<input type="hidden" name="hdifid" value="<?php echo $difid; ?>">
<input type="hidden" name="hmarks" value="<?php echo $t_marks; ?>">
<input type="hidden" name="h_hid[]" value="<?php echo $hh; ?>">
gen_paper4.php
gen_paper4.php
$getstdid = $_POST['hstdid3'] ;
$getsubid = $_POST['hsubid3'] ;
$getdifid = $_POST['hdifid3'] ;
$gethmarks = $_POST['htmarks'] ;
$hdr= $_POST['h_hdrid'] ;
$h = implode($hdr);
<table>
<?php
$h1 = explode(",",$h);
count($h1) . "<br />";
for($i=0;$i<count($h1);$i++){
$h1[$i];
$xheader = mysql_query("SELECT * from s_user_header WHERE h_id = ".$h1[$i]);
while($row = mysql_fetch_array($xheader)){ ?>
<tr>
<td><b>
<?php echo $i + 1 . ".   "; ?>
</b></td><td>
<?php echo $header = $row['h_content'] . "<br />";
?></td>
</tr>
<?php
}
}
//echo $header; ?>
</table>

