PHP - Retrieve via $_POST the hidden input's value between several others - php

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); ?>"

Related

delete a row based on id

I am trying to delete a row based on its ID
HTML:
<form action="" method="post">
<input type="submit" name="delete" value=" delete "/>
</form>
PHP:
$u = $_REQUEST['edit']; // contains the ID of row to be deleted
if(isset($_POST['delete'])){
$db->exec("DELETE FROM infos WHERE id = '$u'");
}
Nothing happens when the delete button is pressed. How can I fix this?
You need an edit input whose value is the ID your want to delete.
<form action="" method="post">
<input type="hidden" name="edit" value="<?php echo $id; ?>">
<input type="submit" name="delete" value=" delete "/>
</form>
Replace $id with the actual variable you use in the script that creates the form.
<form action="" method="post">
<input type="hidden" name="edit" value="<?php echo $id; ?>">
<input type="submit" name="delete" value=" delete "/>
</form>
After that try this
$u = $_REQUEST['edit']; // contains the ID of row to be deleted
if($u){
$db->exec("DELETE FROM infos WHERE id = '$u'");
}
Apparently when you are submitting only you posting data to php file.So you can check like this is enough.you told delete not happening so that i am posting this answer

submit checkbox value for the dynamic elements on html form

I have check box which will be dynamically added along with the textfields.
<input type="checkbox" name="namechkbox[]">
<input type="text" name="nametxt[]">
I will need to map the checkbox value with the text field. I found from other questions that after adding the hidden element over the input element checkbox.
<input type="hidden" name=namechkbox[]" value=0>
Since it's dynamic, it will add the index because of name[] in the name.
What is the way to handle checkbox with value submit for the dynamic elements?
if user checks any checkbox change the values off to on and on to off viceversa.
for ex:
<input type="checkbox" name="namechkbox[0]" value="on" />
<input type="hidden" name="nametxt[0]" value="This my data string 1" />
<input type="checkbox" name="namechkbox[1]" value="off" />
<input type="hidden" name="nametxt[1]" value="This my data string 2" />
now you submits the from then check loop through array to check
if namechkbox[0] value is on
then take the value of nametxt[0]
Try this:
<?php
if(isset($_POST)){
$invite = $_POST;
echo '<pre>';
print_r($invite);
}
?>
<form method="post" action="">
<?php for($i=0;$i<4;$i++)
{
?>
<input value="chkbob1" name="invite['id<?php echo $i;?>']" type="checkbox">
<input value="" name="name['id<?php echo $i;?>']" type="text">
<?php
}
?> <input type="submit">
</form>

Submit buttons: button text different from value

I am writing a search form in PHP.
I want the user to be able to add search fields using following submit button:
<input type="submit" name="fields" value="<?php echo $fields+1 ?>" />
Now the button shows the value of $fields + 1.
Actually what I want is the button to show something else (like add new field).
Just adding text between the <input>...</input> tags does not help.
The text just appears right of the button.
How do I change the text on the button and still pass the value of $fields + 1 to GET/POST?
You can use an hidden input to store the value count, it will be available after GET/POST:
<input type="submit" name="fields" value="add new field" />
<input type="hidden" name="fieldsCount" value="<?php echo $fields+1 ?>" />
You can use the button element:
<button type="submit" name="seven" value="7">Push Me</button>
Refs: http://www.w3schools.com/tags/att_button_type.asp
You Can use the hidden field to store or post the $fields+1 value:
<input type="hidden" name="fields" value="<?php echo $fields+1 ?>" />
<input type="submit" name="submitter" value="Send" />

get value of submit in Text field when form is post in PHP

I need the value of submit button when form is posted in existing text field. and below is the code.
I trying out but it is creating a new text field.
<?php
$var1 = $_POST['hello'];
//below here i am trying out something but not getting in exiting textfield.
//i need the value of submit button ie some value in exiting textfield ie text1
echo "<input type='text' name='text1' value='$var1'>";
?>
<form method="post">
<input type="submit" name="hello" value="some value">
<input type="text" name="text1" value="dskfjls">
</form>
It would create a new text field, because that's what you're echo is doing.
Just change it do:
<?php
$submitbutton = $_POST['hello'];
$textbox = $_POST['text1'];
//below here i am trying out something but not getting in exiting textfield.
//i need the value of submit button ie some value in exiting textfield ie text1
echo $submitbutton;
echo $textbox;
?>
<form method="post">
<input type="submit" name="hello" value="some value">
<input type="text" name="text1" value="dskfjls">
</form>
Edit in reply to comment
Use this:
<form method="post">
<input type="submit" name="hello" value="some value">
<input type="text" name="text1" value="<?php (isset($_POST['hello'])?$_POST['hello']:null); ?>">
</form>

multiple submit button

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.

Categories