Forum Moderators: coopster
I have a pretty simple project schedule sort of web page that lists a number of tasks & 4 dates associated with each task. (planned start, planned complete, actual start, actual complete.
The page is built dynamically based on what is in a mysql database, and the dates listed are form fields, with the intent that a person with access can change any number of these dates.
Each task has an id associated with it, so when i build the form, I name the form field <task_id>-<date type>. so, for example, for task id = 10 I have 4 related form fields:
10-planned_start
10-planned_complete
10-actual_start
10-actual_complete
This basic pattern is repeated for all the tasks (up to 100 right now, but it could grow slightly).
Once all the form fields have been updated, and the user clicks submit I want to update the data in the database with the appropriate data.
So, my questions is:
1) How can I process this form, when I don't know what the names of the fields are going to be, and/or how many fields there are?
2) Is there a good way to identify only those fields that have changed and do updates on only those fields, or am I "stuck" updating all the records, even those where the dates have not changed from when the form was built?
- Brian
When you post the form, you can get the names of all the fields by grabbing the keys from the POST array. Try something like this and you will be able to see what I mean:
echo '<pre>'; print_r([url=http://us2.php.net/manual/en/function.array-keys.php]array_keys[/url]($_POST)); echo '</pre>';
Then you'd have to loop through the 'keys' and create your query from it.
I have 1 form where a person enters a project, and an end date. Based on that end date, the database is populated with a "first stab" at when each task should be started and completed. But these dates can change, and I need a way to allow the person in charge of managing this page the ability to edit the actuals as well.
Currently, the page is one large form, but I am certainly open to ideas about a better way to set things up, if it makes more sense to do so.
I actually mispoke slightly in my original post. The form is organized by a High Level ID, and Task ID (which is basically a subcomponenet of the High Level Id) so there are 3 parts to the dynmically generated form name. Here's an Example, if it helps:
<table>
<tr>
<td align="right"> Hardware Equipment Plan Completed </td>
<td > <input type="text" name="40-21-planned_start" size="10" VALUE="2007-01-01"></td>
<td ><input type="text" name="40-21-planned_finish" size="10" VALUE="2007-01-01"> </td>
<td ><input type="text" name="40-21-actual_start" size="10" VALUE="2007-03-03"> </td>
<td ><input type="text" name="40-21-actual_finish" size="10" VALUE=""> </td><tr><td align="right"> Audio Visual Plan Complated <br></td>
<td > <input type="text" name="40-22-planned_start" size="10" VALUE="2007-02-01"></td>
<td ><input type="text" name="40-22-planned_finish" size="10" VALUE="2007-02-28"> </td>
<td ><input type="text" name="40-22-actual_start" size="10" VALUE=""> </td>
<td ><input type="text" name="40-22-actual_finish" size="10" VALUE=""> </td>
<tr><td align="right"> Tell Comm Plan Completed </td>
<td > <input type="text" name="40-23-planned_start" size="10" VALUE="2007-02-01"></td>
<td ><input type="text" name="40-23-planned_finish" size="10" VALUE="2007-02-28"> </td>
<td ><input type="text" name="40-23-actual_start" size="10" VALUE=""> </td>
<td ><input type="text" name="40-23-actual_finish" size="10" VALUE=""> </td>
</tr>
</table>
The actual number of tasks (i.e. table rows) can change at any given time, based on the number of tasks related to a particular effort.
I'd like to be able to determine what has changed since the page is loaded, although I'm not sure if that's possible or not, and then update the values in the database for those changed fields.
I guess my initial, basic question is how can I process any of the form fields if I don't know their name until the form is built. DO I need to create some sort of array of form field names and then do a loop through that array when the submit button is pressed, or is there a better way?
I only ask because I am sure this scenario has been talked about a million times, and there are probably some good examples I could work from, but I haven't had much luck finding them based on the searches I'e been doing.
Not sure if that clarifies what I am looking for, but I hope it helps.
For each plan you add the text fields as you already have, then another set of hidden fields with names like "orig-40-21-planned_start" "orig-40-21-planned_finish" etc, and set them to the current values in db, and add one more hidden field called "plan[]" set to the unique plan ID, e.g. "40-21".
Now when you process the form:
foreach($_POST['plan'] as $planID) {
if($_POST[$planID.'-planned_start']!= $_POST['orig-'.$planID.'-planned_start']
¦¦ $_POST[$planID.'-planned_end']!= $_POST['orig-'.$planID.'-planned_end']
¦¦ ...) {
update db;
}
Note this doesn't take into account concurrent changes to the DB, ie when the DB is changed between the time the form is initially loaded and submitted. For that, instead of using the "orig-" hidden fields, compare the posted values to whatever's in the db.