Reputation: 522
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 ?
Upvotes: 0
Views: 56
Reputation: 667
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":
}
Upvotes: 0
Reputation: 1058
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']
Upvotes: 0
Reputation: 336
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.
Upvotes: 1