Multiplication table in php

Program to print the multiplication table of any number given from the user using PHP 

Q. Create an html form to take an integer value as input , Whenever use submits the form with the integer number , the next page should display the multiplication table of that particular number in an html table  in php . please perform the necessary javascript validation at the form for integer value and null values.


In this example you learn about how to print the multiplication table of any given number from the user in PHP. 

To create a multiplication table in php you should have the knowledge of for loop. first we get number from the user and display the multiplicatin table using for loop.

we use HTML (hypertext markup language) and PHP to display the multiplication table. HTML part is used for design the form to get input from the user and PHP is used to execute the code and display the result in HTML an table.

To run the PHP code you should have Xampp server otherwise the code is not run. to run the code you first open xammp control panel and start Apache  and run the following code.



We use action attribute to display the result in another page (display.php).


index.php

<!DOCTYPE html>
<html lang="en">


<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> Multiplication table in PHP </title>
</head>


<body>


    <center>


        <form method="POST" action="display.php" name="form" onsubmit="return val()">
            Enter a number:
            <input type="text" name="num" id="n">
            <span id="numberText"></span>
            <input type="Submit" value="submit" onclick="val()">
        </form>
    </center>



    <script>
        function val() {
            var n = document.getElementById("n").value;
            if (n == null || n == "") {
                alert("please enter a number");
                return false;
            } else {
                if (isNaN(n)) {
                        alert("please enter a numeric value");
                        return false;
                    }
               
                }
            }
    </script>



</body>


</html>


display.php


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multiplication of <?php echo ($_POST['num']); ?></title>
</head>

<body>
    <center>

        <h3>The Multiplication table of <?php echo ($_POST['num']); ?></h3>
        <table border="1" cellspacing="0">
            <?php
            if ($_POST) {
                $num = $_POST['num'];

                for ($i = 1; $i <= 10; $i++) {
                    $mul = $num * $i;
            ?>
                    <tr>
                        <td>
                            <?php
                            echo "$num * $i = $mul<br>";
                            ?>
                        </td>
                    </tr>
            <?php
                }
            }

            ?>


        </table>


    </center>


</body>

</html>


Output

     

If you submit the form without any number 


If you submit the form with an Alphabet 












Previous Post Next Post

Contact Form