Jump to content

Recommended Posts

Sorry for the lack of detailed subject - I'm not quite sure how to describe it in a short space;

 

I have a form:

<select id="1[]" name="1[]"><option value="1">1</option><option value="2">2</option> (and so on - lots of options)</select>
<input type="text" id="2[]" name="2[]" />

And a button below which adds another set of those fields, which has no limit.

 

The value of the input is specific to the selection made before. What I would like to do is, for each pair of input and select values, write something to a file.. for example: "You chose an $select1 with the name $input2"

 

I have looked into writing a file and that shouldn't be too hard for me, but im not sure how to pair each selection to the text input, could someone suggest how to do this?

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/179487-solved-form/
Share on other sites

Well, first of all, I would highly discourage using "1" and "2" as field names (assuming that wasn't just for illustrative purposes). Use a meaningful name.

 

Anyway, you are creating the field names in a way that they will be processed by PHP as unindexed arrays. Therefore, PHP will give them numeric indexes starting at zero. Assuming you have exactlythe same number of field 1's and field 2's and they are in the same order as they are to be paired, you should be able to make the association that $field1[0] is paired with $field2[0], and $field1[1] is paired with $field2[1], and so on.

 

However, I would prefer to be more explicit and to give my fields explicit indexes to ensure they are paired correctly

 

<select name="field1[0]">
    <option>1</option>
    <option value="2">2</option>
</select>
<input type="text" name="field2[0]" />

<select name="field1[1]">
    <option>1</option>
    <option value="2">2</option>
</select>
<input type="text" name="field2[1]" />

...etc.

Link to comment
https://forums.phpfreaks.com/topic/179487-solved-form/#findComment-947021
Share on other sites

Ok, so im using the following names:

<select> : aircrafttype

<input> : aircraftreg

 

And I've changed it so each set have their own id, like so:

<select name="aircrafttype[0]">
    <option>1</option>
    <option value="2">2</option>
</select>
<input type="text" name="aircraftreg[0]" />

<select name="aircrafttype[1]">
    <option>1</option>
    <option value="2">2</option>
</select>
<input type="text" name="aircraftreg[1]" />

 

I also forgot to mention: I have a button next to each set which removes that particular set of inputs, so the order will not always be set 1, then set 2 and so on.

Link to comment
https://forums.phpfreaks.com/topic/179487-solved-form/#findComment-947024
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.