Hi Guys,

I really hope someone can help me out here I have a php form that submits to a database which is all fine, a portion of the form allows for more rows to be added as needed but for some reason it only allows for upto 9 rows to be submitted then it just loops back through the first 9 and i have no idea why. Any help is greatly appreciated.

<script language="javascript">
			//add a row to the rows collection and get a reference to the newly added row
				var partsRowCount = 1; //stores the number of rows
		
			//add a new row to the table
			function addRow()
			{
				var namePrefix = "_" + (+partsRowCount + 1); //setup the prefix variable
				
				partsRowCount++; //increment the counter
				
				//add a row to the rows collection and get a reference to the newly added row
				var newRow = document.all("orderentry").insertRow();
				
				//add 3 cells (<td>) to the new row and set the innerHTML to contain text boxes
				
				var oCell = newRow.insertCell();
				oCell.innerHTML = "<input type='text' name='partid" + namePrefix + "'>";
				
				oCell = newRow.insertCell();
				oCell.innerHTML = "<input type='text' name='quantity" + namePrefix + "'>";

				oCell = newRow.insertCell();
				oCell.innerHTML = "<input type='text' name='desc" + namePrefix + "'>";
				
				oCell = newRow.insertCell();
				oCell.innerHTML = "<input type='text' name='serial" + namePrefix + "'>";			
			}
			
			//deletes the specified row from the table
			function removeRow(src)
			{
				/* src refers to the input button that was clicked.	
				   to get a reference to the containing <tr> element,
				   get the parent of the parent (in this case case <tr>)
				*/			
				var oRow = src.parentElement.parentElement;		
				
				//once the row reference is obtained, delete it passing in its rowIndex			
				document.all("orderentry").deleteRow(oRow.rowIndex);		
			}
		
		</script>

what is the code you write to get the values from the form and store into the database ??

//$result = @mysql_query ($query); // Run the query.
		foreach($_POST as $key => $value)
	{
		//looping through all posted fields
		if(strpos($key, 'partid_') !== false)
		{
			//found a part field, need to find out the _x number, then insert the entire row
			$suffix = substr($key, -1); //this gets the _x number
			$partid = $_POST['partid_' . $suffix];
			$quantity = $_POST['quantity_' . $suffix];
			$desc = $_POST['desc_' . $suffix];
			$serial = $_POST['serial_' . $suffix];
			
			$query = "INSERT INTO parts_ordered (partid, quantity, `desc`, serial, order_id) VALUES ('$partid', '$quantity', '$desc', '$serial', '$order_id')";
			$result = mysql_query($query);

I have a few suggestions if you don't mind. I have just written a script a lot like this (if you want to see an example pm me, since my employers site). To make it easier to insert, i set the field names as a array with the id as the key.

I made some edits to your add function. I think you will understand how to go from here. If not let me know and i can help with the rest.

function addRow()
			{
				partsRowCount++; //increment the counter
				
				//add a row to the rows collection and get a reference to the newly added row
				var newRow = document.all("orderentry").insertRow();
				
				//add 3 cells (<td>) to the new row and set the innerHTML to contain text boxes
				
				var oCell = newRow.insertCell();
				oCell.innerHTML = "<input type='text' name='cfield["+partsRowCount+"][partid]' />";
				
				oCell = newRow.insertCell();
				oCell.innerHTML = "<input type='text' name='cfield["+partsRowCount+"][quantity]' />";

				oCell = newRow.insertCell();
				oCell.innerHTML = "<input type='text' name='cfield["+partsRowCount+"][desc]' />";
				
				oCell = newRow.insertCell();
				oCell.innerHTML = "<input type='text' name='cfield["+partsRowCount+"][serial]' />";			
			}

Now all you need to do for the database insertion is do a foreach on the $_POST and build the query. This should fix your issue and be much easier.

Not tested but this should work for the SQL. using your same method as foreach on $_POST
I took the orderby out. since it wasn't used in the provided code.

<?php
foreach($_POST as $key => $value)
{
	if ($key == 'cfield') {
		if (is_array($value)) {
			$sqlQuery = '';
			foreach ($value as $field) {
				$sqlQuery .= "('".escape($field['partid'])."', '".escape($field['quantity'])."', '".escape($field['desc'])."', '".escape($field['serial'])."'),";
			}
			$sqlQuery = rtrim($sqlQuery, ',');
			$query = "INSERT INTO parts_ordered (partid, quantity, `desc`, serial) VALUES ($sqlQuery)";
			$result = mysql_query($query);
		}
	}
}

function escape($string) {
	if(get_magic_quotes_gpc()){
		$string = stripslashes($string);
	}
	return mysql_real_escape_string($string);
}
?>

is your problem solved ?

I wish i could say it was solved but sadly it is not, its doing the same thing cutting off at 9 and submitting a loop for anything over that.

I have attached the whole file for anyone that can maybe help me out.

i work in asp and have no idea of php...
i did an application like the one you are doing a few days back..
this link shows some of the code i used to add rows and all..
may be it will be of some help to you...

and then i pass the values looping the elements on their id's....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.