Help: Dynamic Tabular Input - JQuery/PHP Question

Hi all,

So I have a question which I haven’t found an answer to. I know that when collecting tabular input you need to add [$i] to your fields (in my case ‘[$i]distance’) and that $i needs to be an index from 0 to x depending on the number of elements on the form.

I’m jusing JQuery to clone a div which contains my “many” inputs whoever I’m not sure how to dynamically update $i for each row of inputs.

Really Simple jquery:


	function addSet()

	{

		$("#set:last").clone().appendTo("#set_wrapper");

	}

Example of input row:




				<td style="vertical-align:top">

					<?php echo $form->labelEx($set,'[0]distance'); ?>

					<?php echo $form->textField($set,'[0]distance',array('size'=>4,'maxlength'=>4)); ?>

					<?php echo $form->error($set,'[0]distance'); ?>

				</td>

				

If I add rows manually and manually adjust $i the form works fine, I’m just running into a wall setting $i dynamically when adding inputs with JQuery.

I’d really like to keep the page dynamic so that I’m not forcing people to use a static number of inputs.

Anybody have any suggestions?

**Sorry for double post, forum threw an error at me

I managed to solution to this problem that incorporates PHP and JQuery. It ended up being a smarter solution which helps protect the site from users "spamming" a feature.

First I used a simple for loop to iterate through the "sets" input.


for($i=0; $i<=$maxsets; $i++)

	{

		echo "<div class='row set' id='set_".$i."'><table><tr><td style='vertical-align:top'>";

			echo $form->labelEx($set,'['.$i.']distance'); 

			echo $form->textField($set, '['.$i.']distance',array('size'=>4,'maxlength'=>4)); 

			echo $form->error($set,'['.$i.']distance');

Then I used jquery to hide all but the first set div


$(document).ready(function()

	{

		for(j=1; j<=max; j++)

		{

			$("#set_"+j).hide();

		}

	});

At the bottom of my page I have two buttons “Add Set” and “Remove Set” which simply show or hide additional input rows. I need to go back in and make sure I remove any user input when I remove a row because my actionCreate method checks null rows before it saves and simply “hiding” a row doesn’t clear the data if a user has already entered something.

Anyway thought I’d share in case someone else was running into this problem.