I have a form that gets generated dynamic every time with PHP and also gets updated with jQuery .appendTo() while the visitor interacts with it. The problem I have is that I want to get every single value of input text and select boxes from the current form and submit them with POST in arrays.
So far I have done the following (an example):
<div>
<input type="text" class="dynamicTags" name="dynamicTags1" value="Web Design"/>
<select class="val_tag" name="val_tag1">
<option value="1" >Newbie</option>
<option value="2" >Amateur</option>
<option value="3" selected="selected" >Rockstar</option>
</select>
</div>
<div>
<input type="text" class="dynamicTags" name="dynamicTags2" value="Programming"/>
<select class="val_tag" name="val_tag2">
<option value="1" >Newbie</option>
<option value="2" selected="selected" >Amateur</option>
<option value="3" >Rockstar</option>
</select>
</div>
<div>
<input type="text" class="dynamicTags" name="dynamicTags6" value="Java"/>
<select class="val_tag" name="val_tag6">
<option value="1" >Newbie</option>
<option value="2" >Amateur</option>
<option value="3" selected="selected" >Rockstar</option>
</select>
</div>
<div>
<input type="text" class="dynamicTags" name="dynamicTags13" value="Photoshop"/>
<select class="val_tag" name="val_tag13">
<option value="1" >Newbie</option>
<option value="2" selected="selected" >Amateur</option>
<option value="3" >Rockstar</option>
</select>
</div>
The name of each input text and select box is generated dynamic. The classes are the same.
With jQuery I managed to pass to my script simple variables from textboxes and select boxes (that I know from before) but I can’t get to make it work with arrays. I have tried
data: {valTags: $('.val_tag').val(), dynTags: $('.dynamicTags').val()}
inside the $.ajax() function but without any luck. Sure I’m missing something crucial here but I can’t find the proper way to do it. Do I have to use .each() function somehow?
In the end I would like to have in my $_POST variable two arrays like that:
dynamicTags[] = ([0] => 'Web Design', [1] => 'Programming', [2] => 'Java' ...etc....)
val_tag[] = ([0] => 1, [1] => 0, [2] => 0 ...etc....)
Have you tried your data as
It would send all your form values in the post array which php can json_decode and iterate through as needed