i want to make calculator with php and i wrote this code but doesn't worked whats's the problem ? i use iclude to attach functions and use select and option tag and give options names to get data . i use phpstorm to solve problem but there were no problems . sorry for my bad English
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form style="margin:25%;" action="#" method="GET">
<input type="text" name="num1" size="20" placeholder="number 1">
<input type="text" name="num2" size="20" placeholder="number 2">
<select name="massoud">
<option name="jam1">jam</option>
<option name="tafrigh1">tafrigh</option>
<option name="zarb1">zarb</option>
<option name="taghsim1">taghsim</option>
<option name="baghimande1">baghimande</option>
</select>
<input type="submit" name="submit" value="calculate">
</form>
<?php
include 'calib.php';
?>
<?php
if(isset($_GET['submit']) && isset($_GET['jam1'])) {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo jam1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['tafrigh1'])) {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo tafrigh1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['zarb1'])) {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo zarb1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['taghsim1'])) {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo taghsim1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['baghimande1'])) {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo baghimande1($a1, $b1);
}
?>
</body>
</html>
and this is include file
<?php
function jam1($a1, $b1){
return $a1 + $b1;
}
function tafrigh1($a1, $b1){
return $a1 - $b1;
}
function zarb1($a1, $b1){
return $a1 * $b1;
}
function taghsim1($a1, $b1){
return $a1 / $b1;
}
function baghimande1($a1, $b1){
return $a1 % $b1;
}
?>
this is the result url http://localhost/calib/?num1=6&num2=5&massoud=tafrigh&submit=calculate#
In each case you are asking for the exact value of the option rather than the value of the select which is what is posted:
if(isset($_GET['submit']) && isset($_GET['jam1'])) {
There is no $_GET['jam1']
, you should change this to your select name:
if(isset($_GET['submit']) && ($_GET['massoud'] == 'jam1')) {
You need to add that to each of your cases
Edit: I didnt notice your select is also setup wrong:
<select name="massoud">
<option name="jam1">jam</option>
<option name="tafrigh1">tafrigh</option>
<option name="zarb1">zarb</option>
<option name="taghsim1">taghsim</option>
<option name="baghimande1">baghimande</option>
</select>
You are setting the name of each option rather than the value, change to:
<select name="massoud">
<option value="jam1">jam</option>
<option value="tafrigh1">tafrigh</option>
<option value="zarb1">zarb</option>
<option value="taghsim1">taghsim</option>
<option value="baghimande1">baghimande</option>
</select>
It should be:
<select name="massoud">
<option value="jam1">jam</option>
<option value="tafrigh1">tafrigh</option>
<option value="zarb1">zarb</option>
<option value="taghsim1">taghsim</option>
<option value="baghimande1">baghimande</option>
</select>
And with $_GET['massoud']
you get the selected value.
Change this line
if(isset($_GET['submit']) && isset($_GET['jam1'])) {
to this:
if(isset($_GET['submit']) && isset($_GET['massoud']) && $_GET['massoud'] == 'jam1') {
jam1 is the value not the key. massoud is the name of the select dropdown and therefore the key.
Change your code to this and you will get your output at bottom left corner.
<?php
if(isset($_GET['submit']) && isset($_GET['massoud']) && $_GET['massoud'] == "jam") {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo jam1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['massoud']) && $_GET['massoud'] == "tafrigh") {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo tafrigh1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['massoud']) && $_GET['massoud'] == "zarb") {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo zarb1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['massoud']) && $_GET['massoud'] == "taghsim") {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo taghsim1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['massoud']) && $_GET['massoud'] == "baghimande1") {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo baghimande1($a1, $b1);
}
?>