Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Warning: Invalid argument supplied for foreach()

Posted on 2012-12-30
3
Medium Priority
?
532 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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 111

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 34

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

609 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