Page 1 of 1

Dynamically Add Rows To a Form??

Posted: Tue Jan 03, 2006 1:50 pm
by stylus
Hello,

I am working on a form for my Intranet site. The form is to has a section where the user fills in what accessories go with the product being sold. (We sell Photocopiers).

Here is a place for the base model, and cost, then I need to be able to ask how many accessories did you sell with it? then depending on the answer give them that many box's to fill in.

I plan on having the accessories be inserted in to a seperate table, and then associated by ID.

I have done this using Coldfusion for a online music site, I learned from this tutorial http://tutorial362.easycfm.com/ but I cannot find any simular tutorial in PHP.

On my coldfusion site, I have a page that just asks how many tracks (songs on cd) then post to the next page which displays the input boxes. I would like to have some javascript that can do this or just repost to the same form to get the additional inputs.


here is the coldfusion version that I have so that maybe somebody can see the logic of it and convert to PHP

Code: Select all

<form action="add_tracks_action.cfm" method="post" enctype="multipart/form-data" name="form2" id="form2">
<!--- Need to know how may form fields to process, also need to pass this onto the action page --->
<input type="hidden" name="numba" value="<cfoutput>#form.numba#</cfoutput>">
<input type="hidden" name="album_id" value="<cfoutput>#form.album_id#</cfoutput>">
<table>
<tr><td>Track Number</td><td>Track Name</td><td>Sample File</td></tr>
<cfoutput>
<cfloop from="1" to="#form.numba#" index="idx">
<tr><td><input type="text" name="track_number#idx#" value="#idx#" size="10"></td><td><input type="text" name="track_name#idx#"></td><td><input type="file" name="track#idx#"></td></tr>
</cfloop>
</table>
</cfoutput>
<input type="submit" name="submit" value="Upload">
</form>

Action Page:

Code: Select all

<cfloop from="1" to="#form.numba#" index="idx">
	<!--- process each file upload seperately --->
	<!--- upload the file(s) --->
	<cfset field = "track#idx#">
	<cfif (IsDefined("#field#")) AND (#Evaluate("#field#")# neq "")>
	<cffile action="UPLOAD" filefield="#field#" destination="D:\Sites\ecsmaine.com\wwwroot\klaritymusic\samples" nameconflict="MAKEUNIQUE">
	<!--- insert the record into data base --->
	<cfquery name="data" datasource="klaritymusic">
	<cfoutput>
        insert into tracks (album_id,track_number,track_name,sample)
        values (#form.album_id#,#form["track_number" & idx]#,'#form["track_name" & idx]#','#cffile.serverFile#')
</cfoutput>
</cfquery>
<cfelse>
	<cfquery name="data" datasource="klaritymusic">
	<cfoutput>
        insert into tracks (album_id,track_number,track_name)
        values (#form.album_id#,#form["track_number" & idx]#,'#form["track_name" & idx]#')
</cfoutput>
</cfquery>


</cfif>
</cfloop>
Thanks

Posted: Tue Jan 03, 2006 5:09 pm
by pickle
Reading through the tutorial - it seemed like the user was asked how many titles there were, then they submitted that form. The resulting page had a second form with the requested number of fields.

That's still quite simple to do with PHP:

Initial Form

Code: Select all

.....
<form name = "field_number_form" action = "resulting_page.php" method = 'post'>
How many fields?: <input type = "text" name = "field_count">
<input type = "submit">
</form>
.....
Resulting page

Code: Select all

<?PHP
$field_count = $_POST['field_count']

//setup the form
echo <<<FORM
<form name = "customized_form" action = "some_other_page.php" method = 'post'>
FORM;


//loop through and make $field_count fields
for($i= 1;$i <= $field_count;++$i)
{
   echo <<<NEWFIELD
Field $i: <input type = "text" name = "custom_field_$i"><br />
NEWFIELD;

}

//close up the form
echo '</form>';
?>

Posted: Wed Jan 04, 2006 12:18 pm
by stylus
I am getting a parse error, from this statement

Code: Select all

for($i= 1;$i <= $field_count;++$i)
The Error is
"Parse error: parse error, unexpected T_FOR in D:\Data\Inetpub\wwwroot\add_marginworksheet.php on line 77"

here is the full code:

Code: Select all

<table width="100%" cellpadding="0" cellspacing="0" border="0" align="right">
					<?PHP 
						$field_count = $_POST['field_count'] 
						for($i= 1;$i <= $field_count;++$i) 
							{ 
   							echo <<<NEWFIELD 
							Field $i: 
							<tr>
								<td align="left" width="90">Model:<br><input type="text" size="10" name="model_$i"></td>
				            	<td align="left">Description:<br><input type="text" size="40" name="description_$i"></td>
								<td align="right">Price:<br>$<input type="text" size="10" name="price_$i"></td>
							</tr>
						NEWFIELD; 
							} 
					?> 
				</table>

Posted: Wed Jan 04, 2006 12:24 pm
by hawleyjr

Code: Select all

for($i= 1;$i <= $field_count;++$i)
Should be:

Code: Select all

for($i= 1;$i <= $field_count;$i++)

Posted: Wed Jan 04, 2006 12:31 pm
by stylus
I made that change, and still get the same parse error message.

"Parse error: parse error, unexpected T_FOR in D:\Data\Inetpub\wwwroot\add_marginworksheet.php on line 77"