1

I have a form that contains dynamically generated fields. There can be an arbitrary number of fields in the submitted form, depending on how many fields are added by the user. Here is a JSFIDDLE that you can refer to know what kind of fields i am generating dynamically. LINK TO JSFIDDLE

the only difference in this JSFIDDLE and my project is that instead of generating 2 dropdownsand 2 input fields in one set i am generating 3 dropdowns and 1 input fields in one set. number of sets that can be generated is maximum of 15 by now that the user can select from the dropdown in jsfiddle.

What is need to know is if a user generated 7 sets, how would i know the name of each field as i have to use PHP to process the data of this form and submit to a database.

2
  • Track the field using a counter. in each iteration increase the count of this global counter and append it to fieldname in each row. Commented Oct 5, 2012 at 18:34
  • Thnx Umair, but i really can now understand this as i am really new to all of this. all i know that if i have a text input firld that has a name="xxx", so i can use form method=post, and this way on the PHP processing file i can do $xxx=$_POST['xxx'] and submit it later. Can you please tell me how can i do this in open discription. Commented Oct 5, 2012 at 18:48

2 Answers 2

3

have a look at this. It adds a variable to the end of each input, so you can keep track of the inputs.

Edit1: the names will be as such,

row1 - input1_1, input2_1, input3_1, input4_1

row2 - input1_2, input2_2, input3_2, input4_2 ...and so on

Edit2: this jsfiddle removes the latest row(s) added.

Sign up to request clarification or add additional context in comments.

8 Comments

thnx for answering but still i am kind of confused in this. What is a user select 3 from dropdown and ADD. it will generate 3 sets having 2dropdowns and 2 text input fields per set or we can say a total of 6 text input fields and 6 dropdowns.(1) now what will be the name for the rest two sets? .(2)i understood that for the first text input in first set name will be input1_j or input1_+j ???
+ operator is used for concatenating strings, so 'input1_+j will be input1_1`
that is really going to easy to put or describe the values in my PHP processing file. Thnx a lot. also, if you have some time for this: i tried to use this jsfiddle for me and it is not working for me. actually nothing is happening. the way i tried to use it was: i put the javascript in script open and close tags in head ans also i linked to google cdn jquery lib 1.8.2 and the html code to body. i also tried to use it in a entire blank html page i.e creating another project file other than i was working on, but still no luck
actually what i wanted to do was: have the selection from the first dropdown to be upto 15 insted have a dd button just the user to select a number from the drop down and it adds those sets and if selected something else from dropdown or removed anyone manually it should change the number in the dropdown as well. but was not able to do that.
you should be putting the javascript in the js block and only html in the html 1.
|
3

Your jQuery code is already explicitly naming the fields. Eg:

$("<input>").attr("name", "a").appendTo(newDiv);

This will create an input named a:

<input name="a" />

Your jQuery gives each dynamically created input the same name. If you submit the following form with 3 fields having the same name, the server would receive a=value1&a=value2&a=value3.

<form>
    <input name="a" value="value1" />
    <input name="a" value="value2" />
    <input name="a" value="value3" />
    <input type="submit" />
</form>

This works with most server technologies, but not with PHP. PHP overwrites existing values with the last value. So, if you examined the value of a in PHP, you would only see value3. But, the good news is that you can change your input names slightly and have PHP recognize all the values. In PHP, when you append square brackets to your input names (name="a" becomes name="a[]"), PHP parses the values into an Array. So for the following slightly modified form:

<form>
    <input name="a[]" value="value1" />
    <input name="a[]" value="value2" />
    <input name="a[]" value="value3" />
    <input type="submit" />
</form>

When you look at the value of a in PHP, you will get an array of values containing value1, value2, and value3. So, just change your jQuery code to use square brackets at the end of the name:

$("<input>").attr("name", "a[]").appendTo(newDiv);

1 Comment

thnx @gilly3 for explaining this. have to go through this a couple of times to understand it, or do a practical with this code first. i have to say this: YOU HELPED ME A LOT IN THIS :-D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.