Combining two form elements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
veo
Forum Newbie
Posts: 4
Joined: Thu Aug 14, 2008 2:05 pm

Combining two form elements

Post 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.
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Combining two form elements

Post 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>
 
veo
Forum Newbie
Posts: 4
Joined: Thu Aug 14, 2008 2:05 pm

Re: Combining two form elements

Post by veo »

perfect...works well. thank you much.
Post Reply