Current location: Hot Scripts Forums » Programming Languages » PHP » help needed with saving dynamic generated form fields to DB


help needed with saving dynamic generated form fields to DB

Reply
  #1 (permalink)  
Old 07-20-10, 06:55 AM
SoftDux SoftDux is offline
Newbie Coder
 
Join Date: Mar 2008
Location: Johannesburg,South Africa
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
help needed with saving dynamic generated form fields to DB

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 john@doe.net sick
Jane Smith jane@blah.com
Joe Soap joe@soap.tv 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-22-10, 04:25 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I think you are going about this the wrong way.
The checkboxes should have their own column in the database and they should have nothing to do with the persons data other than to indicate whether they are attending or not.
In which case the checkbox column will be either on or null.
All the rest of the persons data should be collected in their own input fields.
The names of the input fields should be all arrays including the checkbox fields.
All the arrays should be identified with a value starting with 0.
That way, if a checkbox isn't checked, then the corresponding array element for that checkbox will not be there and can be easily verified using PHP's !empty() function.

You should be able to end up with an output that looks something like this:
PHP Code:

<?php
if(!empty($_POST["submit_list"])) {
 
$id $_POST["id"];
 
$name $_POST["name"];
 
$cid $_POST["cid"];
 
$notes $_POST["notes"];
 
$p=0;
 
$patterns = array("(",")");
 for(
$i=0;$i<count($id);$i++)
 {
  if(!empty(
$id[$i])){$body[$p] .= str_replace($patterns,"",$id[$i])."-";}
  if(!empty(
$name[$i])){$body[$p] .= $name[$i]." - ";}
  if(!empty(
$cid[$i])){$body[$p] .= " Attending ";}
  else{
$body[$p] .= " Not attending ";}
  if(!empty(
$notes[$i])){$body[$p] .= "- ".$notes[$i];}
  
$p++;
  }
 echo 
"<pre>";
 
print_r($body);
 echo 
"</pre>";
}
?>
<html>
<head>
</head>
<body>
<form action="#" method="POST">
<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 <input type="text" name="id[0]" value="(69)" readonly style="border:none;width:25px;"></td>
                <td class="sectiontableentry1"><input type="text" name="name[0]" value="Abre van Buuren" readonly style="border:none;"></td>
                <td class="sectiontableentry1" width="40"><input type="checkbox" id="cb0" name="cid[0]" onclick="isChecked(this.checked);" /></td>
                <td class="sectiontableentry1" align="center"><input type="text" name="notes[0]" id="30" value="Sick"></td>
            </tr>
                        <tr>
                <td class="sectiontableentry2" align="center" width="50">31 <input type="text" name="id[1]" value="(71)" readonly style="border:none;width:25px;"></td>
                <td class="sectiontableentry2"><input type="text" name="name[1]" value="Berne Botha" readonly style="border:none;"></td>
                <td class="sectiontableentry2" width="40"><input type="checkbox" id="cb1" name="cid[1]" onclick="isChecked(this.checked);" /></td>
                <td class="sectiontableentry2" align="center"><input type="text" name="notes[1]" id="31" value="On vacation"></td>
            </tr>
                        <tr>
                <td class="sectiontableentry1" align="center" width="50">32 <input type="text" name="id[2]" value="(70)" readonly style="border:none;width:25px;"></td>
                <td class="sectiontableentry1"><input type="text" name="name[2]" value="Corlea van Buuren" readonly style="border:none;"></td>
                <td class="sectiontableentry1" width="40"><input type="checkbox" id="cb2" name="cid[2]" onclick="isChecked(this.checked);" /checked ></td>
                <td class="sectiontableentry1" align="center"><input type="text" name="notes[2]" id="32" value=""></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>
</form>
</body>
</html>
I don't know why all the checkboxes are checked.
You should be checking to see if they have a value in the database and only marking them as checked if the database shows a value.
__________________
Jerry Broughton

Last edited by job0107; 07-22-10 at 05:03 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to job0107 For This Useful Post:
mysamsung (07-23-10)
  #3 (permalink)  
Old 07-23-10, 06:34 AM
mysamsung mysamsung is offline
Newbie Coder
 
Join Date: Apr 2010
Posts: 22
Thanks: 36
Thanked 0 Times in 0 Posts
you have solved my problem
__________________
PHP
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Space Above Form xavier039 CSS 1 07-13-09 11:52 PM
help needed for DB search form tigergrai JavaScript 4 03-20-09 12:25 AM
Need Your HelP! Loading Multiple External Text into Multiple Dynamic Text Fields Flash_Boi Flash & ActionScript 2 03-30-06 04:27 PM
formmail problem gscraper Perl 12 08-27-04 04:06 AM
A simple form to upload fields to an Access Db techknow Script Requests 1 04-19-04 11:11 AM


All times are GMT -5. The time now is 10:31 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.