Link to home
Start Free TrialLog in
Avatar of t3chguy
t3chguyFlag for United States of America

asked on

Warning: Invalid argument supplied for foreach()

Not sure what I'm doing wrong, but I've been up all night trying to catch up on some work.  I'm sure this is just a simple oversight, but the error is occurring on Line 20, which is where I have foreach($_POST[$field] as $key => $line)

<?php

$programid = "2064";

require_once($_SERVER['DOCUMENT_ROOT']."/includes/verifyaccess.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/mainfunctions.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/expFunctions.php");
require_once($_SERVER['DOCUMENT_ROOT']."/includes/header-lightbox.php");

$workrecordnum = safeData($_GET['id']);

if(isset($submit))
	{
	$fields = array('partnumber','partdescription','qty','cost');
	$worknumid = $_POST['workrecordnum'];
	
	
	foreach ($fields as $field) 
		{
		foreach ($_POST[$field] as $key=>$line) 
			{
			$returnArray[$key][$field] = $line;								
			}
		}
						
		foreach ($returnArray as $lineItem) 
			{
			if (!empty($lineItem['partnumber']) && !empty($lineItem['partdescription']) && !empty($lineItem['cost'])) 
				{
				// Dump into DB.
				$partnum = safeDataMySQL($lineItem['partnum']);
				$partdesc = safeDataMySQL($lineItem['partdesc']);								
				$qty = strtotime($lineItem['qty']);
				$amount = strtotime($lineItem['amount']);

				$query = "INSERT INTO `cr_memos_lines` (
													items_id,
													partNo,
													description,
													qty,
													cost)
							VALUES ('".$worknumid."',
									'".$partnum."',
									'".$partdesc."',
									'".$qty."',
									'".$amount."'
									)	";
							//or die(mysql_error());
							
				echo $query . '<br /><br />';
				}
			}
	}
?>

<style type="text/css">
.required {
	background-color:#b6c0b2;
}
.errors {
	color:#F7473D;
	font-weight:bold;
	font-size:15px;
}
.formBox {
	border: 1px solid #2D2D2D;
    color: #000000;
    font-family: arial;
    font-size: 13px;
    font-weight: bold;
    padding: 2px;
	width:295px;
	height:80px;
}
</style>

<form id="addLines" method="POST" action="<?=$_SERVER['PHP_SELF']?>">

	<table border="0" cellspacing="0" cellpadding="0" class="lineTbl" width="100%">
				<thead>
				<tr>
					<th class="columnheader" align="left">Part  #</th>
					<th class="columnheader" align="left">Description of Part</th>
					<th class="columnheader" align="left">Quantity</th>
					<th class="columnheader" align="left">Cost</th>
				</tr>
				</thead>
				<tbody class="lineBdy">
				<?php for ($x=1; $x<=1; $x++) { 
				echo "<tr id='line_".$x."' class='lineRow'>";
				
				$color_A = 'class="alt2_1"'; 
				$color_B = 'class="alt2_2"';

				$row_color = ($x % 2) ? $color_A : $color_B;
				
				?>				

					<td <?=$row_color?>><input type="text" class="serial required" name="partnum[]" size="10" /></td>
					<td <?=$row_color?>><input type="text" class="serial required" name="partdesc[]" size="75" /></td>
					<td <?=$row_color?>>
						<select name="qty[]" class="serial required">
						<?php
							for ($i=1; $i<=100; $i++) {
								echo "<option value='".$i."'>".$i."</option>";
							}
						?>
						</select>
					</td>
					<td <?=$row_color?>><input type="text" class="credit_amt serial cost required" name="amount[]" size="10" /></td>
				</tr>				
				<?php } ?>
				</tbody>
			</table>
			<div align="center"><br /><input type="button" value="Add Line" class="add"/> | <input type="submit" name="submit" value = "Finalize Order"></div>
			<!--Hidden Variables-->
			<input type="hidden" name="workrecordnum" value = "<?=$workrecordnum?>">
			</form>
			
<script type="text/javascript">
	$("document").ready(function(){
		
		$('.cost').blur(function () {
			var sum = 0;
			$('.cost').each(function() {
				num = $(this).val().replace('$', '');
				sum += Number(num);
			});
			
			$('#grandtotal').html(Math.round(sum*100)/100);
		});
				
		$('.start:first').change(function(){
			var start = $(this).val();
			$('.start').val(start);
		});
		$('.end:first').change(function(){
			var end = $(this).val();
			$('.end').val(end);
		});
		
		<?php
			if (!empty($fullChecked)) {
				echo "$('#fullInvoice').show();";
				echo "$('#indivLines').hide();";
			} elseif (!empty($portionChecked)) {
				echo "$('#fullInvoice').hide();";
				echo "$('#indivLines').show();";
			}
			
			
			if (!empty($cloneData['memoMailing'])) {
				echo "$('.mail_to').show();";
			} 
		?>
});
	$(function() {
		$('.amount').blur(function() {
			$(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: 2 });
		})
		.keyup(function(e) {
			var e = window.event || e;
			var keyUnicode = e.charCode || e.keyCode;
			if (e !== undefined) {
				switch (keyUnicode) {
					case 16: break; // Shift
					case 17: break; // Ctrl
					case 18: break; // Alt
					case 27: this.value = ''; break; // Esc: clear entry
					case 35: break; // End
					case 36: break; // Home
					case 37: break; // cursor left
					case 38: break; // cursor up
					case 39: break; // cursor right
					case 40: break; // cursor down
					case 78: break; // N (Opera 9.63+ maps the "." from the number key section to the "N" key too!) (See: http://unixpapa.com/js/key.html search for ". Del")
					case 110: break; // . number block (Opera 9.63+ maps the "." from the number block to the "N" key (78) !!!)
					case 190: break; // .
					default: $(this).formatCurrency({ colorize: true, negativeFormat: '-%s%n', roundToDecimalPlace: -1, eventOnDecimalsEntered: true });
				}
			}
		});
	});
	</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PHP var_dump() is your friend.  Try var_dump($_POST) just before line 12 and see what you get.
greetings   t3chguy, , you do not have "Matching" _POST Key strings in your code you have the name as

name="partnum[]"  and  name="partdesc[]"

BUT you have the fields as =
$fields = array('partnumber','partdescription','qty','cost');

I would think it should be something like =
$fields = array('partnum','partdesc','qty','amount');

but I am not sure about all you intend to do in your code?