0

Hi

I'm been trying to enter numbers into the <input> so I can calculate Add - Subtract - Divide - Multiply.

This is the equations:

<?php
$number = 20;

$number += 10;
print "\$number += 10;<br /> The answer is <b>$number</b>. <br /><br />";

$number -= 12;
print "\$number -= 12;<br /> The answer is <b>$number</b>. <br /><br />";

$number *= 14;
print "\$number *= 14;<br /> The answer is <b>$number</b>. <br /><br />";

$number%= 16;
print "\$number %= 16;<br /> The answer is <b>$number</b>. <br /><br />";
?>

This is my form:

<html>
<body>

<form action="number.php" method="get">

Enter number: <input type="text" name="number" /><br /><br />

Enter number to Add: <input type="text" name="add" /><br /><br />

Enter number to Subtract: <input type="text" name="subtract" /><br /><br />

Enter number to multiply: <input type="text" name="multiply" /><br /><br />

Enter number to divide: <input type="text" name="divide" /><br /><br />

<input type="submit" value="Submit">

</form>

</body>
</html>

This is my number.php

$number = ' ';

$number += ' ';

$number -= ' ';

$number *= ' ';

$number %= ' ';

$number = isset($_GET['number']) ? (float)$_GET['number'] : 0;

This is what I got so far. I'm having problems calculating 4 different operator at once.

Any Suggestions and explanation will help. I appreciate it. Thanks!

3
Contributors
8
Replies
12
Views
5 Years
Discussion Span
Last Post by LastMitch
1

eval() will execute php for you, but this is really unsafe unless you lock down every possible input. Even then, I get really nervous with it.

I'm unclear as to what number.php is trying to show. You're setting number as a space and then you're performing a mathematical operations using a space as the next number?

Confused.

I'm assuming that you're building a calculator. If so, js is a far better vehicle IMO. Involving the server in every calculation may be tedious.

0

@diafol

Thanks for the reply and explanation!

eval() will execute php for you, but this is really unsafe unless you lock down every possible input. Even then, I get really nervous with it.

I'm unclear as to what number.php is trying to show. You're setting number as a space and then you're performing a mathematical operations using a space as the next number?

Confused.

I'm assuming that you're building a calculator. If so, js is a far better vehicle IMO. Involving the server in every calculation may be tedious.

No, It's not a calculator. Sorry for the confusing.

This is the equations is not part of the form and number.php I used this as an outline for me to look at:

<?php

$number = 20;

$number += 10;
print "\$number += 10;<br /> The answer is <b>$number</b>. <br /><br />";

$number -= 12;
print "\$number -= 12;<br /> The answer is <b>$number</b>. <br /><br />";

$number *= 14;
print "\$number *= 14;<br /> The answer is <b>$number</b>. <br /><br />";

$number%= 16;
print "\$number %= 16;<br /> The answer is <b>$number</b>. <br /><br />";

?>

I figure another way. Instead all at once I separate it. I hope this make sense

Edited by LastMitch: grammer

0

This is my form:

<html>
<body>

<form action="number.php" method="get">

Enter number: <input type="text" name="number" /><br /><br />

Enter number to Add: <input type="text" name="add" /><br /><br />

Enter number to Subtract: <input type="text" name="subtract" /><br /><br />

Enter number to multiply: <input type="text" name="multiply" /><br /><br />

Enter number to divide: <input type="text" name="divide" /><br /><br />

<input type="submit" value="Submit">

</form>

</body>
</html>

This my number.php

$number = ' ';
$number = isset($_GET['number']) ? (float)$_GET['number'] : 0;

$add = ' ';
$add = isset($_GET['add']) ? (float)$_GET['add'] : 0;

$subtract = ' ';
$subtract = isset($_GET['subtract']) ? (float)$_GET['subtract'] : 0;

$multiply = ' ';
$multiply = isset($_GET['multiply']) ? (float)$_GET['multiply'] : 0;

$divide = ' ';
$divide = isset($_GET['divide']) ? (float)$_GET['divide'] : 0;

$x = add;
$x = subtract;
$x = multiply;
$x = divide ;

switch ($x) {
    case 1: echo "add.php";
    break;
    case 2: echo "subtract.php";
    break;
    case 3: echo "multiply.php";
    break;
    case 4: echo "divide.php";
    break;
}

My add.php

echo "$number += $add;<br /> The answer is <b>$number</b>. <br />

My subtract.php

echo "$number -= $subtract;<br /> The answer is <b>$number</b>. <br />

My multiply.php

echo "$number *= $multiply;<br /> The answer is <b>$number</b>. <br />

My divide.php

echo "$number %= $divide;<br /> The answer is <b>$number</b>. <br />

OK, My initial plan was to follow the equation all at once but then I realized it will be hard. So I separate each input by using Case. I hope this make sense.

Any Suggestions and explanation will help. I appreciate it. Thanks!

0

I think you've over-engineered this. Can't reply now - going to work, but will come back if nobody else replies.

1

In form.php

<html>
<body>

<form action="number.php" method="post">

Enter number: <input type="text" name="number" /><br /><br />

Enter number to Add: <input type="text" name="add" /><br /><br />

Enter number to Subtract: <input type="text" name="subtract" /><br /><br />

Enter number to multiply: <input type="text" name="multiply" /><br /><br />

Enter number to divide: <input type="text" name="divide" /><br /><br />

<input type="submit" value="Submit">

</form>

</body>
</html>

In number.php

<?php
$number = (isset($_POST['number']) && $_POST['number']!="") ? $_POST['number'] : '0';
print "\$number = ".$number.";<br />";
$number += $_POST['add'];
print "\$number += ".$_POST['add'].";<br /> The answer is <b>$number</b>. <br /><br />";

$number -= $_POST['subtract'];
print "\$number -= ".$_POST['subtract'].";<br /> The answer is <b>$number</b>. <br /><br />";

$number *= $_POST['multiply'];
print "\$number *= ".$_POST['multiply'].";<br /> The answer is <b>$number</b>. <br /><br />";

$number/= $_POST['divide'];
print "\$number /= ".$_POST['divide'].";<br /> The answer is <b>$number</b>. <br /><br />";

?>
Votes + Comments
Thanks for the example!
0

@diafol

I think you've over-engineered this. Can't reply now - going to work, but will come back if nobody else replies.

You might be right about me overthinking a simple example.

0

@Bachov Varghese

Thanks for the reply and example! I will test it.

0

@Bachov Varghese

Thanks for the example! It works!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.