I don't know why it cannot show the result. I have already put isset() Function on it, but the echo string is not come out.
<form method="POST">
<input type="radio" name="colors" value="red" checked="true">
<label id="r">Red</label>
<input type="radio" name="colors" value="green">
<label id="g">Green</label>
<input type="button" name="add" value="Add">
<input type="button" name="clear" value="Clear">
</form>
<?php
if (isset($_POST['add'])) {
if (isset($_POST['colors'])) {
$colorVal = $_POST['colors'];
echo "$colorVal";
}
}
?>
The type of your Add button must be submit also the Clear button has to be reset.
By the way that $POST['colors'] needs to be $_POST['colors'].
<form method="POST">
<input type="radio" name="colors" value="red" checked="true">
<label id="r">Red</label>
<input type="radio" name="colors" value="green">
<label id="g">Green</label>
<input type="submit" name="add" value="Add">
<input type="reset" name="clear" value="Clear">
</form>
<?php
if (isset($_POST['add'])) {
if (isset($_POST['colors'])) {
$colorVal = $_POST['colors'];
echo "$colorVal";
}
}
?>
Update: (the reset button works as expected)
<form method="POST">
<input type="radio" name="colors" value="red" checked="true">
<label id="r">Red</label>
<input type="radio" name="colors" value="green">
<label id="g">Green</label>
<input type="submit" name="add" value="Add">
<input type="reset" name="clear" value="Clear">
</form>
<form method="POST">
<input type="radio" name="colors" value="red" checked="true">
<label id="r">Red</label>
<input type="radio" name="colors" value="green">
<label id="g">Green</label>
<input type="submit" name="add" value="Add">
<input type="reset" name="clear" value="Clear">
</form>
<?php
if(isset($_POST['add']))
{
if(isset($_POST['colors']))
{
$colorVal = $_POST['colors'];
echo "$colorVal";
}
}
?>
Defines a submit button (for submitting the form) by making its type as SUBMIT
<input type="submit">
The <input type="button"> defines a clickable button (mostly used with
a JavaScript to activate a script).
Also, you have a syntax error for $_POST
Related
i m using isset to get the button value clicked using hidden type...below is my code
<form id="posform" name="posform" action="" method="POST" >
<div><input type="hidden" name="updatepos" value="1" /><input type="submit" value="Update" ></div>
<div><input type="hidden" name="updatepos" value="2" /><input type="submit" value="Update" ></div>
<div><input type="hidden" name="updatepos" value="3" /><input type="submit" value="Update" ></div>
<div><input type="hidden" name="updatepos" value="4" /><input type="submit" value="Update" ></div>
</form>
if (isset($_POST['updatepos'])){
$targetbtnvalue=$_POST['updatepos'];
echo $targetbtnvalue;
}
but with below code irrespective of the button clicked it always echos the last button value i.e 4.
i am trying to get it to echo the corresponding value of button clicked...plz guide
That's because it's the same form with the same field.
Try this instead:
<form name="posform" action="" method="POST" >
<div><input type="hidden" name="updatepos" value="1" /><input type="submit" value="Update" ></div>
</form>
<form name="posform" action="" method="POST" >
<div><input type="hidden" name="updatepos" value="2" /><input type="submit" value="Update" ></div>
</form>
<form name="posform" action="" method="POST" >
<div><input type="hidden" name="updatepos" value="3" /><input type="submit" value="Update" ></div>
</form>
<form name="posform" action="" method="POST" >
<div><input type="hidden" name="updatepos" value="4" /><input type="submit" value="Update" ></div>
</form>
All of your hidden input names are same i.e updatepos which shouldn't be. So you can try with different names for hidden inputs or make it array like this way updatepos[]. Then you'll get all the hidden input values on your $_POST array not just the last one.
form id="posform" name="posform" action="" method="POST" >
<div><input type="hidden" name="updatepos[]" value="1" /><input type="submit" value="Update" ></div>
<div><input type="hidden" name="updatepos[]" value="2" /><input type="submit" value="Update" ></div>
<div><input type="hidden" name="updatepos[]" value="3" /><input type="submit" value="Update" ></div>
<div><input type="hidden" name="updatepos[]" value="4" /><input type="submit" value="Update" ></div>
</form>
and then with PHP try like this,
if (isset($_POST['updatepos'])){
$targetbtnvalue=$_POST['updatepos'];
print '<pre>';
print_r($targetbtnvalue);
print '</pre>';
}
HTML CODE :
<div style="padding-left:40px">
<h2> PHP Calculator</h2>
<form method="post" action="">
Enter value: <input type="text" name="value"> <br> <br>
<div style="padding-left: 105px">
<input type="submit" value="9" name="button">
<input type="submit" value="8" name="button">
<input type="submit" value="7" name="button">
<input type="submit" value="+" name="button"> <br>
<input type="submit" value="6" name="button">
<input type="submit" value="5" name="button">
<input type="submit" value="4" name="button">
<input type="submit" value="-" name="button" style="padding-left: 9px"> <br>
<input type="submit" value="3" name="button">
<input type="submit" value="2" name="button">
<input type="submit" value="1" name="button">
<input type="submit" value="/" name="button" style="padding-left: 9px"> <br>
<input type="submit" value="0" name="button" style="padding- left:33px">
<input type="submit" value="." name="button" style="padding-right:9px">
<input type="submit" value="x" name="button" style="padding left: 7px"><br>
</div> <br>
<input type="submit" value="Calculate" name="Calculator">
</form>
</div>
PHP CODE :
<?php
if (isset($_POST["Calculate"]))
{
echo $_POST["button"];
} ?>
I'm developing a PHP calculator, actually my question is when i click to any of the input buttons the value should display in the text box. above is the code what i have tried with, but im unable to get the result. can any one help me in my work,so that how i can get the desired result.
There is nothing like you cannot do in PHP but if you could do it using Jquery it will be much more better in terms of User experience as well. Looking at your HTML , I can see you have a input type of submit which means on each click it will submit a form unless and until you have written any condition not to submit. Also your name for each button is also same and it will make difficult to track which one is clicked/entered, please make sure you have unique name field. If you want to do it in PHP only then on every submit you can save that value in session or cookies and while loading page you can check against that session variable or cookies and can show it the the input value.
You can see following code as a reference but it is using jquery.
<div style="padding-left:40px">
<h2> PHP Calculator</h2>
<form method="post" action="" id="formID">
Enter value: <input type="text" name="value" id='btnValue'> <br> <br>
<div style="padding-left: 105px">
<input type="button" value="9" name="btn1">
<input type="button" value="8" name="btn2">
<input type="button" value="7" name="btn3">
<input type="button" value="+" name="btn4"> <br>
<input type="button" value="6" name="btn5">
<input type="button" value="5" name="btn6">
<input type="button" value="4" name="btn7">
<input type="button" value="-" name="btn8" style="padding-left: 9px"> <br>
<input type="button" value="3" name="btn8">
<input type="button" value="2" name="btn9">
<input type="button" value="1" name="btn10">
<input type="button" value="/" name="btn11" style="padding-left: 9px"> <br>
<input type="button" value="0" name="btn12" style="padding- left:33px">
<input type="button" value="." name="btn13" style="padding-right:9px">
<input type="button" value="x" name="btn14" style="padding left: 7px"><br>
</div> <br>
<input type="submit" value="Calculate" name="Calculator">
</form>
</div>
$('input[type=button]').on('click',function(){
if($('#btnValue').val()==''){
$('#btnValue').val($(this).val());
}else{
$('#btnValue').val($('#btnValue').val()+($(this).val()));
}
})
Here is the jsfiddle you can look on https://jsfiddle.net/nef1qgzn/13/
Note: I have user Jquery libraray 3.3.1
Here is my form and i want to make a simple calculator in php using with one text box
<form action="calculator.php" method="POST">
<input type="text" name="result">
<br>
<input type="button" name="one" value="1">
<input type="button" name="two" value="2">
<input type="button" name="three" value="3">
<input type="button" name="four" value="4">
<input type="button" name="five" value="5">
<input type="button" name="six" value="6">
<input type="button" name="seven" value="7">
<input type="button" name="eight" value="8">
<input type="button" name="nine" value="9">
<input type="button" name="0" value="0">
<input type="button" name="+" value="+">
<input type="button" name="-" value="-">
<input type="button" name="*" value="*">
</form>
need to use some javascript, get the values and append to the text box
<script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#frmCalc input:button").on("click", function() {
var par = $(this).val();
$("#result").val($("#result").val() + par);
});
});
</script>
<form action="calculator.php" id="frmCalc" method="POST">
<input type="text" name="result" id="result">
<br>
<input type="button" name="one" value="1">
<input type="button" name="two" value="2">
<input type="button" name="three" value="3">
<input type="button" name="four" value="4">
<input type="button" name="five" value="5">
<input type="button" name="six" value="6">
<input type="button" name="seven" value="7">
<input type="button" name="eight" value="8">
<input type="button" name="nine" value="9">
<input type="button" name="0" value="0">
<input type="button" name="+" value="+">
<input type="button" name="-" value="-">
<input type="button" name="*" value="*">
</form>
If you send your request to calculator.php like this, you can't tell the difference between the numbers you pressed first.
So first of all you need to put those numbers into a text field - maybe with javascript? - and then you can transmit the form to calculator.php and do the math. If you only got one textfield, you can simply split the string you will get and search for your operations.
the variable $res stock just the last submit input clicked. it must stock all submit values which i clicked in.
<html>
<head></head>
<body>
<form method="post" action="">
<p><input type="text" name="textbox" size="13" readonly></p>
<p>
<input type="submit" name="one" value="1">
<input type="submit" name="one" value="2">
<input type="submit" name="one" value="3">
</p>
<p>
<input type="submit" name="one" value="4">
<input type="submit" name="one" value="5">
<input type="submit" name="one" value="6">
</p>
<p>
<input type="submit" name="one" value="7">
<input type="submit" name="one" value="8">
<input type="submit" name="one" value="9">
</p>
<p>
<input type="submit" name="operation" value="+">
<input type="submit" name="one" value="0">
<input type="submit" name="operation" value="-">
</p>
</form>
php code :
<?php
$res="";
if(isset($_POST['one']))
{
$val = $_POST['one'];
$res.=$val;
echo $res;
}
?>
</body>
</html>
concatenation doesn't work on input submit, $res stock just a value of one input.
if u want to concatenate something it goes like
$res = "something";
$res .= $_POST['one']
.= concatenates strings .. what you are doing is that you are assigning the value to the string, so whatever is inside will be replaced by the $_POST['one'] value
from what i can see from your html, your designing a calculator. so you want to enter each number assigned to a button into the text field. try this:
html file
<html>
<head></head>
<body>
<form method="post" action="">
<p><input type="text" name="textbox" size="13" readonly></p>
<p>
<input type="submit" name="one" value="1">
<input type="submit" name="two" value="2">
<input type="submit" name="three" value="3">
</p>
<p>
<input type="submit" name="four" value="4">
<input type="submit" name="five" value="5">
<input type="submit" name="six" value="6">
</p>
<p>
<input type="submit" name="seven" value="7">
<input type="submit" name="eight" value="8">
<input type="submit" name="nine" value="9">
</p>
<p>
<input type="submit" name="plus" value="+">
<input type="submit" name="zero" value="0">
<input type="submit" name="minus" value="-">
</p>
</form>
function.php
<?php
if (isset($_POST['one'])) {
$num1 .= $_POST['one'];
echo $num1;
}
if (isset($_POST['two'])) {
$num2 .= $_POST['two'];
echo $num2;
etc...........
}
?>
How to read to variable which button is clicked.
I have this five button's which are like answers.
<fieldset> <legend> Question1 </legend>
<input type="button" value="1st Ansswer"/>
<input type="button" value="2nd Ansswer"/>
<input type="button" value="3rd Ansswer"/>
<input type="button" value="4th Ansswer"/>
<input type="button" value="5th Ansswer"/>
</fieldset>
I want to read the answer for question 1 into variable to send it via email.
For better explain i want something like this
This is for
<select name="SOption"><option>Option1</option><option>Option2</option></select>
I read the result in variable Question like this
$Question= $_POST['SOption'];
How to do the same with button's instead select.
Hope you understand
<fieldset>
<legend>
Question1
</legend>
<input type="button" name="foo" value="1st Ansswer"/>
</fieldset>
$Question= $_POST['foo'];
Just add a name or ID to each button whose value you want.
Why cant you just use radio buttons??
<fieldset> <legend> Question1 </legend>
<input type="radio" name="radio_q1" value="1st Ansswer"/>
<input type="radio" name="radio_q1" value="2nd Ansswer"/>
<input type="radio" name="radio_q1" value="3rd Ansswer"/>
<input type="radio" name="radio_q1" value="4th Ansswer"/>
<input type="radio" name="radio_q1" value="5th Ansswer"/>
</fieldset>
<fieldset> <legend> Question2 </legend>
<input type="radio" name="radio_q2" value="1st Ansswer"/>
<input type="radio" name="radio_q2" value="2nd Ansswer"/>
<input type="radio" name="radio_q2" value="3rd Ansswer"/>
<input type="radio" name="radio_q2" value="4th Ansswer"/>
<input type="radio" name="radio_q2" value="5th Ansswer"/>
</fieldset>
<input type="submit" value="SUBMIT">
Otherwise another way would be with a hidden text field, and javscript/jquery.
Like so....
<form id='theForm' method='POST' action='wherever'>
<input type='hidden' name='thequestion1' id='thequestion1'>
</form>
<button class='questionButton' data-answer='TheAnswer 1'>Answer 1</button>
<button class='questionButton' data-answer='TheAnswer 2'>Answer 2 </button>
<button class='questionButton' data-answer='TheAnswer 3'>Answer 3 </button>
<button class='questionButton' data-answer='TheAnswer 4'>Answer 4 </button>
Then your jquery..
$(document).ready,function(){
$('.questionButton').click(function(e){
e.preventDefault();
var answer = $(this).data('answer');
$('#thequestion1').val(answer);
$('#theForm').submit();
});
});
And then from this, you can just extrapolate how you would do multiple questions, store them all in the hidden text fields for each question, then submit the form in the end.
I managed to do this with hiden button
function change_value($id, $value)
{
document.getElementById($id).value = $value;
}
<input type='button' class="myButton2" onclick="change_value('quest1', this.value);" value="Ans1">
<input type='button' class="myButton3" onclick="change_value('quest1', this.value);" value="Ans2">
<input type='button' class="myButton4" onclick="change_value('quest1', this.value);" value="Ans3">
<input type='button' class="myButton5" onclick="change_value('quest1', this.value);"value="Ans4">