Hi im very new to this stuff. My PHP calculator returns nothing when I hit calculate, when I upload this file to my 000webhost site. I don't have the website up right now but here is my code.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="get" action="#">
Enter the First Number <input type="text" name="number1"> <br><br>
Enter the Second Number <input type="text" name="number2"><br><br>
Choose the operation
Addition <input type="radio" name="op" value="add" checked>
Subtraction <input type="radio" name="op" value="sub">
Multiplication <input type="radio" name="op" value="mul">
Division<input type="radio" name="op" value="div"><br><br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
<?php
if ( $op == "add" ) {
echo "$number1 + $number2 = ";
echo $number1 + $number2;
}
if ( $op == "sub" ) {
echo "$number1 - $number2 = ";
echo $number1 - $number2;
}
if ( $op == "mul" ) {
echo "$number1 * $number2 = ";
echo $number1 * $number2;
}
if ( $op == "div" ) {
echo "$number1 / $number2 = ";
echo $number1 / $number2;
}
?>
<html>
When I hit calculate nothing happens and the link gets a question mark in it. Please help.
You need to use $_GET for accessing input value from get form request
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="get" >
Enter the First Number <input type="text" name="number1"> <br><br>
Enter the Second Number <input type="text" name="number2"><br><br>
Choose the operation
<br>
<input type="radio" name="op" value="add" checked>Addition<br>
<input type="radio" name="op" value="sub">Subtraction<br>
<input type="radio" name="op" value="mul">Multiplication<br>
<input type="radio" name="op" value="div">Division<br><br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
<?php
if (isset( $_GET['number1']) && isset( $_GET['number2']) && isset( $_GET['op'])) {
$number1 = $_GET['number1'];
$number2 = $_GET['number2'];
$op = $_GET['op'];
if ($op == "add") {
echo "$number1 + $number2 = ";
echo $number1 + $number2;
}
if ($op == "sub") {
echo "$number1 - $number2 = ";
echo $number1 - $number2;
}
if ($op == "mul") {
echo "$number1 * $number2 = ";
echo $number1 * $number2;
}
if ($op == "div") {
echo "$number1 / $number2 = ";
echo $number1 / $number2;
}
}
?>
<html>
Related
for example, I receive two inputs value1 and value2 and I want this input for different functions. Like addition, subtraction and multiplication.
my code
<?php
$x = $_POST['fnum'];
$y = $_POST['snum'];
if (isset($_POST['add'])) {
$sum = $x + $y;
echo "Result:<input type='text' value='$sum'/>";
}
if (isset($_POST['sub'])) {
$sub = $x - $y;
echo "Result:<input type='text' value='$sub'/>";
}
if (isset($_POST['mul'])) {
$mul = $x * $y;
echo "Result:<input type='text' value='$mul'/>";
}
<body>
<form method="post">
Enter first number <input type="text" name="fnum" />
<hr />
Enter second number <input type="text" name="snum" />
<hr />
<input type="submit" name="add" value="ADD" />
<input type="submit" name="sub" value="Subtract" />
<input type="submit" name="mul" value="Multiply" />
</form>
</body>
In this it is asking me to feed input for each operation separately
Good, Just use the posted value in your form input like -
Enter first number <input type="text" name="fnum" value="<?php echo #$_POST['fnum'];?>"/><hr/>
Enter second number <input type="text" name="snum" value="<?php echo #$_POST['snum'];?>"/><hr/>
So that user don't need to put the same value again and when press the other buttons the form will automatically submit with the previous values.
Note: Remember, you need to check all the posted value is set or not and use proper conditions of POST method. Have a look at the given example of your problem as solution, I give it here to give you a proper guide.
Example:
<?php
$x = 0;
$y = 0;
if(isset($_POST['submit'])) {
$x = $_POST['fnum'];
$y = $_POST['snum'];
$operator = "";
if($_POST['submit'] == 'ADD') {
$operator = "+";
} elseif($_POST['submit'] == 'Subtract') {
$operator = "-";
} elseif($_POST['submit'] == 'Multiply') {
$operator = "*";
}
$result = $x . $operator . $y;
}?>
Your form will be-
<form method="post">
Enter first number <input type="text" name="fnum" value="<?php echo $x;?>"/><hr/>
Enter second number <input type="text" name="snum" value="<?php echo $y;?>"/><hr/>
<input type="submit" name="submit" value="ADD"/>
<input type="submit" name="submit" value="Subtract"/>
<input type="submit" name="submit" value="Multiply"/>
</form>
Result:
<input type='text' value='<?php echo (isset($result) ? $result : "-";)?>'/>
I'm a student doing a challenge where I would have to make a Calculator-type form where you can input two values and click an operation. It's a simple concept using only 4 operations.
I was able to make the format of the form: title, input text and buttons. But I can't find a way to take the input information and manipulating the information. My initial goal was to take the two values and adding/subtracting/multiplying/dividing the values and printing the statement above.
For example:
1 + 2 = 3 //statement printed above
First Value: 1
Second Value: 2 //insert values
+ - * / //click operation buttons
Any suggestions?
<!DOCTYPE HTML>
<?php
print_r($_POST);
if ( isset($_POST["+"]) ) {
$sum = $var1 + $var2;
echo "$var1 + $var2 = $sum";
}
elseif( isset($_POST["-"]) ) {
$sum = $var1 - $var2;
echo "$var1 - $var2 = $sum";
}
elseif ( isset($_POST["*"]) ) {
$sum = $var1 * $var2;
echo "$var1 * $var2 = $sum";
}
elseif ( isset($_POST["/"]) ) {
$sum = $var1 / $var2;
echo "$var1 / $var2 = $sum";
}
?>
<html>
<body>
<form action="" method="POST">
First Value: <input type="text" name="First Value"><br><br>
Second Value: <input type="text" name="Second Value"><br><br>
Operations: <button type="submit" name "+">+</button>
<button type="submit" name "-">-</button>
<button type="submit" name "*">*</button>
<button type="submit" name "/">/</button>
</form>
</body>
</html>
This seems to fix the errors I mentioned in comments
<!DOCTYPE HTML>
<html>
<body>
<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST'){
if ( isset($_POST["+"]) ) {
$sum = $_POST['var1'] + $_POST['var2'];
echo "$_POST[var1] + $_POST[var2] = $sum";
}
elseif( isset($_POST["-"]) ) {
$sum = $_POST['var1'] - $_POST['var2'];
echo "$_POST[var1] - $_POST[var2] = $sum";
}
elseif ( isset($_POST["*"]) ) {
$sum = $_POST['var1'] * $_POST['var2'];
echo "$_POST[var1] * $_POST[var2] = $sum";
}
elseif ( isset($_POST["/"]) ) {
$sum = $_POST['var1'] / $_POST['var2'];
echo "$_POST[var1] / $_POST[var2] = $sum";
}
}
?>
<form action="" method="POST">
First Value: <input type="text" name="var1"><br><br>
Second Value: <input type="text" name="var2"><br><br>
Operations: <button type="submit" name="+">+</button>
<button type="submit" name="-">-</button>
<button type="submit" name="*">*</button>
<button type="submit" name="/">/</button>
</form>
</body>
</html>
I don't know if it's the right way to do things but you can use jquery in the script section to do so, let's say you have this form
<form id="target" action="destination.html">
<input type="text" value="Hello there">
<input type="submit" value="Go">
</form>
so on the submit you do the math
$( "#target" ).submit(function( event ) {
//do the math//
document.write(//your answer//);
event.preventDefault();
});
<?php
$first_num = $_POST['first_num'];
$second_num = $_POST['second_num'];
$operator = $_POST['operator'];
$result = '';
if (is_numeric($first_num) && is_numeric($second_num)) {
switch ($operator) {
case "Add":
$result = $first_num + $second_num;
break;
case "Subtract":
$result = $first_num - $second_num;
break;
case "Multiply":
$result = $first_num * $second_num;
break;
case "Divide":
$result = $first_num / $second_num;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="page-wrap">
<h1>Simple Calculator Program</h1>
<form action="" method="post" id="quiz-form">
<p>
<label>First Number</label>
<input type="number" name="first_num" id="first_num" required="required" value="<?php echo $first_num; ?>" />
</p>
<p>
<label>Second Number</label>
<input type="number" name="second_num" id="second_num" required="required" value="<?php echo $second_num; ?>" />
</p>
<p>
<label>Result</label>
<input readonly="readonly" name="result" value="<?php echo $result; ?>">
</p>
<input type="submit" name="operator" value="Add" />
<input type="submit" name="operator" value="Subtract" />
<input type="submit" name="operator" value="Multiply" />
<input type="submit" name="operator" value="Divide" />
</form>
</div>
</body>
</html>
I'm new to PHP. I have a doubt about posting values of checkbox items to another page as an array. Can anyone tell me a solution for this.
<form method="get">
<input type="checkbox" name="options[]" value="Politics"/> Politics<br/>
<input type="checkbox" name="options[]" value="Movies"/> Movies<br/>
<input type="checkbox" name="options[]" value="World "/> World<br/>
<input type="submit" value="Go!" />
</form>
and the php code is
<?php
$checked = $_GET['options'];
for($i=0; $i < count($checked); $i++)
{
echo "Selected " . $checked[$i] . "<br/>";
}
?>
Before doing php operation, check whether form is submitted and options[] value is set. If it is then iterate through array as you have [] as name.
Try this:
<?php
if (isset($_REQUEST['submit']) && $_REQUEST['submit'] !== null) {
if (isset($_REQUEST['options'])) {
$options = $_REQUEST['options'];
$str = '';
for ($i = 0; $i < count($options); $i++) {
$str .= "Selected " . $options[$i] . "<br/>";
}
echo $str;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>
Testing
</title>
<script src="js/jquery.min.js"></script>
</head>
<body>
<form method="get" name="form">
<input type="checkbox" name="options[]" value="Politics"/>
Politics
<br/>
<input type="checkbox" name="options[]" value="Movies"/>
Movies
<br/>
<input type="checkbox" name="options[]" value="World "/>
World
<br/>
<input type="submit" name="submit" value="Go"/>
</form>
<script>
</script>
</body>
</html>
the action/answer can also be on different page.(as i have done)
Index Page:
<!DOCTYPE html>
<html>
<head>
<title>
Testing
</title>
<script src="js/jquery.min.js"></script>
</head>
<body>
<form method="get" action="display.php">
<input type="checkbox" name="options[]" value="Politics"/>
Politics
<br/>
<input type="checkbox" name="options[]" value="Movies"/>
Movies
<br/>
<input type="checkbox" name="options[]" value="World "/>
World
<br/>
<input type="submit" name="submit" value="Go"/>
</form>
<script>
</script>
</body>
</html>
Display Page:
<?php
if (isset($_REQUEST['submit']) && $_REQUEST['submit'] !== null) {
if (isset($_REQUEST['options'])) {
$options = $_REQUEST['options'];
$str = '';
for ($i = 0; $i < count($options); $i++) {
$str .= "Selected " . $options[$i] . "<br/>";
}
echo $str;
}
}
?>
<form method="post">//changed get to post
<input type="checkbox" name="options[]" value="Politics"/> Politics<br/>
<input type="checkbox" name="options[]" value="Movies"/> Movies<br/>
<input type="checkbox" name="options[]" value="World "/> World<br/>
<input type="submit" value="Go!" />
</form>
<?php
$checked = $_POST['options'];//changed get to post
for($i=0; $i < count($checked); $i++)
{
echo "Selected " . $checked[$i] . "<br/>";
}
?>
Please add action attribute in your form tag for redirecting on another page then post your following code on that page.
<?php
$checked = $_GET['options'];
for($i=0; $i < count($checked); $i++)
{
echo "Selected " . $checked[$i] ;
}
?>
I want to write a php code that displays a random image and the person has to guess what it is. The problem is my php code tells me that the right city is incorrect, i cant figure out why
<?php
$random = rand(1, 3);
$answer ;
if ($random == 1) { $answer = "chicago" ;}
elseif($random == 2) {$answer = "LA" ;}
elseif($random == 3) {$answer = "NewYork" ;}
else {echo "Answer is None" ; }
if (isset($_POST["choice"])) {
$a = $_POST["choice"];
if($a ==$answer){
echo "<br> working <br>" ;
}else {echo "<br> Not Working <br>";}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>City Guess</title>
</head>
<body>
<center>
<img src="four/Image_<?php echo $random ;?>.jpg" width="960" height="639" alt=""/>
<form action="" method="POST">
chicago <input type= "radio" name="choice" value="chicago" />
<br>
LA <input type= "radio" name="choice" value="LA" />
<br>
NewYork <input type= "radio" name="choice" value="NewYork" />
<br>
<input type ="submit" value="Submit" />
</form>
</center>
</body>
</html>
You're generating your random value EVERYTIME the page is loaded. e.g. when the person first visits it and gets the form, you generate 2. When the form is submitted, you generate ANOTHER value, and this time it's 3. So even though the user correctly answers 2, you say they're wrong because the you've forgotten what you originally asked.
You need to store the generated value somehow, and compare that to the response, e.g.
$rand = rand(1,3);
<input type="hidden" name="correct_answer" value="$rand">
if ($_SERVER["REQUEST_METHOD"] == 'POST') {
if ($_POST['correct_answer'] == $_POST['user_answer']) {
echo 'correct';
}
}
The problem is your logic, you do not store the generated number that is used to show the image, instead you generate a new number every time you open the page.
You should store the generated number in (for example...) a session to preserve it and use that to compare.
Try it:
$random = floor(rand(1, 4));
I hope it helps.
Add the following hidden field in your form, using value="<?php echo $answer ?>" and it will work with your present code.
<input type="hidden" name="correct_answer" value="<?php echo $answer ?>">
Matter 'o fact, you don't even need name="correct_answer"
Just do:
<input type="hidden" value="<?php echo $answer ?>">
Remember when you test this, you have a "1 in 3" chance for success!
Edit
Here's the code that I used:
<?php
$random = rand(1, 3);
$answer;
if ($random == 1) { $answer = "chicago" ;}
elseif($random == 2) {$answer = "LA" ;}
elseif($random == 3) {$answer = "NewYork" ;}
else {echo "Answer is None" ; }
if (isset($_POST["choice"])) {
$a = $_POST["choice"];
if($a ==$answer){
echo "<br> working <br>" ;
}else {echo "<br> Not Working <br>";}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>City Guess</title>
</head>
<body>
<center>
<img src="four/Image_<?php echo $random ;?>.jpg" width="960" height="639" alt=""/>
<form action="" method="POST">
<input type="hidden" value="<?php echo $answer ?>">
chicago <input type= "radio" name="choice" value="chicago" />
<br>
LA <input type= "radio" name="choice" value="LA" />
<br>
NewYork <input type= "radio" name="choice" value="NewYork" />
<br>
<input type ="submit" value="Submit" />
</form>
</center>
</body>
</html>
I'm creating a basic PHP calculator that lets you enter two values and chose your operator then displays the answer. Everything is working fine except it's not outputting the answer to the browser.
Here are the codes for my html and PHP files:
<head>
<meta charset="utf-8">
<title>Calculator</title>
</head>
<body>
<form method="post" attribute="post" action="disp_form.php">
<p>First Value:<br/>
<input type="text" id="first" name="first"></p>
<p>Second Value:<br/>
<input type="text" id="second" name="second"></p>
<input type="radio" name="group1" id="add" value="add" checked="true"><p>+</p><br/>
<input type="radio" name="group1" id="subtract" value="subtract"><p>-</p><br/>
<input type="radio" name="group1" id="times" value="times"><p>x</p><br/>
<input type="radio" name="group1" id="divide" value="divide"><p>/</p><br/>
<p></p>
<button type="submit" name="answer" id="answer" value="answer">Calculate</button>
</form>
</body>
</html>
PHP file:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
if($_POST['group1'] == add) {
echo "$first + $second";
}
else if($_POST['group1'] == subtract) {
echo "$first - $second";
}
else if($_POST['group1'] == times) {
echo "$first * $second";
}
else($_POST['group1'] == divide) {
echo "$first / $second";
}
?>
</p>
</body>
</html>
<?php
$result = "";
class calculator
{
var $a;
var $b;
function checkopration($oprator)
{
switch($oprator)
{
case '+':
return $this->a + $this->b;
break;
case '-':
return $this->a - $this->b;
break;
case '*':
return $this->a * $this->b;
break;
case '/':
return $this->a / $this->b;
break;
default:
return "Sorry No command found";
}
}
function getresult($a, $b, $c)
{
$this->a = $a;
$this->b = $b;
return $this->checkopration($c);
}
}
$cal = new calculator();
if(isset($_POST['submit']))
{
$result = $cal->getresult($_POST['n1'],$_POST['n2'],$_POST['op']);
}
?>
<form method="post">
<table align="center">
<tr>
<td><strong><?php echo $result; ?><strong></td>
</tr>
<tr>
<td>Enter 1st Number</td>
<td><input type="text" name="n1"></td>
</tr>
<tr>
<td>Enter 2nd Number</td>
<td><input type="text" name="n2"></td>
</tr>
<tr>
<td>Select Oprator</td>
<td><select name="op">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value=" = "></td>
</tr>
</table>
</form>
Personally I would do a switch instead of all this if, else if, else
$first = $_POST['first'] + 0;//a small "hack" to make sure its an int but allow negs!!
$second= $_POST['second'] + 0;
$operator = $_POST["group1"];
switch($operator)
{
case "add"
echo "Answer is: " .$first + $second;
break;
case "subtract"
echo "Answer is: " .$first - $second;
break;
case "times"
echo "Answer is: " .$first * $second;
break;
case "divide"
echo "Answer is: " .$first / $second;
break;
}
You need to assign $first and $second
$first = $_POST['first'];
$second= $_POST['second'];
Also, As Travesty3 said, you need to do your arithmetic outside of the quotes:
echo $first + $second;
You also need to put the [== 'add'] math operation into quotes
if($_POST['group1'] == 'add') {
echo $first + $second;
}
complete code schould look like that :
<?php
$first = $_POST['first'];
$second= $_POST['second'];
if($_POST['group1'] == 'add') {
echo $first + $second;
}
else if($_POST['group1'] == 'subtract') {
echo $first - $second;
}
else if($_POST['group1'] == 'times') {
echo $first * $second;
}
else if($_POST['group1'] == 'divide') {
echo $first / $second;
}
?>
You need to get the values the same way to get the calculator operation which looks like:
<?php
if($_POST['group1'] == add) {
echo "$_POST['first']+ $_POST['second'];
}
... and so on
?>
Or, to make it easier, just do:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
$first = $_POST['first'];
$second= $_POST['second'];
if($_POST['group1'] == add) {
echo "$first + $second";
}
else if($_POST['group1'] == subtract) {
echo "$first - $second";
}
else if($_POST['group1'] == times) {
echo "$first * $second";
}
else($_POST['group1'] == divide) {
echo "$first / $second";
}
?>
</p>
</body>
</html>
$first = doubleval($_POST['first']);
$second = doubleval($_POST['second']);
if($_POST['group1'] == 'add') {
echo "$first + $second = ".($first + $second);
}
// etc
Check string using single quotes
Ex. $_POST['group1'] == 'add'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Calculator</title>
</head>
<body>
HTML Code is here:
<form method="post">
<input type="text" name="numb1">
<input type="text" name="numb2">
<select name="operator" id="">
<option>None</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
<option>Square</option>
</select>
<button type="submit" name="submit" value="submit">Calculate</button>
</form>
PHP Code:
<?php
if (isset($_POST['submit'])) {
$result1 = $_POST['numb1'];
$result2 = $_POST['numb2'];
$operator = $_POST['operator'];
switch ($operator) {
case 'None':
echo "You need to select any operator";
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;
case 'Square':
echo $result1 ** $result2;
break;
}
}
?>
enter code here
</body>
</html>
Make this changes in php file
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
$first = $_POST['first'];
$second= $_POST['second'];
if($_POST['group1'] == 'add') {
echo $first + $second;
}
else if($_POST['group1'] == 'subtract') {
echo $first - $second;
}
else if($_POST['group1'] == 'times') {
echo $first * $second;
}
else if($_POST['group1'] == 'divide') {
echo $first / $second;
}
else {
echo "Enter the numbers properly";
}
?>
</p>
</body>
</html>
<?php
$cal1= $_GET['cal1'];
$cal2= $_GET['cal2'];
$symbol =$_GET['symbol'];
if($symbol == '+')
{
$add = $cal1 + $cal2;
echo "Addition is:".$add;
}
else if($symbol == '-')
{
$subs = $cal1 - $cal2;
echo "Substraction is:".$subs;
}
else if($symbol == '*')
{
$mul = $cal1 * $cal2;
echo "Multiply is:".$mul;
}
else if($symbol == '/')
{
$div = $cal1 / $cal2;
echo "Division is:".$div;
}
else
{
echo "Oops ,something wrong in your code son";
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Calculator</title>
</head>
<body>
<form method="GET">
<h1>Calculator</h1>
<p>This is a calculator made by Hau Teen Yee Fabrice and is free to use.</p>
<ul>
<ol> Multiplication: * </ol>
<ol> Substraction: - </ol>
<ol> Division: / </ol>
<ol> Addition: + </ol>
</ul>
<p>Enter first number:</p>
<input type="text" name="cal1">
<p>Enter second number:</p>
<input type="text" name="cal2">
<p>Enter the calculator symbol</p>
<input type="text" name="symbol">
<button>Calculate</button>
</form>
<?php
$cal1= $_GET['cal1'];
$cal2= $_GET['cal2'];
$symbol =$_GET['symbol'];
if($symbol == '+')
{
$add = $cal1 + $cal2;
echo "Addition is:".$add;
}
else if($symbol == '-')
{
$subs = $cal1 - $cal2;
echo "Substraction is:".$subs;
}
else if($symbol == '*')
{
$mul = $cal1 * $cal2;
echo "Multiply is:".$mul;
}
else if($symbol == '/')
{
$div = $cal1 / $cal2;
echo "Division is:".$div;
}
else
{
echo "Oops ,something wrong in your code son";
}
?>
</body>
</html>