<body>
<form>
<input type="text" name="input1" placeholder="number 1" >
<input type="text" name="input2" placeholder="number 2" >
<select name="operator">
<option>none</option>
<option>add</option>
<option>subtract</option>
<option>multiply</option>
<option>divide</option>
</select>
<br>
<button type="submit" name="submit" value="submit">calculate</button>
</form>
<p>the answer is:</p>
<?php
//if this is been set already(the button is hit)
if(isset($_GET['submit']))
{
$result1 = $_GET['input1'];
$result2 = $_GET['input2'];
$operator= $_GET['operator'];
switch($operator){
case "none":
echo "Error";
break;
case "add":
echo $result1 $result2;
break;
case "subtract":
echo $result1 - $result2;
break;
case "multiply":
echo $result1 * $result2;
break;
case "divide":
echo $result1 / $result2;
break;
}
}
?>
</body>
why the above section of code doesn't create any output? I would appreciate it sb explained it to me what the code above lacks or what change is required for the black of code to execute correctly
CodePudding user response:
Try moving your PHP Code above the html tags.
<?php
$answer = 0;
//if this is been set already(the button is hit)
if(isset($_GET['submit']))
{
$result1 = $_GET['input1'];
$result2 = $_GET['input2'];
$operator= $_GET['operator'];
switch($operator){
case "none":
$answer = null;
break;
case "add":
$answer = $result1 $result2;
break;
case "subtract":
$answer = $result1 - $result2;
break;
case "multiply":
$answer = $result1 * $result2;
break;
case "divide":
$answer = $result1 / $result2;
break;
}
}
?>
<body>
<form action=''>
<input type="text" name="input1" placeholder="number 1" >
<input type="text" name="input2" placeholder="number 2" >
<select name="operator">
<option>none</option>
<option>add</option>
<option>subtract</option>
<option>multiply</option>
<option>divide</option>
</select>
<br />
<button type="submit" name="submit" value="submit">calculate</button>
</form>
<p>the answer is: <?= $answer ?></p>
</body>
This should do.
CodePudding user response:
I must have typed "localhost" inside ny browser!