Page 1 of 1

Combining two form elements

Posted: Thu Aug 14, 2008 2:12 pm
by veo
Hey all. New to the forums here.

I'm basically creating a timesheet here and I am wondering how to combine two form elements that would be generated dynamically.

the idea is to have one input field for inputing a task and another for inputing time spent doing said task.
how would I go about making sure that each time would get assigned to the correct task. this would eventually be put into a database.

i figured out how to loop through one of them like this.

Code: Select all

if(isset($_POST['submit'])){
    foreach($_POST['detail'] as $detail){
        echo $detai."<br>"l;
        }
}
is there a way to maybe group the form elements...maybe run another foreach for the hours and then combine the arrays?
any ideas are welcome.

Re: Combining two form elements

Posted: Thu Aug 14, 2008 3:12 pm
by nowaydown1
Hi veo,

Welcome to the forum! You could create your form elements so that they would wind up being a multi-dimensional array:

Code: Select all

 
<?php
 
if(count($_POST) > 0) {
    if(is_array($_POST['task'])) {
        foreach($_POST['task'] as $taskDetail) {
            echo "Task was: " . $taskDetail["name"] . "<br>";
            echo "Hours were: " . $taskDetail["hours"] . "<br>";
        }
    }
}
 
?>
 
<form action="<?php print($_SERVER['PHP_SELF']); ?>" method="post" />
 
<table>
    <tr>
        <td>
            Task #1: <input type="text" name="task[0][name]" />
        </td>
        <td>
            Task #1 Hours: <input type="text" name="task[0][hours]" />
        </td>
    </tr>
    <tr>
        <td>
            Task #2: <input type="text" name="task[1][name]" />
        </td>
        <td>
            Task #2 Hours: <input type="text" name="task[1][hours]" />
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" name="add" value="Add Task(s)" />
        </td>
    </tr>
</table>
 
</form>
 

Re: Combining two form elements

Posted: Fri Aug 15, 2008 8:57 am
by veo
perfect...works well. thank you much.