Ujjwal Chowdary
Ujjwal Chowdary

Reputation: 125

Display only the checkbox checked data on form submit

I have a form that consists of a table with four data cells for comparison in it and also each column has a check box. When I click on the checkbox and submit the form then I want to display only the checked tables in new page. How can I do that? Can anyone help me with working example?

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

Upvotes: 1

Views: 2176

Answers (3)

Natwar Singh
Natwar Singh

Reputation: 2273

I think you should use POST instead of GET and store your printer and manufacturer information in a array. i use a 2D array to store the information to make it easy. and get the key values of this array in GET REQUEST and than print only those which match the keys. very simple..

this is full functional code ( changed little bit ) Comparision of Printers

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

Upvotes: 2

David 10K
David 10K

Reputation: 303

You have only ONE table with several TDs.

So within newpage.php you can do sth like:

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 />";
    }
}

Is it what you've meant?

Upvotes: 0

passioncoder
passioncoder

Reputation: 919

The checked printers are stored as array p in the $_GET variable. Eg. checking printer 1 and 3 in your example will show the following result:

<?php var_dump($_GET) ?>

array
  'p' => 
    array
      0 => string 'Printer1' (length=8)
      1 => string 'Printer3' (length=8)
  'compare' => string 'compare' (length=7)

Upvotes: 0

Related Questions