i am a beginner in php and my first task is to build a calculator and I am here to ask how to get a value from a button and just echo it on the same page. I am trying through method post using isset but enable to display any value on the same page.
<form action="" method="POST">
<input type="button" value="0" name="zero">
</form>
<?php
if (isset($_POST["zero"]))
{
echo $_POST["zero"];
}
?>
Only an input[type=submit] will submit the form onclick. It is valid to have multiple submit buttons:
<form action="" method="POST">
<input type="submit" value="0" name="mybutton">
<input type="submit" value="1" name="mybutton">
<input type="submit" value="2" name="mybutton">
</form>
<?php
if (isset($_POST["mybutton"]))
{
echo $_POST["mybutton"];
}
?>
If you want to use input[type=button] then you will need some Javascript to trigger the submit, and a hidden input to transport the value.
<script>
window.onload = function(){
document.getElementsByName("mybutton").onclick = function(){
document.getElementsByName("postvar")[0].value = this.value;
document.forms.myform.submit();
}
};
</script>
<form name="myform" action="" method="POST">
<input type="hidden" name="postvar" value="" />
<input type="button" value="0" name="mybutton">
<input type="button" value="1" name="mybutton">
<input type="button" value="2" name="mybutton">
</form>
<?php
if (isset($_POST["postvar"]))
{
echo $_POST["postvar"];
}
?>
Change
<input type="button" value="0" name="zero">
To
<input type="submit" value="0" name="zero" />
Add an event handler if you want to do it via button click.
Try this
<form action="" method="POST">
<input type="submit" value="0" name="zero">
</form>
<?php
if (isset($_POST["zero"]))
{
echo $_POST["zero"];
}
?>
Use
<input type="submit" value="0" name="zero">
else if you want to use button use javascript
<form action="" method="POST">
<input type="button" value="0" name="zero">
</form>
<script type="text/javascript">
$("input[type='button']").click(function(){
alert(this.value);
});
</script>
Related
<form id="test" method="post" action="getValue.php">
<input type="submit" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="submit" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
</form>
I want to know how to get the value of customized attributes of between several radio buttons like example above by using php.
How can i get the value of customizedValue1 and customizedValue2 in php?
Thanks
You can't access directly from PHP to this values, you need to pass them as AJAX POST values to the PHP file like this:
FORM
<form id="test" method="post" action="getValue.php">
<input type="radio" name="sample" value="A" customizedValue1="1" customizedValue2="X"/>
<input type="radio" name="sample" value="B" customizedValue1="2" customizedValue2="Y"/>
<button type="submit"> Submit </button>
</form>
JS
$('#test').on('submit',function(){
var customizedValue1 = $('#test input[name=sample]:checked').attr('customizedValue1');
$.post('getValue.php',{'customizedValue1':customizedValue1});
});
On getValue.php you can access to the value:
echo $_REQUEST['customizedValue1'];
If they are connected to eachother somehow. You can also use the values as an array in html form
<form id="test" method="post" action="getValue.php">
<input type="text" name="data[A][customizedValue1]" value="value1" />
<input type="text" name="data[A][customizedValue2]" value="value2" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
$customizedValue1 = $_POST['data']['A']['customizedValue1'];
$customizedValue2 = $_POST['data']['A']['customizedValue2'];
echo $customizedValue1;
echo $customizedValue2;
}
?>
I need to post values outside the form in php , How to do it
Here is my sample code
<input type="hidden" id="test" name="test" />
<input type="hidden" id="testvalue" name="testvalue"/>
<?Php for($i=0;$i<5;$i++) { ?>
<form action='some_url'>
<input type="text" value="dynamic_value" name='testname'>
<select name="testselect">
<option value="<?php echo $dynamicvalue[$i]?>"><?php echo $dynamicvalue[$i]?></option>
</select>
<button class="btn btn-primary book_now" type="submit">BookNow</button>
</form>
<?php } ?>
i need to post all input values above the form also,since i cannot use that input field inside the loop, i want id to be unique so i cant use inside
any other option available to post all data
Use jQuery:
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script>
$(document).ready(function(){
$('form').submit(function(){
var test = $('#test').val();
var testvalue = $('#testvalue').val();
$(this).append('<input type="hidden" name="test" value="'+test+'">');
$(this).append('<input type="hidden" name="testvalue" value="'+testvalue+'">');
return true
});
});
</script>
You can add input fields into for-loop
<?php for($i=0;$i<5;$i++) { ?>
<form action='some_url'>
<input type="text" value="dynamic_value" name='testname'>
<select name="testselect">
<option value="<?php echo $dynamicvalue[$i]?>"><?php echo $dynamicvalue[$i]?></option>
</select>
<button class="btn btn-primary book_now" type="submit">BookNow</button>
<input type="hidden" name="test" />
<input type="hidden" name="testvalue"/>
</form>
<?php } ?>
i have two file and two forms:
file 1:
<form name="name1" action="form2.php" method="post">
<input <? $_session['define'] = 'value1' ?> type="submit" ...>
</form>
<form name="name2" action="form2.php" method="post">
<input <? $_session['define'] = 'value2' ?> type="submit" ...>
</form>
As you see i have two forms and two different submit buttons but when i press each of them, the second value (the last) set to the $_session['define'] and in the second form i always have 'value2'.
You must post the data to PHP:
<form name="name1" action="form2.php" method="post">
<input name='v1' value='1' type="submit" />
</form>
<form name="name2" action="form2.php" method="post">
<input name='v2' value='2' type="submit" />
</form>
In form2.php:
<?php
session_start();
$_SESSION['value1'] = isset($_POST['v1'])?$_POST['v1']:0;
$_SESSION['value2'] = isset($_POST['v2'])?$_POST['v2']:0;
?>
Personally, I would make it one form and use JQuery/AJAX to POST the values.
<form id="selectForm" name="name1" action="form2.php" method="post">
<input id="v1" name='v1' value='1' type="submit" />
<input id="v2" name='v2' value='2' type="submit" />
</form>
<script>
// Requires JQuery
$("#selectForm input[id^='v']").click(function(e){
e.preventDefault();
$.post(form2.php, { $(this).attr("name"): $(this).attr("value") }, function(){ alert("Selection Saved.) });
});
</script>
can we use $_POST 2 times with different page ?
this example ..
action.php
<form action="next.php" method="post">
<input name="test" value="test" />
<button type="submit" name="test">submit</button>
</form>
next.php
<?php
$test=$_POST['test'];
?>
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="test1">submit</button>
</form>
next1.php
<?php
echo $test2=$_POST['test1']; >> in here i didnt get test1 value . why ?
?>
Within next.php and action.php you need to change your input type like as
<input type="text" name="test" value="test" />
<input type="text" name="test1" value="<?php echo $test; ?>" />
Within next.php and action.php you were missing the type attr of input tag
and you have same name attributes for submit and input within next.php need to remove or change the value from respective
<input type="submit" value="submit"/>
Below code i have tried my local server and it executed successfully.
action.php:-
<form action="next.php" method="POST">
<input type="text" name="test"/>
<input type="submit" value="submit"/>
</form>
next.php:-
<?php
$test = $_POST['test'];
echo $test;
?>
<form action="next1.php" method="POST">
<input name="test1" value="<?php echo $test; ?>" />
<input type="submit" value="submit"/>
</form>
next1.php:-
<?php
$test2=$_POST['test1'];
echo "------".$test2;
?>
Hope this will help.I think this is achieved your requirement.
If you want to do it within one file e.g. index.php then here is the code
<?php
if(isset($_POST['test'])){
if(isset($_POST['test_text'])){
echo 'Output from NEXT: '.$_POST['test_text'];
}
}
elseif(isset($_POST['test1'])){
if(isset($_POST['test_text1'])){
echo 'Output from NEXT1: '.$_POST['test_text1'];
}
}
?>
<table style="width:100%;">
<tr >
<td style="width:50%;">
<form action="" method="post" name="next">
<input type="text" name="test_text" value="<?php echo isset($_POST['test_text']) ? $_POST['test_text']:''; ?>" />
<button type="submit" name="test">submit test1</button>
</form>
</td>
<td style="width:50%;">
<form action="" method="post" name="next1">
<input type="text1" name="test_text1" value="<?php echo isset($_POST['test_text1']) ? $_POST['test_text1']:''; ?>" />
<button type="submit" name="test1">submit test1</button>
</form>
</td>
</tr>
</table>
I hope this will help.
You need to send the POST value with the second form again, e.g. in a hidden field, otherwise the value won't be sent.
Furthermore your submit button shouldn't have the same name as the input field you want the content from. So change the name of your submit button, e.g.:
action.php
<form action="next.php" method="post"> <input name="test" value="test" /> <button type="submit" name="submit">submit</button> </form>
next.php
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="submit">submit</button>
</form>
<?php
echo $_POST['textvalue'];
echo $_post['radiovalue'];
?>
<div id="hidethis">
<form method="POST" action="">
<label>Tekst Value</label>
<input type="text" name="textvalue">
<label>Radio Value</label>
<input type="radio" name="radiovalue" value="autogivevalue">
<input type="submit" id="submit" name="submit" value="submit">
</div>
http://jsfiddle.net/Bjk89/2/ here is it with the jQuery.
What i try to do is to hide the <div id="hidethis"> when it's clicking submit.
I know i can make another page where i can recieve the values without the <form> section, but i want to put both in one page, make the <div id="hidethis"> hidden after submit.
So i'll be able to get echo $_POST['textvalue']; and echo $_post['radiovalue']; as results
RESULT MUST BE LIKE
A Text // This is the value you input into Tekst Value
autogivevalue // This is the value from the radio button
----- INVISIBLE -----
<form is hidden because we set it in jQuery so>
</form>
Try this. No need to use jQuery here.
<?php
if($_POST) {
echo $_POST['textvalue'];
echo $_post['radiovalue'];
} else {
?>
<form method="POST" action="">
<label>Tekst Value</label>
<input type="text" name="textvalue">
<label>Radio Value</label>
<input type="radio" name="radiovalue" value="autogivevalue">
<input type="submit" id="submit" name="submit" value="submit">
</form>
<?php
}
?>
Try adding '#' in your jquery code. Your version does not have # next to submit. Also your form is missing a closing tag here and in your JSFiddle code.
Try this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('#submit').click(function () {
$('form').submit();
$('#hidethis').hide();
});
});
</script>
<form method='post' id="hidethis" name='form'>
<input type="text" name="textvalue">
<input type="radio" name="radiovalue" value="1">
<input type="button" id="submit" name="submit" value="submit">
</form>