Virge Assault
Virge Assault

Reputation: 1396

php not loading after html form

The html in this form loads in the browser, but when I fill the form and submit the php does not load, just a blank page is show.

I tried separating the files into an html and a php file as well, that didn't work.

<!DOCTYPE html>
<head><title>Calculator</title></head>
<body>


<form action="calculator.php" method="POST">
    <p>
        <label for="firstnumber">First Number</label>
        <input type="text" name="firstnumber" id="firstnumber" />
    </p>
    <p>
        <label for="second number">Second Number:</label>
        <input type="text" name="secondnumber" id="secondnumber" />
    </p>

    <p>
        <input type="radio" name="Operation" value="Add" id="addinput" /> <label for="addinput">Add</label>
        <input type="radio" name="Operation" value="Sub" id="subinput" /> <label for="subinput">Sub</label>
        <input type="radio" name="Operation" value="Mult" id="multinput" /> <label for="multinput">Mult</label>
        <input type="radio" name="Operation" value="Div" id="divinput" /> <label for="divinput">Div</label>
    </p>
    <p>
        <input type="submit" value="=" />
        <input type="reset" />
    </p>
</form>


<?php

$Operation = $_POST['Operation'];
$x = $_POST["firstnumber"];
$y = $_POST["secondnumber"];

if($Operation == "Add"){
    echo ($x + $y);
}

if($Operation == "Sub"){
    echo ($x - $y);
}

if($Operation == "Mult"){
    echo ($x * $y);
}

if($Operation == "Div"){
    if ($y==0){
    echo "error-cannot divide by zero";
}
    else echo = ($x / $y);
}

?>

</body>
</html>

Upvotes: 0

Views: 58

Answers (3)

SuperDJ
SuperDJ

Reputation: 7661

Wrap all of this in an if($_POST) Like so:

if($_POST) {
    $Operation = $_POST['Operation'];
    $x = $_POST["firstnumber"];
    $y = $_POST["secondnumber"];

    if($Operation == "Add"){
        echo ($x + $y);
    }

    if($Operation == "Sub"){
        echo ($x - $y);
    }

    if($Operation == "Mult"){
        echo ($x * $y);
    }

    if($Operation == "Div"){
        if ($y==0){
            echo "error-cannot divide by zero";
        }
    }
        else echo ($x / $y);
    }
}

This way the code is only executed when the form is posted. This should also help you in the futher. Also consider escaping your user input. So something like: mysqli_real_escape_string($connection, $_POST['Operation']); aditional to this you could use trim() and strip_tags(). Some links:

Upvotes: 0

Mate Solymosi
Mate Solymosi

Reputation: 5977

Most likely you have error reporting turned off in your PHP installation.
Here are some answers on how to turn it on: How to get useful error messages in PHP?

If you turn on error messages, instead of a blank page, you will see a Syntax Error message because of the unnecessary = sign in the following line:

else echo = ($x / $y);

Upvotes: 1

Stephen
Stephen

Reputation: 4249

else echo = ($x / $y);

The equals sign should not be there. remove it.

else echo ($x / $y);

This syntax error was preventing your page from rendering.

Upvotes: 2

Related Questions