views:

362

answers:

2

Hi, I am working on a web form that dynamically adds text fields as the user desires for multiple dates. In essence this is what happens... The user goes to the form, it has a primary date text field for the user, IF the user so desires he may add a date text field through clicking a button that says "Add Date". The javascript is as follows:

function addDate() {
           current++;
           var strToAdd = '<p><input type="text" id="date'+current+'" name="classDate'+current+'" class="required" /> <input type="text" id="alt'+current+'" name="classDate'+current+'" class="required" /></p>'
           //console.log(strToAdd)
           $('.dynDate').append(strToAdd)
           $('#date'+current+'').datepicker({altField: '#alt'+current+'', altFormat: 'DD, d MM, yy'});
      }

This adds two date fields to the form along with putting the jquery date picker on the form for each new date field. Exactly like this: http://jqueryui.com/demos/datepicker/#alt-field ...

Anyway, my question is how do I put the data into the MYSQL database with PHP from the dynamic text fields? Do I loop through them or what? I guess I am really not sure how to manipulate dynamic fields very well.

Thanks for any help!

Aaron

+1  A: 

I tend to find it easier to parse if I have something like: classDate_x, where x is the counter, so I can split it later.

But, you can just loop through the names in php until you don't get a match.

I use: isset($_POST['classDate_x']) to determine if there is one. Once I get where one isn't set I just exit my processing loop for that.

Since you are using jquery you may find it easier to just submit the date when the user selects one, as then you can just include the functionality within each dynamic element.

James Black
Thanks a lot, I will have to take a look at that! =)
Ratty
A: 

Name each date field as if it's being added to a PHP array (not the ids):

<input type="text" id="date'+current+'" name="classDate[]" class="required" />

Using the [ ] syntax (sort of an array_push() shortcut), classDate will automatically become an array in $_POST. Then just do a for loop on that array.

wmid
Worked brilliantly...
Ratty