Creating a dynamic html table in php

With PHP we can dynamically create HTML table and fetch record from database to show into that table.

In the following form, user is asked to input to numbers.

index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

</head>

<body>

<form id="frm" name="frm" method="post" action="table.php" >
  <table border="1">
  <tr>
    <td>Enter Number</td>
    <td>
    <input id="txtnum" name="txtnum" type="text" />
    </td>
  </tr>
  
    <tr>
    <td>Enter Number (upto)</td>
    <td>
<input id="txtnumupto" name="txtnumupto" type="text" />
    
    </td>
  </tr>

    <tr>
    <td></td>
    <td>
    <input id="btnsubmit" name="btnsubmit" type="submit" value="Submit"/>
    </td>
  </tr>

  </table>
  
  
</form>

</body>
</html>

When the user presses submit button, the next page table.php is called on which a dynamic table is created using php

table.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

</head>

<body>
<?php 
$txtnum = $_POST['txtnum'];
$txtnumupto = $_POST['txtnumupto'];
?>
<table border="1">
<?php 
for($i=1; $i<$txtnumupto; $i++)
{
?>
<tr>
<td><?php echo $txtnum; ?></td>
<td>x</td>
<td><?php echo $i; ?></td>
<td>=</td>
<td><?php echo $i * $txtnum; ?></td>
</tr>
<?php	
}
?>
</table>


</body>
</html>

 

Add a Comment

Your email address will not be published. Required fields are marked *