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

时间:2012-04-27 17:40:00

标签: php html arrays forms post

我正在为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变量实际上就像它设置的数组名一样? 如果没有,有什么好办法吗?

4 个答案:

答案 0 :(得分:6)

因此,在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数组中获取相关产品。

答案 1 :(得分:3)

您可以为value option提供相应项目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)查询中使用它。

答案 2 :(得分:1)

如果我正确理解你,你会看到类似这样的复选框:

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

那应该将作为数组检查的内容发布到您的服务器。

答案 3 :(得分:0)

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>