concatenation doesn't work on input submit - php

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...........
}
?>

Related

cannot show the result of php file, help me

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

PHP CALCULATOR USING BUTTONS

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

How we will make a simple calculator just in php with one output textbox

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.

php variable gets echoed twice

I am trying to echo a form when a function is called. My code is as follow :
function add_post(){
....
echo '<form method="post" action="">
<input type="text" name="post_title" size="45" id="input-title"/>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea>'
.wp_dropdown_categories().'
<input type="hidden" name="new_post" value="1"/>
<input class="subput round" type="submit" name="submit" value="Post"/>
</form>
';
}
but wp_dropdown_categories() is displayed twice. Here is the HTML output:
<div class="entry-content">
<!-- this should not be displayed -->
<select class="postform" id="cat" name="cat">
<option value="9" class="level-0">Entertainment</option>
</select>
<!-- form starts here -->
<form action="" method="post">
<input type="text" id="input-title" size="45" name="post_title">
<textarea id="text-desc" cols="66" name="post_content" rows="5"></textarea>
<select class="postform" id="cat" name="cat">
<option value="9" class="level-0">Entertainment</option>
</select>
<input type="hidden" value="1" name="new_post">
<input type="submit" value="Post" name="submit" class="subput round">
</form>
</div>
any idea why wp_dropdown_categories() is called twice?
By default, wp_dropdown_categories() echos the result. So you should either break your code in the following way:
echo '<form method="post" action="">
<input type="text" name="post_title" size="45" id="input-title"/>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea>
';
wp_dropdown_categories();
echo '<input type="hidden" name="new_post" value="1"/>
<input class="subput round" type="submit" name="submit" value="Post"/>
</form>
';
or pass the echo variable to the function as zero as such:
echo '<form method="post" action="">
<input type="text" name="post_title" size="45" id="input-title"/>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea>'
.wp_dropdown_categories('echo=0')).'
<input type="hidden" name="new_post" value="1"/>
<input class="subput round" type="submit" name="submit" value="Post"/>
</form>
';
Try this...
See the documentation for this function wp_dropdown_categories()
function add_post(){
....
echo '<form method="post" action="">
<input type="text" name="post_title" size="45" id="input-title"/>
<textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea>'
.wp_dropdown_categories(array('echo'=>0)).'
<input type="hidden" name="new_post" value="1"/>
<input class="subput round" type="submit" name="submit" value="Post"/>
</form>
';
}

How to submit one out of many forms to seperate tables in mysql database?

Here is my existing code:
<?php
if (isset($_POST['submitForm'])) {
print_r($_POST);
}
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm" />
</form>
This simply posts any of the forms which are submitted, individually.
What I'm trying to accomplish is submitting these individual forms to a specific table in the same database.
So for example, Form1 would be submitted to Table1, Form2 to Table2, etc. Each form will always be submitted to it's matching table.
Change the name foreach of your Submit input form element, for example to submitForm1, submitForm2 and submitForm3, like:
<input type="Submit" value="Submit Form" name="submitForm1" />
<input type="Submit" value="Submit Form" name="submitForm2" />
<input type="Submit" value="Submit Form" name="submitForm3" />
Then in your php logic you could do something like:
if(isset($_POST['submitForm1'])){
// Do things with your form1
}elseif(isset($_POST['submitForm2'])){
// Do things with your form2
}elseif(isset($_POST['submitForm3'])){
// Do things with your form3
}
the $_POST or $_GET array is composite of your input tag. So when you call $_POST['submitForm'] you should have an input with that name like
<input name ='sumbitform' />
If you need to get separate form just change your code in this way:
<?php
if (isset($_POST['submitForm1'])) {
//sql statement for table 1 there
}
if (isset($_POST['submitForm2'])) {
//sql statement for table 2 there
}
if (isset($_POST['submitForm3'])) {
//sql statement for table 3 there
}
?>
<form action="" name="form1" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm1" />
</form>
<form action="" name="form2" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm2" />
</form>
<form action="" name="form3" method="post">
<input type="text" value="" name="A" />
<input type="text" value="" name="B" />
<input type="text" value="" name="C" />
<input type="text" value="" name="D" />
<input type="Submit" value="Submit Form" name="submitForm3" />
</form>

Categories