HTML form with multiple submit options - php

I'm trying to create a small web app that is used to remove items from a MySQL table. It just shows the items in a HTML table and for each item a button [delete]:
item_1 [delete]
item_2 [delete]
...
item_N [delete]
To achieve this, I dynamically generate the table via PHP into a HTML form. This form has then obviously N [delete]-buttons. The form should use the POST-method for transfering data.
For the deletion I wanted to submit the ID (primary key in the MySQL table) of the corresponding item to the executing php skript. So I introduced hidden fields (all these fields have the name='ID' that store the ID of the corresponding item.
However, when pressing an arbitrary [delete], it seems to submit always just the last ID (i.e. the value of the last ID hidden field).
Is there any way to submit just the ID field of the corresponding item without using multiple forms? Or is it possible to submit data from multiple forms with just one submit-button? Or should I even choose any completly different way?
The point why I want to do it in just one single form is that there are some "global" parameters that shall not be placed next to each item, but just once for the whole table.

<input type="submit" name="delete[1]" value="delete">
if (isset($_POST['delete'])) $id=key($_POST['delete']);

it seems to submit always just the last ID
It submits all of them, but since the name doesn't end with [], PHP discards all by the last.
Is there any way to submit just the ID field of the corresponding item without using multiple forms?
No. At least not without some unfortunate JavaScript. All (non-disabled) hidden inputs (with names and values) will be successful. You can't limit based on proximity to a clicked input element.
If I understand your goals correctly, you have two main options.
Put one form per row (in the cell with the delete button)
Encode the id value into the name of the submit button

You could get rid of the hidden fields and name your submit buttons like this:
<input type="submit" name="delete[1]" />
<input type="submit" name="delete[2]" />
<input type="submit" name="delete[3]" />
and then
<?php
if (isset($_POST['delete'])) {
$toDeleteId = key($_POST['delete']);
}

Related

HTML Button contain two names

I am trying to make the button in my form contain two names. Let me explain.
I have 2 buttons, the first one has a value of 1, and the other one has a value of 2. They are both connected to the post method "Click". But I need my "$pos = $_POST['position']" to become the value of the button. Is that possible?
What I tried was <button name="click" name="position" type="submit"> but it's not working
No element can have two attributes with the same name (i.e. you cannot have two attributes named name on one element). Some attributes take a space-separated list of values, but name is not one of them.
Pick either click or position to be the name of the button, and change your server-side code so that it recognises data with that name as representing both pieces of information that you were trying to convey.
You'll probably want to add a value attribute too.

Getting values of all checked checkboxes in PHP and put them in an array

I'm creating a CMS in which I have an overview of pages. I want the user to be able to mass delete these pages and so I have created a form in which each page has a checkbox with the pages database ID as value and name:
<input class="mass-delete-check" type="checkbox" name="<?=$page["id"]?>" value="<?=$page["id"]?>" id="<?=$page["id"]?>">
Now when I submit this form I need to get the values of the checkboxes that are actually checked and put them in an array I can go through to delete them. The thing here is that I will have to get checkbox values based on if they are checked and not on their name because I can't know all names.
Does anyone have a solution to this?
Use the same name for all checkboxes. So after submiting you will have array with page IDs to delete.
<input class="mass-delete-check" type="checkbox" name="delete_pages[]" value="<?=$page["id"]?>" id="<?=$page["id"]?>">
After submit you would get array of IDs with $_POST['delete_pages'], which contains actual page IDs what you need to delete.

PHP - input a list of input fields into a database

New.php (form)
This has a section where you can clone input fields and the values go into array. When posting, these values are then added to the database via a foreach loop.
e.g. <input type="text" name="number[]" />
Edit.php (form)
I now extract all the rows from the data and put them into a list of input fields.
How do i update a mysql entry for that particular value? Normally with ajax and jquery, i can get the rowid from 'data-rowid' i generated and then update where rowid = x. What about on submit. Would it need to go back into an array or how can i check against a row id?
Your help appreciated.
You would need to submit rowid as a hidden input field so it is posted along with all your other information. That way when you get to edit.php you have the information you need.
<input type="hidden" name="rowid" value="1">
And then on edit you can do :
$rowid = $_POST['rowid'];

Fill form with checkbox array

I want to create a PHP script which can fill up a form from a text file. The form is on a password-protected page. There are 4 fields where i need to put text (name, village, etc.)
I need to select checkboxes but they are in an array and I only know their label name. At max I can look up id's and use it as static values, but I don't know them how to use them either.
<form method="POST" action="...">
<input type="checkbox" name="tag_id[]" id="tag_id_192" value="192"><label for="tag_id_192">House</label>
I saw lots of tutorials which shows how to do it with checkboxes but they used the check box name and without the login part. I never used PHP before so I don't know even where to start. If someone could guide i would be very grateful.
Example:
The .txt file each row represents a form fill up the fields are separated with a *: Name * village .... * the check boxes label names which i need to set active
If you're using cURL, you can post multiple fields with the same name, if it has [] after the name, php will automatically concatenate them all into an array..
So its valid to do tag_id[]=192&tag_id[]=175&tag_id=[285]..
In PHP it will know to make it array(192,175,285)
As a side note, fields that do not have [] get overwritten each time they appear...

Submit form and send data through AJAX at the same time?

Let's say I have a simple form, with a select combobox and a table that has a checkbox for each row, to choose the rows you want.
Now, on the server-side, I need to associate all the items selected from the table (using the checkbox for each row), with the item selected in the combobox.
I know the value in the combobox will be submitted with the form, but how can I send all the selected elements from the table? do I have to use AJAX or something? or is it possible to do it via POST/GET?
The form needs to enclose (be an anscestor of) both the table and the combo box.
The table row checkboxes should all look like
<input type="checkbox" name="rows[]" value="ROW ID"/>
And then on the server side you'll get, in addition to the combobox value, $_POST['rows'] as an array containing the ids of the checked rows.

Categories