在表单提交时仅显示复选框选中的数据


Display only the checkbox checked data on form submit

我有一个表单,它由一个表组成,其中有四个用于比较的数据单元格,并且每个列都有一个复选框。当我单击复选框并提交表单时,我想在新页面中只显示选中的表。我该怎么做呢?有人能帮我举个例子吗?

 <!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>Comparision of Printers</title>
</head>
<body>
<form action="newpage.php" method="get">
<table width="1023" border="1">
  <tr>
    <td width="193"></td>
    <td class="p1" width="113"><img src="images/upmini.png"/>
      <label>
      <input type="checkbox" name="p[]" value="Printer1" id="CheckboxGroup1_0" />
      Printer1</label>
</td>
    <td class="p2" width="67"><img  src="images/upmini.png"/>
      <label>
      <input type="checkbox" name="p[]" value="Printer2" id="CheckboxGroup1_1" />
      Printer2</label></td>
    <td class="p3" width="67"><img  src="images/upmini.png"/><label>
      <input type="checkbox" name="p[]" value="Printer3" id="CheckboxGroup1_2" />
      Printer3</label></td>
    <td class="p4" width="67"><img  src="images/upmini.png"/><label>
      <input type="checkbox" name="p[]" value="Pritner4" id="CheckboxGroup1_3" />
      Printer4</label></td>
</tr>
<tr>
  <td>Title</td>
    <td class="p1" >Printer1</td>
    <td class="p2" >Printer2</td>
    <td class="p3" >Printer3</td>
    <td class="p4" >Printer4</td>
</tr>
<tr>
    <td>Manufacturer</td>
    <td class="p1" >Personal Portable 3DPrinters (PP3DP) founded by Be</td>
    <td class="p2" >Personal Portable 3DPrinters (PP3DP) founded by Be</td>
    <td class="p3" >Personal Portable 3DPrinters (PP3DP) founded by Be</td>
    <td class="p4" >Personal Portable 3DPrinters (PP3DP) founded by Be</td>
</tr>
</table>
<input type="submit" name="compare" value="compare" />
</form>
</body></html>

我认为您应该使用POST而不是GET,并将printermanufacturer信息存储在数组中。我使用二维数组来存储信息,使它更容易。并在GET REQUEST中获取该数组的key值,然后只打印与键匹配的值。很简单. .

这是完整的功能代码(稍作改动)打印机的比较

</head>
<body>
// store your infomation in a arrya and use KEY same as in your form checkbox name
<?php 
  $printers = array(
    'printer1' => array(
                    'title' => 'printer1',
                    'manufacturer' => 'Personal Portable 3DPrinters (PP3DP) founded by Be 1'
                  ),
    'printer3' => array(
                    'title' => 'printer2',
                    'manufacturer' => 'Personal Portable 3DPrinters (PP3DP) founded by Be 2'
                  ),
    'printer2' => array(
                    'title' => 'printer3',
                    'manufacturer' => 'Personal Portable 3DPrinters (PP3DP) founded by Be 3'
                  ),
    'printer4' => array(
                    'title' => 'printer4',
                    'manufacturer' => 'Personal Portable 3DPrinters (PP3DP) founded by Be 4'
                  ) 
    )
 ?>
 //if form submited then print the submited index only
<?php if(isset($_GET['p'])):?>
    <?php $printerIndex = $_GET['p'];?> 
    <table width="1023" border="1">
        <td>Title</td>
            <?php foreach($printerIndex as $index): ?>
                <td class = "p1" ><?php echo $printers[$index]['title']; ?></td>
            <?php endforeach; ?>
        </tr>
        <tr>
            <td>Manufacturer</td>
          <?php foreach($printerIndex as $index): ?>
                <td class = "p1" ><?php echo $printers[$index]['manufacturer']; ?></td>
            <?php endforeach; ?>
        </tr>
    </table> 
<?php endif; ?>
//make the table
<?php if(!isset($_GET['p'])): ?>
<form action="" method="get">
<table width="1023" border="1">
    <tr>
        <td width="193"></td>
        <?php foreach($printers as $printer ): ?>
        <td class="p1" width="113"><img src="images/upmini.png"/>
        <label> <?php echo $printer['title']; ?></label>
        <input type="checkbox" name="p[]" value="<?php echo $printer['title']; ?>" id="CheckboxGroup1_0" />
        </td>
        <?php endforeach; ?>
    </tr>
<tr>
<td>Title</td>
    <?php foreach($printers as $printer): ?>
        <td class = "p1" ><?php echo $printer['title']; ?></td>
    <?php endforeach; ?>
</tr>
<tr>
    <td>Manufacturer</td>
   <?php foreach($printers as $printer): ?>
       <td class = "p1" ><?php echo $printer['manufacturer']; ?></td>
   <?php endforeach; ?>
</tr>
</table>
<input type="submit" name="compare" value="compare" />
</form>
<?php endif; ?>
</body></html>

检查的打印机以数组p的形式存储在$_GET变量中。如。检查示例中的打印机1和3将显示以下结果:

<?php var_dump($_GET) ?>
array
  'p' => 
    array
      0 => string 'Printer1' (length=8)
      1 => string 'Printer3' (length=8)
  'compare' => string 'compare' (length=7)

只有一个表有几个td。

在newpage。php中你可以这样写:

if (isset($_GET['P']))
{ 
    $P = $_GET['P'];
    for ($i=0;$i<sizeof($P);$i++)
    {
        // Show what you like, e.g.
        echo "Printer".$P[$i]."<br />";
    }
}

这是你的意思吗?