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.
Related
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>
I have a html form with about 280 text, radios and select dropdown fields. I have to capture a lot of information about people's outgoings. Eg. Who their gas and electricity providers are, how much they pay, the rates, their bank accounts, loan accounts, broadband etc. The list of fields sounds huge, but it's laid out in tabs and it isn't too overwhelming and people don't have to fill in all of it anyway.
I present the blank form to the new user and then post every field value (blank or not) to the mysql dbase on the first submit.
The problem is.. how do I retrieve all that data and put it into the form the next time? I'm out of my depth with elegant solutions, so I guess I'd do a foreach loop through each database record retrieved and save it to a variable, but then I have to insert the 280 field values into the fields using an statement in the value="xxx" for each field, something like:
<div class="form-group row">
<label for="loan1name" class="col-lg-5 control-label text-right">Loan Name</label>
<div class="col-lg-3">
<input type="text" class="form-control" name="loan1name" placeholder="Car loan" value=<?php echo htmlspecialchars($loan1name) ?>>
</div>
</div>
If I had 10 fields it wouldn't matter, but 280 is unmanageable doing it that way. Is there any other way? Do I need to go about this a completely different way? Does it make sense to duplicate the form and use one for the blank one which does the INSERT, then another for the pre-populated version for the UPDATE query? Any help would be greatly appreciated. I just know there must be a better way, but I can't find any answer on here or anywhere else.
It doesn't seem simple because the fields are quite varied and one is even a set of pictures as a radio button sequence (it's pictures of types of locks you have on your front door - for your home insurance).
Mysql usually return an associative array. You can then use the key/value pairs in this array to dynamically create your form.
<?php foreach($fields AS $name => $value){ ?>
<input type="text" name="<?= $name ?>" value="<?= $value ?>" /><br />
<?php } ?>
Note: The downside is that you are still missing the label and placeholder of each input. Also, they would all be text input.
I have a page where a user can create a recipe. I have a javascript script that allows the user to keep adding form input elements for more ingredients. Say the recipe has 4 ingredients the user can continue to add inputs. However this changes the id to ingredient1/2/3/4 for the input ID/Name,and i can't understand how to get around getting the information from the form post as the amount of elements is upto user discretion. How would you go about loading the results into PHP variables and then inserting them into a mysql query.
the syntax for the html form
<div id="ingredient1" class="clonedInput">
<h2 class="first-column">Ingredient</h2>
<span style="float:left; padding-right: 10px;">
<input type="text" name="scroll" value="Add Ingredient" id="ingredient" class="ingredient"/>
</span>
</div>
ingredient1 would change to ingredient2 for the purpose 2 ingredient's being added.
A slightly confusing problem, so any help would be amazing!
JB
I think what you are looking for is using [] in your input element names. This will post the data as an array. So, say you have this as your form's ingredient input elements (after javascript has added several):
<input type="text" name="ingredients[]" value="" />
<input type="text" name="ingredients[]" value="" />
<input type="text" name="ingredients[]" value="" />
<input type="text" name="ingredients[]" value="" />
<input type="text" name="ingredients[]" value="" />
When the user POST's the data, you will be able to access the array of ingredients at $_POST['ingredients']
I will tell you the logic, not the code as only javascript here would not be enough.
you create a table ingredients. Anything you can allow them to use. id, ecc
while posting, instead of doing giving the user a freedom chosing their ingredient1, ingredient2 ... you do load from ingredients table the values and index them as ingredient[id_from_db] ecc..
you create a table recipe. It should have a unique id, its description
you create a relation table recipe_id, ingredient_id
then when the user post its data you create 1 row in the recipe and as many rows in the relation table as the ingredients they use.
When selecting later the recipe you also select everything which is in the relation table, it could be several rows there...
Hope it is of any help for you. Can't show you any code for this, but it is not hard to make if you understand how it goes.
maxim
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 have a long questionnaire (46 questions) with each having (3) possible answers, yes, sometimes, and no, (3 radio buttons) each of which have a corresponding value, 10, 5, and -10 respectively.
I had the questions divided up amongst 4 headings with several questions under each heading.
I was able to post all the data to mySQL Database by giving each a different name attribute that corresponded with its field in the Database.
The problem I am having is that I can no longer use the radio buttons as a set, that is, they are all selectable, defeating the purpose of having them, essentially making them check-boxes....
I need to have each radio button its own value in my database and I need them to be grouped in their 3 possible choices for each question...
I will post an example of my form html and php send script....
Thank you in advance for any advice...
html code one question
<div class="questionBox">
<p>1.Have I clearly defined my companies target market?</p>
<input type="radio" name="ca1y" value="10"/>
<label for="ca1">YES</label>
<input type="radio" name="ca1s" value="5"/>
<label for="ca1">SOMEWHAT</label>
<input type="radio" name="ca1n" value="-10"/>
<label for="ca1">NO</label>
</div>
/* php post update */
$radioPost="INSERT INTO questions
( ca1y,
ca1s,
ca1n,...)
VALUES
( '$_POST[ca1y]',
'$_POST[ca1s]',
'$_POST[ca1n]',...)
I tried to give each radio button an ID attribute and use that to update the database but I can't seem to get that to work....
any help would be greatly appreciated
cheers.
I'm not sure if I'm getting the jist of what you trying to do, but it seems like you need to save an individuals answers to different questions to the DB but that each question can only have ONE of let's say three possible answers. Well, in that case you might think of redesigning your DB to look something like this:
You store the users details in the users table, the question details in the questions table and the different answers in the answers table. A user will be able to answer multiple questions but can only answer a particular question once. You then save the answer in ONE field called answer_value for example. There is no need to store the answer in multiple fields since you will be storing the VALUE of the radio button they chose, i.e you will store either a 10, 5 or -10 depending on which radio option was selected.
It's also important that in your HTML you name all the radio buttons for a particular question the same. This ensures that the radio options act as radio buttons and not as checkboxes. Your HTML would most likely look something like this:
<div class="questionBox">
<p>1.Have I clearly defined my companies target market?</p>
<input type="radio" name="q1" value="10"/>
<label for="ca1">YES</label>
<input type="radio" name="q1" value="5"/>
<label for="ca1">SOMEWHAT</label>
<input type="radio" name="q1" value="-10"/>
<label for="ca1">NO</label>
</div>
<div class="questionBox">
<p>2.Have I clearly defined my companies product?</p>
<input type="radio" name="q2" value="10"/>
<label for="ca2">YES</label>
<input type="radio" name="q2" value="5"/>
<label for="ca2">SOMEWHAT</label>
<input type="radio" name="q2" value="-10"/>
<label for="ca2">NO</label>
</div>
.....
Note that question 1 has 3 radio options all named "q1" and question 2 has 3 radio options all named "q2". Follow the same convention for all your questions. Your PHP then becomes pretty straightforward and will probably look something like this:
$radioPost="INSERT INTO answers (user_name, question_name, answer_value)
VALUES (('$_POST[user_name]', 'q1', '$_POST[q1]),('$_POST[user_name]', 'q2', '$_POST[q2]), ....)
If you not sure what the radio button names are, you can use a foreach loop as follows:
$user = $_POST[user_name];
foreach ( $_POST as $key => $val )
{
If ($key <> 'user_name') //you might need to check that you only reading the radio options
{
$radioPost="INSERT INTO answers (user_name, question_name, answer_value)
VALUES ('$user', $key, '$val')"
....
}
}
Also be advised that even if you only using radio options, you should still clean the inputs to prevent attacks like SQL-Injection etc. I suggest you find some more info on securing your code against attack.
BTW, if you place the questions in a table of it's own, you can dynamically create your questionnaire from this table, which then allows you to add or delete questions at will, making your whole application a lot more flexible. And saving your answers in one field instead of three, will remove all the NULL values from your table, which will simplify your queries when you perform calculations. I know MySQL normally puts a zero as the default integer value, but it's still better to save it into one field. If you want to know which option a user chose for a particular question, just check the value stored in that field.
Not very good solution: write js to deselect appropriate radio buttons.
On more serious note i don't understand the need to have all radios with different names. Why is such solution not applicable: name radios in one question with same name, lets say question1, then after form post just look at value and from that you can easily update db. I suppose you want to collect how many times each radio was selected. So if you know that question1 had choice 1 you than also know that question1 did not have choices 2 and 3. Unless you collect data in other way or for other purpose, but then I think there need to be a bit more input from you about the purpose of this. Though I still think you can do this like i said, this would not need complex processing, id say rudimentary programming knowledge should suffice.