Simple Calculator using radio button in php - php

i made a simple calculator.But the problem is not radio buttons are not working properly to select the operation to be performed my code for input is as follow:
<form action="output.php" method="post" class="form">
<label for="">Value 1</label>
<input type="text" name="value1" placeholder="Enter 1st value">
<br>
<label for="">Value 2</label>
<input type="text" name="value2" placeholder="Enter 2nd value">
<h1>Select Operator </h1>
<input type="radio" name="addition" value="add">+
<br>
<input type="radio" name="subtraction" value="sub">-
<br>
<input type="radio" name="multiplication" value="mul">*
<br>
<input type="radio" name="division" value="div">/
<br>
<input type="submit" class="btn btn-success" value="Show Result">
</form>
and output php code is as follow:
<?php
$value_1=$_POST['value1'];
$value_2=$_POST['value2'];
if(isset($_POST['submit'])) {
if($_POST['operation'] == add) {
echo "$value_1 + $value_2";
}else if($_POST['operation'] == sub){
echo "$value_1 - $value_2";
}else if($_POST['operation'] == mul){
echo "$value_1 * $value_2";
}else if($_POST['operation'] == div){
echo "$value_1 / $value_2";
}
}
?>

The radio buttons need to share the same name, only the value should change.
label {
display: block;
}
<label><input type="radio" name="operation" value="add">+</label>
<label><input type="radio" name="operation" value="sub">-</label>
<label><input type="radio" name="operation" value="mul">*</label>
<label><input type="radio" name="operation" value="div">/</label>

The submit button has no name - so it will not appear in the POST array and the logic will never be processed. Rather than test for the presence ( or not ) of a button in the POST array you really should test for the actual form elements that you will use in your calculations.
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>simple calculator</title>
</head>
<body>
<form method="post" class="form">
<label>Value 1 <input type="text" name="value1" placeholder="Enter 1st value"></label>
<label>Value 2<input type="text" name="value2" placeholder="Enter 2nd value"></label>
<h1>Select Operator </h1>
<input type="radio" name="operator" value="add">+
<br>
<input type="radio" name="operator" value="sub">-
<br>
<input type="radio" name="operator" value="mul">*
<br>
<input type="radio" name="operator" value="div">/
<br>
<input type="submit" class="btn btn-success" value="Show Result">
</form>
<?php
/*
Ensure that ALL post fields are set
*/
if( isset(
$_POST['value1'],
$_POST['value2'],
$_POST['operator']
)) {
/*
Assign POST values as variables.
*/
$value_1=(int)$_POST['value1'];
$value_2=(int)$_POST['value2'];
$operator=$_POST['operator'];
/*
Perform the relevant calculation
and assign the correct symbol for display
*/
switch( $operator ){
case 'add':$symbol='+';$res=$value_1 + $value_2;break;
case 'sub':$symbol='-';$res=$value_1 - $value_2;break;
case 'mul':$symbol='*';$res=$value_1 * $value_2;break;
case 'div':$symbol='/';$res=$value_1 / $value_2;break;
}
/*
print the sum and result
*/
printf(
'%d %s %s=%d',
$value_1,
$symbol,
$value_2,
$res
);
}
?>
</body>
</html>

Related

How do i receive the right Value

The Counter wont go up. As you see should the Counter go up if the PHP Code receive the A Value.
html:
<body>
<div>
<div>
<h1>Login</h1>
<form method="post" action="quiz.php">
<input name="A" value="A" type="radio">
<label for="A">A</label>
<input name="A" value="B" type="radio">
<label for="A">B</label>
<input name="A" value="C" type="radio">
<label for="A">C</label>
<button id="submitbutton"
type="submit">Antworten</button>
</form>
</div>
</div>
</body>
<?php
if(!empty($_POST)) {
if(!empty($_POST['A'])) {
$counter = $counter + 1;
}
}
echo $counter;
?>
Sometimes i get HTTP ERROR 412

How to differentiate name in PHP

I have a code. Somehow, it only able to pick the last hidden input name field instead of the other one. I also tried use if and else but nothing is displayed. Please advise.
Without the if else cases scenario:
HTML:
<div class="tab-label">
<input type="radio" id="ldktech_product_good" name="conditition" value="good" >
<input type="hidden" id="ldktech_product_price_good" name="price" value="7.50">
<label for="ldktech_product_good">Good</label>
NOTE: Please include the charger with your iPad trade-in, or a replacement fee will be deducted from the offer
<div class="tab-label">
<input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" >
<input type="hidden" id="ldktech_product_price_flawless" name="price" value="10">
<label for="ldktech_product_flawless">Flawless</label>
PHP:
$condition = $_POST["conditition"];
$price = $_POST["price"];
echo $price;
echo "<br>";
echo $condition;
With the if else scenarios:
HTML Code:
<div class="tab-label">
<input type="radio" id="ldktech_product_good" name="conditition" value="good" >
<input type="hidden" id="ldktech_product_price_good" name="price-good" value="7.50">
<label for="ldktech_product_good">Good</label>
<div class="tab-label">
<input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" >
<input type="hidden" id="ldktech_product_price_flawless" name="price-flawless" value="10">
<label for="ldktech_product_flawless">Flawless</label>
PHP code:
$condition = $_POST["conditition"];
if($condition == "good"){
$price = $_POST["price-good"];}
else if ($condition == "flawless"){
$price = $_POST["price-flawless"];}
echo $price;
echo "<br>";
echo $condition;
Nothing work. Please advised
works fine for me. don't know if your form method is post or not or your action url is set to your script url or if your script is on the same page make sure you place it accordingly.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PhpFiddle Initial Code</title>
</head>
<body>
<div style="margin: 30px 10%;">
<h3>My form</h3>
<form id="myform" name="myform" method="post">
<div class="tab-label">
<input type="radio" id="ldktech_product_good" name="conditition" value="good" >
<input type="hidden" id="ldktech_product_price_good" name="price-good" value="7.50">
<label for="ldktech_product_good">Good</label>
<div class="tab-label">
<input type="radio" id="ldktech_product_flawless" name="conditition" value="flawless" >
<input type="hidden" id="ldktech_product_price_flawless" name="price-flawless" value="10">
<label for="ldktech_product_flawless">Flawless</label> <br /><br />
<button id="mysubmit" type="submit">Submit</button><br /><br />
</form>
</div>
<?php
if(isset($_POST["conditition"])){
$condition = $_POST["conditition"];
if($condition == "good"){
$price = $_POST["price-good"];
}
else if ($condition == "flawless"){
$price = $_POST["price-flawless"];
}
echo $price;
echo "<br>";
echo $condition;
}
?>
</body>
</html>
I believe this is what you're trying to do?
EDIT:
<form method="post">
<div class="tab-label">
<input type="radio" name="condition1" value="good">
<input type="hidden" name="price1" value="7.50">
<label for="ldktech_product_good">Good</label>
</div>
<div class="tab-label">
<input type="radio" name="condition2" value="flawless">
<input type="hidden" name="price2" value="10">
<label for="ldktech_product_flawless">Flawless</label>
</div>
<input type="submit" name="submit" value="Get records">
</form>
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['condition1']) && $_POST['condition1'] == "good") {
echo "You have selected :".$_POST['price1']; // Displaying Selected Value
} else if (isset($_POST['condition2']) && $_POST['condition2'] == "flawless") {
echo "You have selected :".$_POST['price2']; // Displaying Selected Value
}
}
?>

get form values from one php "echo" to the other

i want get the values of form in other php file but i am getting only one input value as the name are same in loop....How to get all the input values with the same name or if i have to change the name tell me how? and how to get it in other php file
<?php
if(isset($_POST['submit'])){
//getting values of number of MCQ questions and true/false questions
$mcq = $_POST['mcq'];
$truefalse = $_POST['truefalse'];
$number = 1;
echo '<form action="finalquestions.php" method="post">';
//loop to get inputs of mcq as many as the user posted
for($i=1;$i<=$mcq;$i++){
echo 'Question:'.$number.'<input type="text" name="question1" /> </br></br>';
echo '<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>';
echo '</br></br>';
$number++;
}
//loop to get inputs of truefalse as many as the user posted
for($i=1;$i<=$truefalse;$i++){
echo 'Question:'.$number.' <input type="text" name="question2" /> </br></br>';
echo '<input type="radio" name="question2">True</br>
<input type="radio" name="question1">False</br>';
echo '</br></br>';
$number++;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<input type="submit" name="submit" value="Generate Quiz" />
</form>
</body>
</html>

How to display correct answer in different color than the rest?

I am working on a quiz building application that will let a user enter a quiz question with four possible answers. They must select which answer will be correct with a radio button. I then need to display the answers in a random order with the correct one being a different color than the rest. Currently I able to display the answers in a random order, but I don't know how to make the answer that was selected with a radio button a different color. Here is what it looks like...
And this is the result page...
As you can see, the results are displayed in random order, which is what I want. How can I change the color of "Albany" on the result page to show that it is the correct answer?
Here is my form code....
<form style="text-align:center" action="QuestionReview.php" method="POST">
<br>
<label class="instructions" for="question" >Enter your question here</label><br>
<textarea name="question" rows="5" cols="40"></textarea>
<br><br>
<p class="instructions">
Please provide four answers to your question and select the
correct one.
</p>
<input type="radio" name="selection" value="answerA">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answerB">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answerC">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answerD">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="submit" value="Submit Entry">
</form>
And here is the result page.....
<?php
// Retrieve the question and answers from the HTML form
$question = $_POST['question'];
$answers = $_POST['answer'];
$selection = $_POST['selection'];
shuffle($answers);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Entry Review</title>
<style>
.instructions {
color: #696D6E;
}
</style>
</head>
<body>
<h1 class="instructions">Entry Review</h1>
<p><em>You entered the following question:</em></p>
<p><strong><?php echo $question; ?></strong></p><br>
<p><em>These are the answers you provided:</em>
<p>
<strong>
<?php
foreach($answers as $value) {
echo $value . '<br>';
}
?>
</strong>
</p>
</body>
</html>
You could wrap each answer in <div>-s and add style to them only if they were selected.
It is a good idea to change input field names in your form from selection and answer[] to answer[selected] and answer[body] respectively. This allows you to group together the body and the correctness indicator of an answer. Now you can modify your foreach in the following way:
<?php
foreach($answers as $answer){
// Print div with style, if this answer is correct
$answer['selected'] ? print '<div style="color:green;">' : print '<div>';
echo $answer['body']
echo '</div>'
}
?>
Form
<input type="radio" name="answer[0][selected]" value="true">
<input type="text" name="answer[0][body]" style="width:400px">
<br><br>
<input type="radio" name="answer[1][selected]" value="true">
<input type="text" name="answer[1][body]" style="width:400px">
<br><br>
<input type="radio" name="answer[2][selected]" value="true">
<input type="text" name="answer[2][body]" style="width:400px">
<br><br>
<input type="radio" name="answer[3][selected]" value="true">
<input type="text" name="answer[3][body]" style="width:400px">
<br><br>
PHP Code
<?php
// Retrieve the question and answers from the HTML form
$question = $_POST['question'];
$answers = $_POST['answer'];
shuffle($answers);
?>
// . . .
<?php
foreach($answers as $answer){
// Print div with style, if this answer is correct
$answer['selected'] ? print '<div style="color:green;">' : print '<div>';
echo $answer['body']
echo '</div>'
}
?>
P.S
Make sure to escape the user passed data before you use it!
You can change your input fields as below:
<input type="radio" name="selection" value="answer0">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answer1">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answer2">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="radio" name="selection" value="answer3">
<input type="text" name="answer[]" style="width:400px">
<br><br>
<input type="submit" value="Submit Entry">
Then you can modify your PHP code(foreach part) as below:
<?php
foreach($answers as $key=>$value) {
if($selection == "answer$key"){
echo "<span style='color:green'>".$value . "</span><br>";
}else{
echo $value."<br>";
}
}
?>

How to rank (prioritize) values from numbered checkbox form in PHP

I am using the code below to ask users to rank which programming language they are more comfortable with.
The users need to rank from 1-3 (1 being the one they are most comfortable with)
<form id="form1" name="form1" method="post" action="">
<input type="number" name="php" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="python" required="required" max="3" min="1"/>Python <br />
<input type="number" name="ruby" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
Once the user prioritizes the programming languages and hits submit, how can I on the next page echo the ranking selection? (e.g. Your first choice is x, your second choice is y and your third choice is z)
I would do it like so (Note that I've changed the the value of the name attributes on the form elements):
<form id="form1" name="form1" method="post" action="">
<input type="number" name="lang[php]" required="required" max="3" min="1"/>PHP <br />
<input type="number" name="lang[python]" required="required" max="3" min="1"/>Python <br />
<input type="number" name="lang[ruby]" required="required" max="3" min="1"/>Ruby <br /><br />
<input type="submit" name="button" id="button" value="Submit" />
</form>
And in the php:
//Get the form results (which has been converted to an associative array) from the $_POST super global
$langs = $_POST['lang'];
//Sort the values by rank and keep the key associations.
asort($langs, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($langs as $lang => $rank)
{
//echo out here first, second, and third rank with each iteration respectively.
}
The asort function simply sorts the array by value while maintaining key association.
I am not sure that tag input type="number" exists.
you do better
<legend>
<label><input type="radio" name="php" value="1">1</label>
<label><input type="radio" name="php" value="2">2</label>
<label><input type="radio" name="php" value="3">3</label>
</legend>
<legend>
<label><input type="radio" name="python" value="1">1</label>
<label><input type="radio" name="python" value="2">2</label>
<label><input type="radio" name="python" value="3">3</label>
</legend>
you must not use 'required' attribute for radio tag or checkbox tag
so you make a check javascript function whether radio box is checked or not.
<form name..... onsubmit = "return check_submit();">
<script>
var check_submit = function(){
if($("input[name=php]:checked").val() =="")
return false;
...
return true;
}
</script>
or you can use
<input type="text" name="php">
then on next page you can do like this
$php = intval(trim($_POST['php']));
$python = intval(trim($_POST['python']));
$msg = "your first choice for php is '.$php;
$msg.="your second choice for phthon is '.$python;
.....etc..

Categories