如何查看已检查列表的PHP?

时间:2014-02-19 22:03:37

标签: php html

大家好日子!我是PHP编码的新手。请问我该怎么做才能查看所选的复选框? 我有这段代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<style>
.form1{
    margin-left:655px;
    margin-top:-150px;
    }
.form2{
    margin-left:655px;
    margin-top:18px;
    }

.form3{
    margin-left:655px;
    margin-top:15px;
    }

.form4{
    margin-left:655px;
    margin-top:15px;
    }

.sub{
    width:100px;
    height:30px;
    }
</style>
<body bgcolor="#00FF99">
<center>
<br />
    <?php
    $ECHO = "";
    function button(){

        function button1(){
            echo '<form method = "post" action = "" class = "form1"><td align = "center" ><input type="checkbox" name="ch1" id="checkbox"/> Check 
            me out</td></form>';
        }
        function button2(){
            echo '<form method = "post" action = "" class = "form2"><td align = "center" ><input type="checkbox" name="ch[]" id="checkbox"/> Check
            me out</td></form>';
        }
        function button3(){
            echo '<form method = "post" action = "" class = "form3"><td align = "center" ><input type="checkbox" name="ch[]" id="checkbox"/> Check 
            me out</td></form>';
        }
        function button4(){
            echo '<form method = "post" action = "" class = "form4"><td align = "center" ><input type="checkbox" name="ch[]" id="checkbox"/> Check
            me out</td></form>';
                }
    }

$items = array (
                'item1' => array('1001', 'Apple', 'Red Apple', 18),
                'item2' => array('1002', 'Orange', 'Sweet Orange', 17),
                'item3' => array('1003', 'Strawberry', 'Class A Strawberry', 30),
                'item4' => array('1004', 'Mango', 'Yellow Mango', 20),
                );

echo '<table width = "800" border = "0">';
echo '<tr>';
echo '<th width = "500" align = "center">ID</th>
      <th width = "500" align = "center">Name</th>
      <th width = "500" align = "center">Description</th>
      <th width = "500" align = "center">Price</th>
      <th width = "500" align = "center">ADD</th>
      ';
echo '<tr>';

foreach($items as $item){
    foreach ($item as $s1){
            echo '<td height="30" width = "90" align = "center">'.$s1.'</td>';
    }
    echo '</tr>';
    }
echo '</table><br>';

button();
button1();
button2();
button3();
button4();
echo '<br><br><br><br>';
echo '<input type="submit" name="button" id="button" value="Submit" class="sub"/>';
?>
</center>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

<?php
$items = array (
    'item1' => array('1001', 'Apple', 'Red Apple', 18),
    'item2' => array('1002', 'Orange', 'Sweet Orange', 17),
    'item3' => array('1003', 'Strawberry', 'Class A Strawberry', 30),
    'item4' => array('1004', 'Mango', 'Yellow Mango', 20),
);

echo '<form method = "post" action = "" class = "df">';
echo '<table width = "800" border = "0">';
echo '<tr>';
echo '<th width = "500" align = "center">ID</th>
      <th width = "500" align = "center">Name</th>
      <th width = "500" align = "center">Description</th>
      <th width = "500" align = "center">Price</th>
      <th width = "500" align = "center">ADD</th>
      ';
echo '<tr>';

foreach ($items as $item){
    foreach ($item as $s1){
        echo '<td height="30" width = "90" align = "center">'.$s1.'</td>';
    }
    echo '<td align = "center" ><input type="checkbox" name="checked[]" value="ch_'. $item[0] .'" id="checkbox"/> Check me out</td>';
    echo '</tr>';
}
echo '</table><br>';

echo '<input type="submit" name="button" id="button" value="Submit" class="sub"/>';

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $checked = @$_POST['checked'];
        if (is_array($checked)) {
            foreach ($checked as $item) {
                $itemId = substr($item, 3);
                $key = 'item' . $itemId;
                print_r($items[$key]);
                echo "<br />checked item $itemId";

            }
        }
    }


?>