Solved

Warning: Invalid argument supplied for foreach()

Posted on 2012-12-30
3
504 Views
Last Modified: 2013-01-05
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

0
Comment
Question by:t3chguy
3 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38731088
$_POST is an array, but $_POST[$field] is you indexing an array. Unless you put an array into $_POST[$field], then that line doesn't make much sense.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 38731096
PHP var_dump() is your friend.  Try var_dump($_POST) just before line 12 and see what you get.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 38732940
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?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now