Jump to content

PHP Calculator… Improvement


masonh928

Recommended Posts

<html><form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">Number-1: <input type="text" name="num_1"><br>Multiply or Divide:<select><option name="multiply">Multiplly '*'</option><option name="Divide">Divide '/'</option></select><br>Number-2<input type="text" name="num_2"><br><hr><input type="submit" value="calculate" name="calculate"><input type="reset"></form><?php include 'nav.php';$multiply = $_POST['multiply'];$divide = $_POST['divide'];$num_1 = $_POST['num_1'];$num_2 = $_POST['num_2'];function calculate() {if(isset($multiply)){echo "The product is:"."($num_1 * $num_2)";}if(isset($divide)){echo "The quotient is:"."($num_1 * $num_2)";}}if(isset($_POST['calculate'])){echo calculate();}?></html>======================0r======================<html><?php include 'nav.php';$multiply = $_POST['multiply'];$divide = $_POST['divide'];$num_1 = $_POST['num_1'];$num_2 = $_POST['num_2'];function calculate() {//if they choose multiplicationif(isset($multiply)){echo "The product is:"."($num_1 * $num_2)";}//if they choose divideif(isset($divide)){echo "The quotient is:"."($num_1 * $num_2)";}}//see if they clicked the sunmit buttonif(isset($_POST['calculate'])){echo calculate();}?><form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">Number-1: <input type="text" name="num_1"><br>Multiply or Divide:<select><option name="multiply">Multiplly '*'</option><option name="Divide">Divide '/'</option></select><br>Number-2<input type="text" name="num_2"><br><hr><input type="submit" value="calculate" name="calculate"><input type="reset"></form></html>================================================+++++++++++++++++++++================================================It gives me notice: undefined index?This post has been edited by masonh928: Today, 04:59 PM

 

calculator.php

Link to comment
Share on other sites

this to be exact

Notice: Undefined index: multiply in /Applications/XAMPP/xamppfiles/htdocs/socialboard/calculator.php on line 4Notice: Undefined index: divide in /Applications/XAMPP/xamppfiles/htdocs/socialboard/calculator.php on line 5Notice: Undefined index: num_1 in /Applications/XAMPP/xamppfiles/htdocs/socialboard/calculator.php on line 6Notice: Undefined index: num_2 in /Applications/XAMPP/xamppfiles/htdocs/socialboard/calculator.php on line 7

Link to comment
Share on other sites

The <option> element only has a value attribute, not a name attribute. The <select> element needs a name in order to be sent to PHP.

<select name="operation">    <option value="multiply">Multiply</option>    <option value="divide">Divide</option></select>

On the server $_POST['operation'] will have the value "multiply" or "divide"

  • Like 1
Link to comment
Share on other sites

do I just delete $divide and $multiply. How is PHP supposed to figure out whether they chose division or multiplication...

Link to comment
Share on other sites

do I just delete $divide and $multiply. How is PHP supposed to figure out whether they chose division or multiplication...

have a drop down, and then an if statement, if($_POST['operation'] == "multiply") { }

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...