help needed with saving dynamic generated form fields to DB

SilverNodashi

Expert Member
Joined
Oct 12, 2007
Messages
3,340
Reaction score
48
Location
Johannesburg, South Africa
Hi all,

I need some help with saving dynamic generated form values to the database.

The form has 4 fields: a checkbox, "first name", "email" (textfield), "notes" (textfield)

Basically an admin needs to indicate which users attended a meeting, and also sometimes update their info on the fly.
So, a form gets generated full of names, depending on various criteria, like "age group" / "sex" / "city", etc. Thus the length and content of the forms can vary every time. The admin person then needs to tick the select box next to the name of the person attending, and sometimes update his info on the fly as well. The "Email" & "Notes" textfields are editable and I need to save the info in those textfields as well.

So, I have the following list:

Name Fistname Lastname Email Notes
John Doe [email protected] sick
Jane Smith [email protected]
Joe Soap [email protected] on vacation


etc, etc.

I need to record:
a.) who attended - I got this working fine already
b.) all their updated info. - this is the one which I don't know how todo, since the list is dynamically generated.

For the checkboxes, I have the following:

Code:
<?php
        $k = 0;
        for($i = 0, $n = count($rows); $i < $n; $i++) {
            $row = $rows[$i];
            $y = $k + 1;
            $class = "sectiontableentry" . $y;
            ?>
            <tr>
                <td class="<?php echo $class;?>" align="center" width="50"><?php echo $row->id; ?> (<?php echo$row->userid; ?>)</td>
                <td class="<?php echo $class;?>"><?php echo $row->fullusername; ?></td>
                <td class="<?php echo $class;?>" width="40"><input type="checkbox" id="cb<?php echo $i;?>" name="cid[]" value="<?php echo $row->userid.'-'.$row->fullusername; ?>" onclick="isChecked(this.checked);" /checked ></td>
                <td class="<?php echo $class;?>" align="center"><input type="text" name="notes" id="<?php echo $row->id; ?>"></td>
            </tr>
            <?php
            $k = 1 - $k;
        }
        ?>

Which then produces an HTML list as follows:

Code:
<table cellpadding="3" cellspacing="1" border="0" width="100%">
			<tr>
				<td class="sectiontableheader" align="center" width="20">#</td>
				<td class="sectiontableheader">Naam:</td>
				<td class="sectiontableheader" align="center">Teenwoording:</td>
				<td class="sectiontableheader" align="center">Notas:</td>
			</tr>
					<tr>
				<td class="sectiontableentry1" align="center" width="50">30 (69)</td>
				<td class="sectiontableentry1">Abre van Buuren</td>
				<td class="sectiontableentry1" width="40"><input type="checkbox" id="cb0" name="cid[]" value="69-Abre van Buuren" onclick="isChecked(this.checked);" /checked ></td>
				<td class="sectiontableentry1" align="center"><input type="text" name="notes" id="30"></td>
			</tr>
						<tr>
				<td class="sectiontableentry2" align="center" width="50">31 (71)</td>
				<td class="sectiontableentry2">Berne Botha</td>
				<td class="sectiontableentry2" width="40"><input type="checkbox" id="cb1" name="cid[]" value="71-Berne Botha" onclick="isChecked(this.checked);" /checked ></td>
				<td class="sectiontableentry2" align="center"><input type="text" name="notes" id="31"></td>
			</tr>
						<tr>
				<td class="sectiontableentry1" align="center" width="50">32 (70)</td>
				<td class="sectiontableentry1">Corlea van Buuren</td>
				<td class="sectiontableentry1" width="40"><input type="checkbox" id="cb2" name="cid[]" value="70-Corlea van Buuren" onclick="isChecked(this.checked);" /checked ></td>
				<td class="sectiontableentry1" align="center"><input type="text" name="notes" id="32"></td>
			</tr>
						<tr>
				<td colspan="">&nbsp;</td>
			</tr>
			<tr> 
				<td class="sectiontablefooter" align="center" colspan=""></td>
			</tr>
			<tr> 
				<td class="sectiontablefooter" align="center" colspan=""></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td><input class="button" type="submit" name="submit_list" value="Submit" /></td>
			</tr>
		</table>

Then I capture the results as follows:

Code:
$cid = $_POST["cid"];
foreach ($cid as $value ) {
            $users = explode('-',$value);
            $body .= $users[0].'-'.$users[1]."<BR />";
}


2 problem exists with this approach though:
1. Since both the "cid" & "notes" fields are arrays, I can't get join the data. i.e. I can't reliably see what notes were given with the corresponding users.
2. HTML doesn't submit non-checked checkboxes, so I don't get those rows' data. This also mean I don't get to capture the updated email address & notes for this user, even though he didn't attend. Basically, if a user is sick / on vacation / etc, then we still need to know why he didn't attend a meeting.

So, the question is, how do I capture a whole row in a dynamic table like this, and map the corresponding info to the right user / userid?


P.S. I took the email textfield out of the code to see if I can first get the "notes" field's info collected.
P.P.S. This is a Joomla component
 
How about using a hidden input, eg:
Code:
<input type=hidden name="id[]" value="<?php echo $row->id; ?>">
In this way, it will always return all the entries (rowid with comment).
The checkboxes will still only return if selected, but if you use the same id, as in the hidden field, you will be able to marry the two values.
 
Use a two-dimensional array / hash:


<td class="<?php echo $class;?>" width="40">
<input type="checkbox" id="cb<?php echo $i;?>" name="cid[<?php echo $row->userid; ?>][attended]" value="1" onclick="isChecked(this.checked);" /checked >
<input type="hidden" name="cid[<?php echo $row->userid; ?>][name]" value="<?php echo $row->fullusername; ?>">
</td>
<td class="<?php echo $class;?>" align="center"><input type="text" name="cid[<?php echo $row->userid; ?>][notes]" id="<?php echo $row->id; ?>"></td>


After you submit, loop through $cid with foreach in the same way. You can then check if they attended depending on whether it contains an "attended" key, and the rest of info for each person is available through the other keys. And the index for each array element in the cid array/hash is your id for each person if you need it. Hope it helps.
 
Top
Sign up to the MyBroadband newsletter
X