是否可以从HTML表单(复选框)发布多个“值”


Is it possible to have multiple "values" posted from a HTML form (checkboxes)?

我正在为某些产品开发PHP的订购页面。用户需要能够选择多种类型的产品,因此 HTML 表单中必须选中复选框。

我已经通过"name"属性为复选框建立了一个数组。

我的问题是,我想在确认页面上显示用户选择的内容,因此我希望这些复选框的"值"最终返回产品名称和产品价格。据我所知,我只会坚持一个值,所以我试图解决这个问题:

<?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>";
}
}
?>

有没有办法让我的$key变量实际上就像它是数组的名称一样?如果没有,有什么好方法可以做到这一点吗?

因此,在 HTML 表中,您需要像这样呈现每个复选框:

<input type="checkbox" name="selectedIDs[]" value="$key" />

其中$key是物料编号。

然后,在 PHP 中,您将有一个 $_POST["selectedIDs"] 变量,它是所有已检查的项目编号的数组。

假设您有一个这样的产品列表数组:

$products = array( 1 => array("Big Red", 575), 2 => array("Spearmint", 525));

然后,您可以使用如下循环打印所选产品的列表:

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>";
}

这和你写的之间唯一真正的区别是我的$products数组是二维的,我的$key用于从$products数组中获取相关产品。

您可以为optionvalue提供相应项的 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'];
}
?>

当然,如果您的数组源自数据库,则可以使用表的 ID 作为复选框的值。然后,您可以用逗号内爆 $_POST['product'](当然是在转义之后),并在SELECT ... WHERE ID IN ($productIds)查询中使用它。

如果我理解正确,你会有这样的复选框:

<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/>

这应该将检查为数组的任何内容发布到您的服务器。

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.....我需要在进一步的页面中所有这些值,所以采取了隐藏变量

<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

    $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 . ".&nbsp&nbsp&nbsp"; ?>
                                                                </b></td><td>
                                                                <?php echo  $header = $row['h_content'] . "<br />";
                                                                ?></td>
                                                                </tr>
                                                                <?php
                                                            }
                                                    }
                                            //echo $header; ?>
                                            </table>