My situation is this: I have an html page which contains a table of four rows and three columns. When creating the page (using a PHP script) some of the rows have their cells filled with text while the rest have their cells filled with text boxes. What I want to do is allow the user to fill any number of the rows with text boxes and then submit them to a PHP script.
When the user clicks submit, I want to get all the rows which the user has filled in (essentially the rows with filled textboxes) and submit just the data in those text boxes to the script, ideally in a 2D array where each row of the array represents a row of the table?
Well, you can echo out the input tags with the name attribute that goes something like fields[]. When submitted this will set $_POST['fields'] to an array containing the data given from the user.
HTML
<tr>
<td><input type="text" name="fields[]" /></td>
<td><input type="text" name="fields[]" /></td>
<td><input type="text" name="fields[]" /></td>
</tr>
PHP
var_dump($_POST['fields']); // User submitted data
I would name each cell as cell<column number>$<row number> then all you do in the PHP script is to check all the possible entries and find out if they have been sent to the server and use the data as appropriate.
Related
I have a food recipes site, users are able to submit their own recipes, on the page where you submit your recipes there are textboxes to enter the description of the recipe.
What I need is basically, when users open the page with the form, on the textbox I want to appear a table there (I already have the code for the table). I just don't know where to put that code. I was wondering if I could use value="table code here" for the table to appear in the text box? Or is there another way?
Here's the code of the textbox:
<label for="inputPassword3" class="col-sm-3 control-label"><?php echo RECIPE_DESCRIPTION; ?></label>
The tag does not allow you to pass in a tag. What I would recommend however is creating a and adding tags inside of your individual table row's cells.
Ex:
<table>
<tr><td><input type="text" /></td></tr>
</table>
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'];
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.
I have a HTML table base invoice for collecting the details of a order. New table rows can be added or deleted to this as the user needs them. Table has columns like item name, type, color, unit price, qty. I have place <input> and <select> tags inside <td> tags to collect data.
I need to submit a complete invoice to a database. The problem I'm having is I'm not sure how to get input values to my $_POST variables since the number of <input> and <select> vary from time to time based on the items on the invoice.
I can give dynamic or static names to input fields based on the table row id when they are generated using javascript. But how can I collect these data in the submit end to my php arrays or variables?
This is one table row. All other are similar to this.
<tr class="table_row_blue">
<td class="table_data_index">1</td>
<td><select>
<option>Test</option>
</select></td>
<td class="table_data_item"><select>
<option> 2GB </option>
</select></td>
<td><input type="text" id="price_field" size="6"></td>
<td><input type="text" id="qty_field" size="6"></td>
<td><span class="table_subtotal">125,200.00</span></td>
</tr>
Please give me some ideas to implement my idea.
encapsulate all the varying parts in separate tables in the database. so you can store them separately and relate to each other with foreign keys.
also it's a good idea to use array naming for you HTML inputs like
<input name="data[Invoice][field1]" />
<input name="data[Invoice][field2]" />
this gives you data, which is more structured and easy to iterate.
Add a hidden form value, which will hold the number of rows you are going to submit.
On the PHP end, first of all check the number of rows and then get all values using
a row index.
You can have input fields which submit to an array:
<input type="text" id="price_field" name="data[0][price_field]" size="6">
which will appear in you $_POST as
$_POST['data'][0]['price_field']
I would suggest you use the unique id of an entry as differentiator (replacing the 0 in the above example)
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']);
}