【问题标题】:Display only the checkbox checked data on form submit仅在表单提交时显示复选框选中的数据
【发布时间】:2013-03-13 23:52:06
【问题描述】:

我有一个表格,它由一个表格组成,表格中有四个数据单元格用于比较,而且每一列都有一个复选框。当我单击复选框并提交表单时,我只想在新页面中显示选中的表格。我怎样才能做到这一点?任何人都可以帮助我提供工作示例吗?

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

【问题讨论】:

    标签: php jquery html-table


    【解决方案1】:

    我认为您应该使用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>
    

    【讨论】:

    • 非常感谢:)。它解决了我的问题。 +1 这个答案。再次感谢。
    • 很高兴知道..如果可以的话,请通过“此答案左上角”的投票按钮 +1 并将其标记为已接受的答案。谢谢
    • 我试图标记它,但只有 6 个声望。我需要另一个 9 来标记它为好。我总共需要 15 声望。
    • 好的没问题,当你有足够的时候做......享受stackOverflow
    【解决方案2】:

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

    这是你的意思吗?

    【讨论】:

    • 不,我想在复选框选中的条件下显示标题、制造商等表格数据。这个问题很直接。
    • @user2148257 用名称属性替换类,例如p1_manufacturer、p2_manufacturer 等等。然后你可以像我的例子一样使用 $_GET['p'.$P[$i].'_manufacturer'] 访问这些值。对标题做同样的事情。
    【解决方案3】:

    检查的打印机以数组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)
    

    【讨论】:

    • 您好,我可以在 URL 栏中的 newpage.php 中看到检查的值。但是我怎样才能只在 newpage.php 中显示选中的表格????
    • 出于调试目的,只需使用上面示例中的 'var_dump($_GET)'。
    • 要在模板中显示数据,只需遍历 p 数组。类似的东西:foreach ($_GET['p'] as $printer) { echo $printer; }
    • 你能给我看一个工作的例子吗?可能在小提琴中。谢谢
    • &lt;?php if (isset($_GET['p']) &amp;&amp; is_array($_GET['p'])) { echo '&lt;p&gt;Selected printers:&lt;/p&gt;'; foreach ($_GET['p'] as $printer) { echo '&lt;p&gt;' . $printer . '&lt;/p&gt;'; } } ?&gt;
    猜你喜欢
    • 2021-09-22
    • 2013-01-05
    • 1970-01-01
    • 2023-02-17
    • 2016-01-29
    • 2012-05-23
    • 2014-04-28
    相关资源
    最近更新 更多