Skip to content Skip to sidebar Skip to footer

Need To Identify Dynamic Input/textareas When Inserting To Database

I have a form that allows the user to dynamically add extra inputs and textareas. Since there will be multiple sets of each, I can not get away with just grabbing all the data post

Solution 1:

You will need to manually manage your array indexes in Javascript. But it seems you only need to count the outcome ids, so:

var outcome_num = 0;

$("button.addoutcome").click(function(){
     $("form").append("<input name='outcome[" + outcome_num + "]' ...>")
});

And the measure list would carry the outcome_num alike, but be additonally an array itself:

.append("<input name='measure[" + outcome_num + "][]' ...>");

So you end up with:

 <input name='outcome[1]' ...>
     <input name='measure[1][]' ...>
     <input name='measure[1][]' ...>
     <input name='measure[1][]' ...>

 <input name='outcome[2]' ...>
     <input name='measure[2][]' ...>
     <input name='measure[2][]' ...>

This way you'll receive the data in a usable associative format. Just loop over $_REQUEST["outcome"][$i] and the $_REQUEST["measure"][$i][$x] subarray.

If each measure subentry needs additonal _description fields, then you will have to manually count them too. In that case it would be best to replicate the whole array structure in Javascript to keep track of existing indicies. It's more work to extract the index number from existing form fields - doable with regular expressions, but more work, so I would prefer the manual counters here. Seems workable in your case, unless I'm missing something.


Solution 2:

I would suggest naming them into arrays

example might be something like this

    <input name="outcome[i]" type="text" />
    <input name="outcome[i][measure][]" type="text" />

where i is a number you define when you add elements

then in your php you can parse them in for loop or whatever


Post a Comment for "Need To Identify Dynamic Input/textareas When Inserting To Database"