0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Thanks a lot, I will have to take a look at that! =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.