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);
}
?>

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.

Attachments
<?php # Script 7.3 - orderentry.php
include ('style.html');
$page_title = 'Order Entry';

// Check if the form has been submitted.
if (isset($_POST['submitted'])) {

	$errors = array(); // Initialize error array.

	// Trim Select Dropdown
	{
		$se = trim($_POST['select']);
	}
	
	// Trim Company
	{
		$co = trim($_POST['company']);
	}

	// Trim Contact
	{
		$cn = trim($_POST['contact']);
	}

	// Trim Address
	{
		$ad = trim($_POST['address']);
	}

	// Trim Address2
	{
		$ad2 = trim($_POST['address2']);
	}

	// Trim City
	{
		$ct = trim($_POST['city']);
	}

	// Trim State/Province
	{
		$sp = trim($_POST['state_prov']);
	}


	// Trim Zip/Postal
	{
		$zp = trim($_POST['zip_postal']);
	}
	
	// Trim Country
	{
		$cou = trim($_POST['country']);
	}


	// Trim Phone
	{
		$ph = trim($_POST['phone']);
	}

	// Trim Email
	{
		$em = trim($_POST['email']);
	}


	// Trim Fax
	{
		$fx = trim($_POST['fax']);
	}

	// Trim Tax ID
	{
		$tx = trim($_POST['tax_id']);
	}

	// Trim Shipping Company
	{
		$cos = trim($_POST['sh_company']);
	}

	// Trim Shipping Contact
	{
		$cns = trim($_POST['sh_contact']);
	}

	// Trim Shipping Address
	{
		$ads = trim($_POST['sh_address']);
	}

	// Trim Shipping Address 2
	{
		$a2s = trim($_POST['sh_address2']);
	}


	// Trim Shipping City
	{
		$cts = trim($_POST['sh_city']);
	}


	// Trim Shipping State/Province
	{
		$sps = trim($_POST['sh_state_prov']);
	}

	// Trim Shipping Zip/Postal
	{
		$zps = trim($_POST['sh_zip_postal']);
	}
	
	// Trim Shipping Country
	{
		$cous = trim($_POST['sh_country']);
	}

	// Trim Shipping Phone
	{
		$phs = trim($_POST['sh_phone']);
	}

	// Trim Shipping Email
	{
		$ems = trim($_POST['sh_email']);
	}

	// Trim Shipping Fax
	{
		$fxs = trim($_POST['sh_fax']);
	}

	// Trim Order Date
	{
		$dt = trim($_POST['date']);
	}

	// Trim Order P.O.
	if (empty($_POST['po'])) {
		$errors[] = 'You forgot to enter the P.O.';
	} else	{
		$po = trim($_POST['po']);
	}

	// Trim Invoice Date
	{
		$idt = trim($_POST['invoice_date']);
	}

	// Trim Invoice Number
	{
		$in = trim($_POST['invoice']);
	}

	// Trim Sent to Manufacturer
	{
		$mfg = trim($_POST['sent_mfg']);
	}

	// Trim Requested Ship Date
	{
		$re = trim($_POST['req_date']);
	}

	// Trim Final Ship Date
	{
		$fn = trim($_POST['final_date']);
	}

	// Trim Shipped Via
	{
		$sh = trim($_POST['shipped_via']);
	}

	// Trim Tracking #
	{
		$tr = trim($_POST['tracking']);
	}
	
	// Trim Tracking #
	{
		$ia = trim($_POST['inv_amount']);
	}
	
	// Trim Tracking #
	{
		$fr = trim($_POST['freight']);
	}

// Trim status #
	{
		$status = trim($_POST['status']);
	}

// Trim Tracking #
	{
		$currency = trim($_POST['currency']);
	}

// Trim Tracking #
	{
		$povalue = trim($_POST['po_value']);
	}


	if (empty($errors)) { // If everything's okay.
	
		// Register the user in the database.
		require_once ('mysql_connect.php'); // Connect to the db.
		
		// Make the query.
		$query = "INSERT INTO customers_log (customer_id, company, contact, address, address2, city, state_prov, zip_postal, country, phone, email, fax, tax_id) VALUES ('', '$co', '$cn', '$ad' , '$ad2', '$ct', '$sp', '$zp', '$cou', '$ph', '$em', '$fx', '$tx')";
		$result = @mysql_query ($query); // Run the query.
		$customer_id = mysql_insert_id();
		
		$query = "INSERT INTO shipping (shipping_id, sh_company, sh_contact, sh_address, sh_address2, sh_city, sh_state_prov, sh_zip_postal, sh_country, sh_phone, sh_email, sh_fax, customer_id) VALUES ('', '$cos', '$cns', '$ads' , '$a2s', '$cts', '$sps', '$zps', '$cous', '$phs', '$ems', '$fxs', '$customer_id')";	
		$result = @mysql_query ($query); // Run the query.
		$shipping_id = mysql_insert_id();
		
		$query = "INSERT INTO orders (order_id, date, po, invoice_date, invoice, sent_mfg, req_date, final_date, shipped_via, freight, inv_amount, tracking, shipping_id, status, currency, po_value) VALUES ('', '$dt', '$po', '$idt' , '$in', '$mfg', '$re', '$fn', '$sh', '$ia', '$fr', '$tr', '$shipping_id', '$status', '$currency', '$povalue')";
		$result = @mysql_query ($query); // Run the query.
		$order_id = mysql_insert_id();
		
		//$result = @mysql_query ($query); // Run the query.
		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);}
		
		if ($result) { // If it ran OK.
		
			// Send an email, if desired.
			
			// Print a message.
			echo '<h1 id="mainhead">Thank you!</h1>
		<p>Your entry has now been registered.</p><p><br /></p>';	
		
			// Include the footer and quit the script (to not show the form).
 
			exit();
			
		} else { // If it did not run OK.
			echo '<h1 id="mainhead">System Error</h1>
			<p class="error">Your entry could not be registered due to a system error. We apologize for any inconvenience.</p>'; // Public message.
			echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message.
 
			exit();
		}
		
		mysql_close(); // Close the database connection.
		
	} else { // Report the errors.
	
		echo '<h1>Error!</h1>
		<p class="error">The following error(s) occurred:<br />';
		foreach ($errors as $msg) { // Print each error.
			echo " - $msg<br />\n";
		}
		echo '</p><p>Please try again.</p><p><br /></p>';
		
	} // End of if (empty($errors)) IF.

} // End of the main Submit conditional.
?>
<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()
			{
				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]' />";			
			}

			
			//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>
<html xmlns:spry="http://ns.adobe.com/spry">
<style>
body {
	margin:0 0 0 0;
	font-family:"Verdana", Geneva, sans-serif;
	font-size:10px;
	width:700px;
}
#row {
	width:695px;
}
.customerdrop {
	height:20px;
	font-family:"Verdana", Geneva, sans-serif;
	font-size:10px;
 .select {
 width:100px;
}
}
</style>
<script src="SpryAssets/xpath.js" type="text/javascript"></script>
<script src="SpryAssets/SpryData.js" type="text/javascript"></script>
<script type="text/javascript">
<!--
var ds1 = new Spry.Data.XMLDataSet("customers.xml", "customers/customer");
//-->
</script>
<div id="body">
  <form method="post" action="orderentry.php">
    <div spry:detailregion="ds1">
    <table width="695" border="0" cellspacing="5" cellpadding="0" style="table-layout:fixed" id="orderentry">
      <tr>
        <td colspan="4"><strong>Customer Info:</strong>&nbsp;&nbsp;&nbsp;
          <select name="select" spry:choose="choose" class="customerdrop" spry:repeatchildren="ds1" onChange="ds1.setCurrentRowNumber(this.selectedIndex)">
            <option spry:when="{ds_RowNumber} == {ds_CurrentRowNumber}" value="{company}">{company}</option>
            <option spry:default="default" value="{company}">{company}</option>
          </select>
          &nbsp;&nbsp;&nbsp;<a href="cust_xml.php" target="_blank"><img src="images/update.jpg" alt="Update" border="0" /></a>&nbsp;Click to Update Customer Dropdown then refresh page.</td>
      </tr>
      <tr>
        <td>Company:</td>
        <td><input name="company" type="text" id="company" value="{company}" /></td>
        <td>Contact:</td>
        <td><input name="contact" type="text" id="contact" value="{contact}" /></td>
      </tr>
      <tr>
        <td>Address:</td>
        <td><input name="address" type="text" id="address" value="{address}" /></td>
        <td>Address2:</td>
        <td><input name="address2" type="text" id="address2" value="{address2}" /></td>
      </tr>
      <tr>
        <td>City</td>
        <td><input name="city" type="text" id="city" value="{city}" /></td>
        <td>State/Province:</td>
        <td><input name="state_prov" type="text" id="state_prov" value="{state_prov}" /></td>
      </tr>
      <tr>
        <td>Zip/Postal:</td>
        <td><input name="zip_postal" type="text" id="zip_postal" value="{zip_postal}" /></td>
        <td>Country:</td>
        <td><input type="text" name="country" id="country" value="{country}" /></td>
      </tr>
      <tr>
        <td>Email:</td>
        <td><input type="text" name="email" id="email" value="{email}" /></td>
        <td>Phone:</td>
        <td><input type="text" name="phone" id="phone" value="{phone}" /></td>
      </tr>

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....

This article has been dead for over six months. Start a new discussion instead.