I'm trying to retrieve information from database with submit buttons , which have the same name . I'm giving value which is id of the row , so I could make a different actions, but I can not make up my mind how to do that .
When trying like this for example
<form name="form" action="index.php" method="post">
<input type="image" name="x" value="1">
<input type="image" name="x" value="2">
</form>
and php =>
<?php
if (isset($_POST['x'])){
echo $_POST['x'];
}
?>
in chrome it works fine , but in other browsers it doesn't , any ideas ? thanks
UPDATE
if ($r = $con->query("SELECT * FROM table")){
if ($row = $r->fetch_assoc()){
echo "<input type='image' name='submit' value='" . $row['id'] . "'>";
// and other data which I need to
}
$r->free();
}
I meant like this
HTML
<form id="form1" action="index.php" method="post">
<input type="button" name="x" value="1">
<input type="button" name="x" value="2">
<input type="text" name="action" id="action">
</form>
JavaScript
var nodes = document.getElementById('form1').childNodes;
for(var i=0; i<nodes.length; i++) {
nodes[i].onclick = function(){
if(this.name == 'x'){
document.getElementById('action').value = this.value;
}
};
}
PHP
if(isset($_POST['action'])){
if($_POST['action'] == "1"){
...
}
if($_POST['action'] == "2"){
...
}
}
http://jsfiddle.net/qLubz/
Just change type="text" to type="hidden" when ready to deploy.
Alternatively with no JavaScript:
<form action="index.php" method="post">
<button type="submit" name="action" value="delete">Delete</button>
</form>
<form action="index.php" method="post">
<button type="submit" name="action" value="copy">Copy</button>
</form>
<form action="index.php" method="post">
<button type="submit" name="action" value="edit">Edit</button>
</form>
Related
I have a site where I make a payment, a bill is created through it, but the problem is that I can not use $ _POST twice in one, and this example:
<? if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<? } if (isset($_POST['pay'])) {
// MY QUERY HERE
// HEADERS HERE
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<? } ?>
Try this.
Kindly check the code for comment.
<?
if (isset($_POST['submit'])) {
$info = $_POST['info'];
// use echo to display second form
echo '
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<!-- // Note the action value, it's for post back. This allow you to post back to the current page -->
<!-- This is to keep the record passed from the first form -->
<input name="info" value="'. $info .'" type="hidden" />
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>';
} else if (isset($_POST['pay'])) {
// MY QUERY HERE
} else {
// Note the action value, it's for post back. This allow you to post back to the current page
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
}
?>
Not the prettiest way to do this but to achieve your result try this...
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<?php } elseif (isset($_POST['pay'])) {
// Perform query here
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<?php } ?>
var el = document.getElementById('btn1');
el.addEventListener('click', function() {
var hidEl = document.getElementById('hidElement');
hidEl.value = 'Add';
var form = document.getElementsByTagName('form');
form.submit();
});
var el2 = document.getElementById('btn2');
el2.addEventListener('click', function() {
var hidEl = document.getElementById('hidElement');
hidEl.value = 'Print';
var form = document.getElementsByTagName('form');
form.submit();
});
<?php
if($_POST["hidElement"]=='Add')
echo "add";
else
echo "Print";
?>
<form action="" method="">
<input type="text" name="nasir" >
<input type="hidden" name="hidElement" id="hidElement" />
<input type="button" id="btn1" value="Submit & Add">
<input type="button" id="btn2" value="Submit & Print">
</form>
<?php
if(isset($_POST["yasir"]))
echo "Submit & Print";
else
echo "Submit & Add";
?>
<form action="" method="">
<input type="text" name="nasir" >
<input type="submit" value="Submit & Add">
</form>
<input type="hidden" name="yasir">
<input type="submit" value="Submit & Print">
</form>
There is no second starting for second form. I want both forms use same , but how PHP will recognize which form button has been pressed. I tried above but in both cases it submiting second yasir hidden input!!!
There is alot of inputs in first form.
please try this
<form action="" method="post">
<input type="text" name="nasir" >
<input type="submit" value="Submit & Add" name="first_btn">
<input type="hidden" name="yasir">
<input type="submit" value="Submit & Print" name="second_btn">
</form>
in php you can check by using this
<?php
if(isset($_POST['first_btn'])){
/*first form submit*/
}
if(isset($_POST['second_btn'])){
/*second form submit*/
}
?>
For every there should be an end also. Otherwise it will not work
try
<form action="" method="post">
<input type="text" name="nasir" >
<input type="submit" value="Submit & Add" name="btn1">
<input type="hidden" name="yasir">
<input type="submit" value="Submit & Print" name="btn2">
</form>
In that case it's better to use javascript to handle form post. You can add to buttons to same form for add and print and handle click on them using javascript. Something like below:
<form action="" method="" id="form">
<input type="text" name="nasir" >
<input type="hidden" name="hidElement" id="hidElement" />
<input type="button" id="btn1" value="Submit & Add">
<input type="button" id="btn2" value="Submit & Print">
</form>
In javascript
var el = document.getElementById('btn1');
el.addEventListener('click', function() {
var hidEl = document.getElementById('hidElement');
hidEl.value = 'Add';
var form = document.getElementById('form');
form.submit();
});
var el2 = document.getElementById('btn2');
el2.addEventListener('click', function() {
var hidEl = document.getElementById('hidElement');
hidEl.value = 'Print';
var form = document.getElementById('form');
form.submit();
});
In Php you can check based on hidElement value.
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>
I have n number of text box (n may be any number) with same name. And
I want to access the value of all the text box of that name.
Ex-:
<form method="post" id="create">
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="button" id="newFieldBtn"/>
<input type="submit" name="save" value="save"/>
</form>
jQuery
<script>
jQuery(document).ready(function($) {
$('#newFieldBtn').click(function(){
var code='<input type="text" name="user[]" />';
jQuery('#create').append(code);
</script>
or is there any other way to access the value of the text box. Either
by class or any other property..
<script>
jQuery(document).ready(function($) {
$('#newFieldBtn').click(function(){
var count = document.getElementById('count').value;
count++;
var code = '<input type="text" name="user'+count+'" />';
jQuery('#create').append(code);
document.getElementById('count').value = count;
</script>
and your html like...
<form method="post" id="create">
<input type="hidden" id="count" value="0" name="count">
<input type="button" id="newFieldBtn"/>
<input type="submit" name="save" value="save"/>
</form>
and in your php code...
<?php
if(isset($_POST))
{
$count = $_POST['count'];
for($i=1;$i<=$count;$i++)
{
$user.$i = $_POST['user'.$i];
}
}
?>
Try this one, it will show you the values :
<form action="#" method="post">
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="text" name="user[]" />
<input type="submit" value="submit" >
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
foreach($_POST['user'] as $key => $value)
{
echo $key." has the value = ". $value."<br>";
}
}
?>
See this in action: http://wistudat.be/try/array.php
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>