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 plete 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.
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 plete 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.
Share Improve this question edited Mar 29, 2013 at 18:15 nneonneo 180k37 gold badges329 silver badges410 bronze badges asked Jun 18, 2011 at 7:11 Kanchana RandikaKanchana Randika 5462 gold badges13 silver badges27 bronze badges 3- in php part use the if isset($_POST['optional property']) to check for values to see if they are present – Ibu Commented Jun 18, 2011 at 7:14
- @lbu ya but this table might have 300 total inputs then should I have to check this dynamically or is there a way to do this dynamically.I mean using a loop or something like that. – Kanchana Randika Commented Jun 18, 2011 at 7:18
- I think you should edit your question. Some things are not clear: what do you mean get values to $_POST? it is browser what places input values to post on submit. Second your input fields don't have a name. Third in second paragraphs second sentence seems to have excess words, that makes it a bit unclear. And lastly do you know that you can have array ing from forms? If you just hive names yo your fields i.e.: "items[]" and then if you have some some inputs with such name all of them will end up in $_POST['items'] and that will be an array with all values from fields named "items[]" – morphles Commented Jun 18, 2011 at 7:19
3 Answers
Reset to default 4encapsulate 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)