0

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>
Virge Assault
  • 1,356
  • 6
  • 18
  • 40
  • done any basic debugging, like doing a 'view source' on your page to see if any of the php shows up? If it does, then your server is misconfigured and not executing your file, or you've named the file wrong, or you don't have php installed. – Marc B Sep 15 '14 at 17:36
  • you are loading calculator.php, where is that file? – Grasper Sep 15 '14 at 17:42
  • @Grasper by the looks of it. calculator.php is this file. – SuperDJ Sep 15 '14 at 17:43
  • Replace 'echo = ($x / $y)' with 'echo ($x / $y)' in "div" action. – doubleui Sep 15 '14 at 17:47

3 Answers3

2
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.

Stephen
  • 4,041
  • 22
  • 39
1

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);
Community
  • 1
  • 1
Mate Solymosi
  • 5,699
  • 23
  • 30
0

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:

SuperDJ
  • 7,488
  • 11
  • 40
  • 74