Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
 
Worked brilliantly... –  Ratty Nov 24 '09 at 5:49
add comment

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.

share|improve this answer
 
Thanks a lot, I will have to take a look at that! =) –  Ratty Nov 23 '09 at 16:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.