仅显示表单提交上的复选框检查数据

时间:2013-03-24 13:12:58

标签: php jquery html-table

我有一个表格,其中包含一个包含四个数据单元格的表格,用于比较,每个列都有一个复选框。当我单击复选框并提交表单时,我想只显示新页面中的已检查表格。我怎样才能做到这一点?任何人都可以通过工作实例帮助我吗?

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

3 个答案:

答案 0 :(得分:2)

我认为您应该使用POST代替GET,并将printermanufacturer信息存储在数组中。我使用2D数组来存储信息,以方便使用。并在key中获取此数组的GET REQUEST值,而不是仅打印与键匹配的值。很简单..

这是完整的功能代码(稍微改变一下)                    打印机的比较

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

答案 1 :(得分:0)

已检查的打印机将作为数组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)

答案 2 :(得分:0)

你只有一张表有几个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 />";
    }
}

这是你的意思吗?