Variable number of input fields in form - php

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)

Related

How can I use a number input to make an array?

edit: Somebody in the answers recommended a list, and I think that that is a good idea, but then I have no idea of how to implement that with the amount as well as the type. So if someone could explain that?
I have to make a fast food order form for school, and I decided to make you able to enter the amount as an input instead of checkboxes, but I still want them stored in an array. Is there any way to do that? Here's a little snippet of what I have in html:
<form>
<table class="menu" style="width:100%;">
<tr>
<td class="productlist">
<!-- Drinks -->
<input type="number" name="drinks[]" value="cokeclassic" id="cokeclassic" class="productCheckBox" min="0" max="20" placeholder="0">
<label for="cokeclassic" class="product">Coca Cola Classic $0.75<br><br></label>
</tr>
</table>
<input type="submit" value="Cart" class="submit">
</form>
If you want them stored in an array, you would need to have multiple input fields with name="drinks[]". All of your inputs will be sent as one array called drinks, containing numbers passed in each field.
If you want to pass name of a drink and an amount of it, you would need something like name="drinks[0].name" for name of your drink and name="drinks[0].amount" for amount
Why you would like to store them in an array?
When submitting the form, the input-tag can only deliver a single value anyways, because there is only a single input.
Try to do a list/stack with the orders or even try to create a dynamic multiselect including amounts when ordering.

Multiple choice from database without <select>

I'm trying to make a form with Symfony to be able to select multiple choice by checking checkboxes without having a <select> dropdown alone in my form.
Theses choice come from a table in my database, each row must be a choice possibility in my form.
For example table T_Choices :
ID
Choices
1
Choice_1
2
Choice_2
I'm able to create the form with many checkbox as there are rows.
But how can i submit this "dynamic" form and get thoses data in one "array" in my controller to have for example $form["choices"]->getData()[0] or 1 or 2 etc.
Thank you for your help. And sorry for my bad english.
By giving those checkboxes the same value for the name-attribute and adding square brackets at the ending [].
You probably need:
<input id="1" name="choices[]" type="checkbox" value="1"><label for="1">Choice_1</label>
<input id="2" name="choices[]" type="checkbox" value="2"><label for="2">Choice_2</label>

How to get data of a select option tag into a php variable before submitting pure php?

I have a table that shows a list of students. The data is shown using a foreach loop.
Now inside the table, I have a column to store the attendance which specifies whether a student is present or absent. I put the present and absent inside a select tag in HTML.
as shown below:
<table>
<tr>
<td>ID</td>
<td>
<select name="att">
<option value="1">Present</option>
<option value="0">Absent</option>
</select>
</td>
</tr>
</table>
Now I want to take the attendance and update related rows inside the table.
How to get values of IDs and atts to pass through php to the database?
I tried to get the values inside php array and send it but how to get the value from the select tag inside an array I have no idea.
If possible please provide the code.
just like this
$att =$_POST['att'];

how to update database records using form

There are number of records which I gather them from mysql say thease:
id name mark
1234 john 18
53 smith 12
324 mike 15
...
I want to build a form to give ability to edit(update) all marks at once
I know that I can show them at the form using textbox value property.
But how can I Identify the exact same record when I want to process the posted form, in order to update the correct filed? and surly, I don't know how many records are there in the form.
The idea might be identifying the records through the id field.
but how to do that?
If this application is for administrative use only:
<input type="hidden" name="id" id="id" value="_ID_VALUE_" />
<input type="text" name="name" id="name" value="_NAME_VALUE_" />
<input type="text" name="mark" id="mark" value="_MARK_VALUE_" />
then
UPDATE table SET mark = '_SANITIZED_MARK_VALUE_' WHERE id = "_SANITIZED_ID_VALUE_";
If it's an application for the end user, you don't want to trust that he/she won't change the value of the hidden input. In this case, you'll most likely want to store the id, name, and mark in a $_SESSION variable and do comparisons on the post to figure out which record pertains to which id, and then build your update statement accordingly.
When you build your form, use the ID from the database as part of each text field's NAME attribute. Then when you receive your array of posted values, you can process the keys to find out the ID of the record you should be updating. For example, you could have your fields named "record-1234", "record-53", etc. Then you would iterate over the keys in $_POST, split them on "-" and use the resulting IDs in your UPDATE query.

Submitting an unknown number of fields to a PHP Script

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.

Categories