i've a form POST with multiple submit buttons. i understand to get this to work i must have them with different name.
however, i wanna keep the name to be the same because i wanna handle the POST using a single script.
im not sure if there is other way but i know javascript can be used. however, how do i get the value of the hidden value associated to the button since now they have only a single ??
my example is as follows:
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="1" />
<input type="submit" id="btnremove" name="btnremove" value="Remove" inputbutton/>
<input type="hidden" name="removeid" value="2" />
<input type="submit" id="btnremove" name="btnremove" value="Remove" inputbutton/>
<input type="hidden" name="removeid" value="2" />
<input type="submit" id="btnremove" name="btnremove" value="Remove" inputbutton/>
</form>
Your hidden values are not associated with the buttons at all. Furthermore, you cannot use the same value for the ID attribute on multiple elements.
What I usually do in this situation is check the POST vars. Name them something like remove_1, remove_2, etc. Then you can search through your POST vars, find all of them beginning with remove_ (or whatever format you choose... don't use it for other things) and then you can parse out the ID of what you are trying to remove.
You could always just use 3 different forms, all with the same action. No JavaScript needed.
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="1" />
<input type="submit" value="Remove" inputbutton/>
</form>
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="2" />
<input type="submit" value="Remove" inputbutton/>
</form>
<form method="Post" action="file.php">
<input type="hidden" name="removeid" value="2" />
<input type="submit" value="Remove" inputbutton/>
</form>
It's possible using two different methods:
If you absolutely have to show 3 different buttons, use a separate <form> wrapper for each one. Put each "removeid" element in a different form.
Otherwise, just have a single button, and when submitted, use JavaScript to set the value of a single hidden input element before posting the form. You can find sample code for this easily with a Google query for "javascript+form+post".
You can have one form with more than one submit button sharing the same name, your initial assumption was wrong.
The following code is perfectly valid, and the value of the clicked submit button will be passed along with its name:
<form action="TestZone.html" method="GET">
<input type="submit" name="MySubmit" value="First" /><input type="submit" name="MySubmit" value="Second" /><input type="submit" name="MySubmit" value="Third" />
</form>
You can't have multiple elements with same ID, but same name for form elements is common and valid.
Hi i have resolved my questions by following Brad solution. to get the POST var, i did this:
//Check if Remove btn is clicked
$isClickRemove = false;
$cid = "";
foreach($_POST as $k=>$v){
$pos = strpos($k,"btnremovecart_");
if($pos !== false){
$pos2 = strpos($k,"_"); //2nd pos to get cartID
$cid = substr($k,$pos2+1);
$isClickRemove = true;
break;
}
}
my html looks like this:
<input type="submit" id="btnremovecart_11" name="btnremovecart_11" value="Remove" />
hope this helps =)
You can't because there is no way of distingushing the different fields.
Related
I have a table that is in a foreach loop, and every iteration the hidden input receives the ID from the database. My table has in each column an input field because I'd like to edit the data from the database right in the table, and to make it happen I had to put a submit button that when clicked retrieves the ID from the hidden input into another file that then does the operation.
<form action="../php/Operations.php" method="post">
//...
<?php foreach ($Service->select() as $col) { ?>
<input type="hidden" value="<?php echo $col["id"]; ?>" name="id"/>
//...
The problem is that the $_POST value is always the very first from the table, because there are n hidden input with the same name.
I'd like to know if there is a way to retrieve the hidden input's value from the clicked row, having in mind I'm not using $_GET, but a submit button,
<button type="submit" name="submit" value="update" class="btn_action">Editar</button>
that when clicked is supose to execute the code I wrote:
switch($_REQUEST["submit"]) {
case "update":
$ServiceDatabase->update($_POST["id"]);
break;
//...
Thank you.
If you want multiple inputs with the same name use name="id[]" for the input name attribute. $_POST will then contain an array for name with all values from the input elements. Then you can then loop over this array.
Example:
<form method="post">
<input type="hidden" name="id[]" value="foo"/>
<input type="hidden" name="id[]" value="bar"/>
<input type="hidden" name="id[]" value="baz"/>
<input type="submit" />
</form>
PHP
$ids= $_POST['id'];
print_r($ids);
EDIT: You can change your code like this so each submit button is tied to exactly one hidden input with the name="id".
<?php foreach ($Service->select() as $col) { ?>
<form action="../php/Operations.php" method="post">
<input type="hidden" value="<?php echo $col["id"]; ?>" name="id"/>
<button type="submit" name="submit" value="update" class="btn_action">Editar</button>
</form>
<?php } ?>
Rendered HTML:
<form action="../php/Operations.php" method="post"><input type="hidden" value="1234" name="id"/><button type="submit" name="submit" value="update" class="btn_action">Editar</button></form>
<form action="../php/Operations.php" method="post"><input type="hidden" value="5678" name="id"/><button type="submit" name="submit" value="update" class="btn_action">Editar</button></form>
<form action="../php/Operations.php" method="post"><input type="hidden" value="9101112" name="id"/><button type="submit" name="submit" value="update" class="btn_action">Editar</button></form>
i would recommend you to use ajax for submitting the data
Just create a click event in for the button and get the button id
in your htm form create an id attribute like this
<input type="hidden" id="colId<?php echo $col["id"]; ?>" value="<?php echo $col["id"]; ?>" />
and your button should look like this
<button class="buttonSubmit" id="<?php echo $col["id"]; ?>" >Submit</button>
$(".buttonSubmit").click(function(){
var id = $(this).attr("id");
var hiddeninput = $("colId"+id).val();
// here you can process how would you send the data to a php file
});
Either wrap each col in a separate form and have a separate submit button per col or make the name attribute an array. i.e. name="id[<?php echo htmlspecialchars($id); ?>"
I am done with the search page where the user enters the information and select from the drop-list. I've also added the button AddList where you can have more than one search form with tag names changed. All of the searches will eventually be executed in one Submit button and each search will go in one single query. My table caries all the information and tuples contain only numbers.
UPDATED: I tried changing the input type of the input tags but the enable and disable functions can't seem to work on integers, only on text fields. How can I fix that?
My submission is tomorrow, and here is my search code:
<script type="text/javascript">
$('#exactButton').live('click', function(){
$(this).prev().prev().prev().prev().prev().removeAttr('disabled');
$(this).prev().prev().prev().attr('disabled',true);
$(this).prev().prev().prev().prev().attr('disabled',true);
});
$('#rangeButton').live('click',function(){
$(this).prev().prev().removeAttr('disabled');
$(this).prev().prev().prev().removeAttr('disabled');
$(this).prev().prev().prev().prev().attr('disabled',true);
});
})
</script>
And this is my HTML code:
<button id="button">Add List</button><br><br>
<form id ="form" name="search" method="get" action="test.php">
<div id="div">
<select name ="select" >
...options...
</select>
Value:<input type="text" name="exact" id="exactField" />
From: <input type="text" name="from" id="fromField" />
To: <input type="text" name="to" id="toField" />
<br>
<input type="button" name="answer" value="Range" id="rangeButton" />
<input type="button" name="answer" value="Exact" id="exactButton" />
</div>
<br>
<input type="submit"name="search" value="Submit">
</form>
Thank you in advance..
as Dagon said, you will see all submitted parameters in the URL since you are submitting the form with method GET. here is very good explanation for this: http://www.w3schools.com/php/php_get.asp
One idea:
add some custom attributtes to the elements (to the clones too).
this.attr("mycustomattribute","somevalue");
after this, you can get all elements on the page with your custom attribute and your value.
divs = $('div[mycustomattribute="somevalue"]'); //should give all div container with attribute "mycustomattribute" with value "somevalue"
divs.each(function(){
console.log(this,$(this).attr('name'));//show expression (for debug)
});
then you can collect this elements, serialize it and add it to your post Not tested, but an idea.
Kind Regards
In PHP, it's already there.
print_r($_GET); will list all parameters sent by GET method
print_r($_POST); will list all parameters send by POST method.
Then, of course, you will need to iterate in the array to include each values in your query statement.
You can naming input with prefix or suffix correspond to sequence that user click to add list and add those set of inputs to only form.
<form id ="form" name="search" method="get" action="test.php">
<div>
<select name ="select[1]" >
...options...
</select>
Value:<input type="text" name="exact[1]" class="exactField" />
From: <input type="text" name="from[1]" class="fromField" />
To: <input type="text" name="to[1]" class="toField" />
<br>
<input type="button" name="answer[1]" value="Range" class="rangeButton" />
<input type="button" name="answer[1]" value="Exact" class="exactButton" />
</div>
<div>
<select name ="select[2]" >
...options...
</select>
Value:<input type="text" name="exact[2]" class="exactField" />
From: <input type="text" name="from[2]" class="fromField" />
To: <input type="text" name="to[2]" class="toField" />
<br>
<input type="button" name="answer[2]" value="Range" class="rangeButton" />
<input type="button" name="answer[2]" value="Exact" class="exactButton" />
</div>
.
.
.
<br>
<input type="submit"name="search" value="Submit">
</form>
I'm trying to figure out how to send data from one php function to another through using a submit button. The idea is that a spellchecker will highlight the correct spelling for a word, and the user will click the button to run the program again with the correct spelling.
<form method="post" action="results.php">
<input type="submit" value="Search!" name="submit" id="searchButton"
value="<?php
$search = "dog";
$_POST[$search]
?>"
/>
This is what I have so far...
Also, is it possible for the value of a button to be a variable?
You can use hidden variables as an alternative to ajax.
<input type="hidden" value="dog" id="search" />
<input type="submit" value="Submit" />
Is it possible for me to have two self submitting forms on a single page. If yes how do I allot different blocks of code to each form ?
Have a hidden input with two different values.
<form action="" ...>
<input type="hidden" name="form_no" value="0">
...
</form>
<form action="" ...>
<input type="hidden" name="form_no" value="1">
...
</form>
On the server side, different on the basis of $_REQUEST['form_no']
Or you could also add it as a name parameter in submit element.
<input type="submit" name="form0">
Use isset($_REQUEST['form0']) to differentiate.
Another way of doing it is to append a GET parameter to differentiate
<form action="<?php echo $_SERVER['PHP_SELF'];?>?form_no=0" ...>
...
</form>
<form action="<?php echo $_SERVER['PHP_SELF'];?>?form_no=1" ...>
...
</form>
Use $_GET['form_no'] to differentiate.
Name your first form form_one, and the second form form_two.
<?php
if (isset($_POST['form_one'])) {
// First form was submitted
}
if (isset($_POST['form_two'])) {
// Second form was submitted
}
?>
Two forms can be placed in a code by giving them different names and keeping their target _blank
<form action="action.asp" method="get">
First name: <input type="text" name="fname" /><br />
Last name: <input type="text" name="lname" /><br />
<input type="submit" name="submit1" value="Submit" />
</form>
<form action="action.asp" method="get">
First name: <input type="text" name="fname" /><br />
Last name: <input type="text" name="lname" /><br />
<input type="submit" name="submit2" value="Submit" />
</form>
<?php
if(isset($_GET['submit1'])){
// first form was submitted
}
if(isset($_GET['submit2'])){
// second form was submitted
}
?>
Each form has a different script specified in this example, but keep in mind that you can simply use the same php file for both actions, specifying a different block of code for each form (that's the php part above).
Someone may have a better answer, but this method has served me well in the past.
I'm trying to build a form using php & jquery, but I'm a little confused as to what to do with the jquery portion of it...
Basically, when the user submits the first form, I want to direct them to the "next step" form, but I want to retain the values submitted from the first one in a hidden input field...
If someone can either show me how or point me to a good tutorial, I'd appreciate it...
I don't have any of the php or jquery yet, and this is just a simplified version of the html markup...
//first.php
<form name="form1" method="post" action="second.php">
<input type="text" name="name" value="" />Name
<input type="submit" name="step1" value="Next" />
</form>
//second.php
<form name="form2" method="post" action="process.php">
<input type="hidden" name="name" value="{$_POST['name']}" />
<input type="text" name="message" value="" />message
<input type="submit" name="step2" value="Finish" />
</form>
<input type="hidden" name="name" value="{$_POST['name']}" />
should be,
<input type="hidden" name="name" value="<?php echo $_POST['name']}; ?>" />
and also sanitize the input, if you want
I don't no if there is a better way to do that.
But, when I need to do such thing, I do in this way:
<script>
<?php
foreach($_POST as $key => $valule)
{
echo "$('$key').val('$value')";
}
?>
</script>
So, in your nextstep file, all you'll need to do is set up the hidden fields and then just loop through the post vars and set each one via jquery.