Tuesday, February 2, 2021

Basic Math Operations in PHP using Drop Down Menu

 A simple php program to perform basic math operations using addition, subtraction, multiplication and division by asking the user to give two numbers and select the operations using drop-down menu in HTML.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod City I also accepting computer repair, networking, and Arduino Project development at a very affordable price. My website is www.jakerpomperada.blogspot.com and www.jakerpomperada.com

If you like this video please click the LIKE button, SHARE, and SUBSCRIBE to my channel.

Your support on my channel is highly appreciated.

Thank you very much.









Program Listing

<?php 
    $num1 = $num2 = $oper = $answer = $infotext = "";

    if(isset($_POST['btnSolve'])) 
    { 
        $num1   = $_POST['txtNum1'];
        $num2   = $_POST['txtNum2'];
        $oper   = $_POST['txtOper'];

        switch ($oper) {
            case "Addition":
                $answer = $num1 + $num2;
                $infotext = $num1." + ".$num2;
                break;
            case "Subtraction":
                $answer = $num1 - $num2;
                $infotext = $num1." - ".$num2;
                break;
            case "Multiplication":
                $answer = $num1 * $num2;
                $infotext = $num1." x ".$num2;
                break;
            case "Division":
                $answer = $num1 / $num2;
                $infotext = $num1." / ".$num2;
                break;
        }

        $answer='<div class="answer">'.$infotext.' = '.$answer.'</div>';
    }
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Basic Math Operations in PHP Using Drop Down Menu</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>          
        <h3 align="center"> Basic Math Operations in PHP Using Drop Down Menu</h3>
        <main>
            <div class="container">
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                    <label for="txtNum1"><b>First Number:</b></label>
                    <input type="number" name="txtNum1" id="txtNum1" value="<?php echo $num1; ?>" required>

                    <label for="txtNum2"><b>Second Number:</b></label>
                    <input type="number" name="txtNum2" id="txtNum2" value="<?php echo $num2; ?>" required>

                    <label for="txtOper"><b>Select Operator:</b></label>
                    <select name="txtOper" id="txtOper" value="<?php echo $oper; ?>" required>
                        <option value="Addition">Addition</option>
                        <option value="Subtraction">Subtraction</option>
                        <option value="Multiplication">Multiplication</option>
                        <option value="Division">Division</option>
                    </select>

                    <button type="submit" name="btnSolve" class="btnSolve">Solve</button>

                    <?php echo $answer;?>
                </form>
            </div>
        </main>
    </body>
</html>

style.css

* {
    box-sizing: border-box;
}

body {
    font-family: Arial, Helvetica, sans-serif;
    background: #fefefe;
    color: #1a1a1a;
    font-size: 15px;
}

main {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    box-shadow: rgba(99, 99, 99, 0.2) 0px 2px 8px 0px;
    border-radius: 10px;
}

.container {
    display: block;
    padding: 30px;
    background: #fff;
    text-align: left;
    border-radius: 2px;
}

input,
select {
    display: inline-block;
    background: #f5f5f5;
    width: 100%;
    margin: 5px 0 20px 0;
    padding: 15px;
    border: none;
    font-size: 15px;
}

input:focus,
select:focus {
    background: #eeeeee;
    outline: none;
}

button {
    background: #2196f3;
    color: #fff;
    padding: 10px 20px;
    margin: 10px 0 0;
    border: none;
    cursor: pointer;
    width: 100%;
    opacity: 0.9;
    text-transform: uppercase;
    font-size: 15px;
    outline: none;
}

button:hover {
    opacity: 1;
}

.answer {
    padding: 20px;
    margin-top: 20px;
    text-align: center;
    font-size: 30px;
    font-weight: bold;
    background: #dadada;
}




No comments:

Post a Comment