0

So I got this code and I'm still learning and I don't know what might the problem be, and a question is it unsafe to do this way?

<?php
    print_form();
    if(isset($_POST['calculate'])){
        process_form();
    }
    function process_form(){
        switch($_POST){
            case "addition":
                print $_POST['operand1'] + $_POST['operand2'];
                break;
            case "subtraction":

                break;
            case "multiplication":

                break;
            case "division":

        }
    }
    function print_form(){
        print <<<HTML
            <html>
                <head><title>Learning Php</title></head>

                <body>
                    <form method="POST" action="">
                        Operand 1: <input type="text" name="operand1"><br>
                        Operand 2: <input type="text" name="operand2"><br>
                        <select name="operation">
                            <option value="addition"> Addition </option>
                            <option value="subtraction"> Subtraction </option>
                            <option value="multiplication"> Multiplication </option>
                            <option value="division"> Division </option>
                        </select>
                        <input type="submit" name="calculate" value="calculate">
                    </form> 
                </body>
            </html>
HTML;
    }
?>

Is it just not doing process_form() or is it resetting the value of calculate when pressing the button ?

3 Answers 3

1

You need to switch $_POST['operation'].

Most of vulnerabilities on websites are from saving data to databases, presenting other users data to users. Code like that does not seem to have any security problems, but echo seems to be more popular than print for outputting data.

Sign up to request clarification or add additional context in comments.

Comments

0

In your switch statement you should change switch($_POST) to switch($_POST['operation']).

As i can see you are matching select tag with name operation and then you do the math.

So, if you want to get the correct value of select tag then you must use $_POST['operation']

Comments

0

In function call, Pass the post data and define your function with parameter or argument as below

if(isset($_POST['calculate'])){
        process_form($_POST['calculate']);
    } 
function process_form($arg){
    switch($arg){
        case "addition":
            print $_POST['operand1'] + $_POST['operand2'];
            break;
        case "subtraction":

            break;
        case "multiplication":

            break;
        case "division":

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.