Warning: Invalid argument supplied for foreach()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webdev
Forum Commoner
Posts: 25
Joined: Tue Jun 05, 2007 2:07 am

Warning: Invalid argument supplied for foreach()

Post by webdev »

I get an error of Warning: Invalid argument supplied for foreach() in /Library/WebServer/Documents/australianreit/contact/mail.php on line 138

Here is the full code you guys to see:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Australian Real Estate Investment Limited</title>
<meta http-equiv="Content-Type" content="text/html; charset=macintosh" />
<LINK href="../common/areitstyle.css" rel="stylesheet" type="text/css">
</head>

<BODY>
<DIV id="wrapper_bg_left">
<DIV id="wrapper_bg_right">
<DIV id="wrapper_bg_left2">
<DIV id="wrapper_bg_right2">
<DIV id="wrapper_fullheight_left">
<DIV id="wrapper_fullheight_right">
<DIV id="wrapper_leftbg">
<DIV id="wrapper_leftbg2">
<DIV id="wrapper_rightbg">
<DIV id="wrapper_rightbg2">

<DIV id="header">
	<DIV id="header_logo"></DIV>
</DIV>

<!-- NAVIGATION -->
<DIV id="nav_wrapper" class="menu">
<UL>

<!-- HOME -->
<LI>
<DIV class="home_line">
<a href="../index.html" title="Home" class="home">HOME</a>
</DIV>
</LI>

<!-- ABOUT US -->
<LI>
<DIV class="aboutus_line">
<a href="#" title="About Us" class="aboutus">ABOUT US</a>
</DIV>

<UL>
<LI><a href="../aboutus/overview.html">Overview</a></LI>

<LI>
<a class="drop" href="#" title="The Board">The Board</a>

<UL>
<LI><a href="../aboutus/sydney.html" title="Sydney Chesson">Sydney Chesson</a></LI>
<LI><a href="../aboutus/simon.html" title="Simon Chesson">Simon Chesson</a></LI>
<LI><a href="../aboutus/jeanette.html" title="Jeanette Chesson">Jeanette Chesson</a></LI>
</UL>
</LI>
<LI><a href="../aboutus/management.html">Management Team</a></LI>
</UL>
</LI>
</UL>

<!-- OPEN INVESTMENT -->
<UL class="noindent">
<LI>
<DIV class="openinvestment_line">
<a href="#" title="Open Investment" class="openinvestment">OPEN INVESTMENTS</a>
</DIV>

<UL>
<LI><a href="../openinvestments/diversified.html" title="AREIT Diversified Fund">AREIT Diversified Fund</a></LI>
<LI><a href="../openinvestments/development.html" title="AREIT Augusta Development Fund">AREIT Augusta Development Fund</a></LI>
<LI><a href="../openinvestments/mortgage.html" title="Agricultural Mortgage Trust">Agricultural Mortgage Fund</a></LI>
</UL>
</LI>

<!-- CLOSED INVESTMENT -->
<LI>
<DIV class="closedinvestment_line">
<a href="#" title="Closed Investments" class="closedinvestment">CLOSED INVESTMENTS</a>
</DIV>

<UL>
<LI><a href="../closedinvestments/kelmscott.html" title="Kelmscott Central Development Fund">Kelmscott Central Development Fund</a></LI>
</UL>
</LI>

<!-- INVESTOR SERVICES -->
<LI>
<DIV class="investors_line">
<a href="#" title="Investor Services" class="investors">INVESTOR SERVICES</a>
</DIV>

<UL>
<LI><a href="../investorservices/newsletters.html" title="Newsletters">Newsletters</a></LI>
<LI><a href="../investorservices/accounts.html" title="Financial Accounts">Financial Accounts</a></LI>
<LI><a href="../investorservices/forms.html" title="Forms">Forms</a></LI>
</UL>
</LI>

<!-- ADVISORS SERVICES -->
<LI>
<DIV class="advisors_line">
<a href="#" title="Advisor Services" class="investors">ADVISOR SERVICES</a>
</DIV>

<UL>
<LI><a href="../advisorservices/services.html" title="Advisor Services Team">Advisor Services Team</a></LI>
</UL>
</LI>
</UL>

<!-- NEWS -->
<UL class="noindent">
<LI>
<DIV class="news_line">
<a href="#" title="News" class="news">NEWS</a>
</DIV>

<UL>
<LI><a href="../news/2007.html" title="2007">2007</a></LI>
</UL>
</LI>

<!-- CONTACT US -->
<LI>
<DIV class="contact_line">
<a href="contact.html" title="Contact Us" class="contact">CONTACT US</a>
</DIV>
</LI>
</UL>

</DIV>
</DIV>


<!-- CONTENT AREA -->
<DIV id="center1">
<DIV id="content_center">

<?php
//Headers
$emailFromName = $_POST['name'];
$emailAddress = $_POST['address'];
$emailCity = $_POST['city'];
$emailState = $_POST['state'];
$emailPostcode = $_POST['postcode'];
$emailTelephone = $_POST['phone'];
$emailFrom = $_POST['email'];
$emailAdviser = $_POST['advisergroup'];
$emailContact = $_POST['contact'];
$emailAre = $_POST['are'];
$emailPds = $_POST['pds'];
$emailComment = $_POST['comment'];
$action = (isset($_POST['submit']) ? "submit" : "entry");

//Checkbox for Product Disclosure Statement
foreach($_POST['pds'] as $value) {
$check_msg .= "<br />- $value\n";
}

//Errors
$error_lines = array();
if ($action == "submit")
{
if (empty($emailFromName)) {
$error_lines['emailFromName'] = 'You forgot to enter your name.';  	
}
   
if (empty($emailAddress)) {
$error_lines['emailAddress'] = 'You forgot to enter your address.';
}

if (empty($emailCity)) {
$error_lines['emailCity'] = 'You forgot to enter your city.';
}

if (empty($emailState)) {
$error_lines['emailState'] = 'You forgot to enter your state.';
}

if (empty($emailPostcode)) {
$error_lines['emailPostcode'] = 'You forgot to enter your city.';
}

if (empty($emailTelephone)) {
$error_lines['emailTelephone'] = 'You forgot to enter your telephone.';
}
   
if (!preg_match('/^([A-Z0-9\.\-_]+)@([A-Z0-9\.\-_]+)?([\.]{1})([A-Z]{2,6})$/i', $emailFrom) || empty($emailFrom)) {
$error_lines['emailFrom'] = 'You forgot to enter a valid email address.';
}
}

if($action == "submit" && count($error_lines) == 0) {
 
//Send an email  					
$to = "paul.crawford@austasiagroup.com";
$Subject = "AREIT Enquiry Form";
$body = "The following enquiry was submitted via the AREIL website on ".date('l, d M Y H:i:s') . "\n\nName: ". $emailFromName ."\n\nAddress: ". $emailAddress ."\nCity: ". $emailCity ."\nState: ". $emailState ."\nPostcode: ". $emailPostcode ."\n\nTelephone: ". $emailTelephone ."\n\nEmail: ". $emailFrom ."\n\nAdviser Group: ". $emailAdviser ."\n\nPreferred method of contact: ". $emailContact ."\n\nAre you a: ". $emailAre ."\n\nPlease rush me Product Disclosure Statement for: ". $check_msg ."\n\nComments: ". $emailComment;
mail($to, "$Subject" ,"$body", "From: $emailFrom");
   
echo "

Thank you for enquiry, ". $emailFromName .". Your mail was sent successfully.
<p></p>
Your message is

<div class=\"quote\"><strong>Name</strong>
$emailFromName
<br />

<strong>Address</strong>
$emailAddress
<br />

<strong>City</strong>
$emailCity
<br />

<strong>State</strong>
$emailState
<br />

<strong>Postcode</strong>
$emailPostcode
<br />

<strong>Telephone</strong>
$emailTelephone
<br />

<strong>Email</strong>
$emailFrom
<br />
<br />

<strong>Adviser Group</strong>
$emailAdviser
<p></p>

<strong>Preferred method of contact</strong>
$emailContact
<p></p>

<strong>Are you a</strong>
$emailAre
<p></p>

<strong>Please rush me Product Disclosure Statement for</strong>
$check_msg
<p></p>

<strong>Comments</strong>
$emailComment</div>";

} else {
if (count($error_lines) > 0) {
echo "<div class=\"error\"><h1>Error!</h1>You have the following error(s):<br />\n\n";
foreach($error_lines as $line) {
echo "- $line<br />\n";
}
echo "<form method=\"post\" action=\"mail.php\">
</p>
Please complete the form and re-send it.</div>\n\n";
}
}
?>

</DIV>
</DIV>

<!-- FOOTER -->
<DIV id="footer3">
	<DIV id="footer_border"></DIV>
</DIV>

</DIV>
</DIV></DIV>
</DIV>
</DIV></DIV>

<!-- footer -->
<DIV id="footer">
	<DIV id="content_footer">
	   <a href="../aboutus/overview.html" id="nav_overview">About AREIT</a></LI>
		<LI><a href="../links/links.html" id="nav_links">Links</a></LI>
		<LI><a href="../faqs/faqs.html" id="nav_faqs">FAQ's</a></LI>
		<LI><a href="../privacy/privacy.html" id="nav_privacy">Privacy Policy</a></LI>
		<LI><a href="../disclaimer/disclaimer.html" id="nav_disclaimer">Disclaimer</a></LI>
		<LI><a href="../../sitemap/sitemap.html" id="nav_sitemap">Sitemap</a></LI>
		<LI><a href="contact.html" id="nav_contact">Contact Us</a></LI>
	</DIV>
</DIV>

</DIV></DIV>
</DIV></DIV>

<script>
setActiveLink();
</script>

</BODY>
</HTML>
Any help would be great! :)
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Pointing out the exact code, or at the very least LINE 138, rather than posting a huge script would have been helpful. For next time. :)

I'm assuming line 138 is this bit:

Code: Select all

//Checkbox for Product Disclosure Statement
foreach($_POST['pds'] as $value) {
    $check_msg .= "<br />- $value\n";
}
Is $_POST['pds'] an array? What does your form code look like?


BTW, this is wrong:

Code: Select all

$action = (isset($_POST['submit']) ? "submit" : "entry");
You need to close the parenthesis after the isset() function call.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Is $_POST['pds'] an array?
webdev
Forum Commoner
Posts: 25
Joined: Tue Jun 05, 2007 2:07 am

Post by webdev »

Yes, astions.
webdev
Forum Commoner
Posts: 25
Joined: Tue Jun 05, 2007 2:07 am

Post by webdev »

Ok sorry,bdlang. I'm a new user in this forums.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Is $error_lines an array? One of them isn't. I don't know what line is 138.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

webdev wrote:Ok sorry,bdlang. I'm a new user in this forums.
No need to apologize, but please provide the information we need to help you solve the problem.

So we still need to know which array structure may be causing you this error. Which line is 138?

Any call to foreach() in your script, look at the array going into that function call. If it's $_POST['pds'], make sure it's an array. Same thing with $error_lines. Use is_array() to test the variables and var_dump() or print_r() to display the values within.
webdev
Forum Commoner
Posts: 25
Joined: Tue Jun 05, 2007 2:07 am

Post by webdev »

I looked at Line 138 and it's showing //Headers.

Ok, have a look at my website - http://www.australianreit.com/contact/contact1.html.

Firstly, when the user select the checkbox and the form going to send through my email, that's no problem.
Secondly, when the user forgot to select the checkbox on the form, its something goes wrong and showing "Invalid argument supplied for foreach()..."

I think the reason is the checkbox. I don't know what is the code for checkboxes?

What is the problem?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

An checked checkbox results in a name=value pair when its form is submitted, in your case e.g. pds%5B%5D=Agricultural+Mortgage+Trust.
An unchecked checkbox results in ...nothing, nothing at all.
Therefore if an user does not check any checkbox $_POST['psd'] won't be an array, in fact there will be no $_POST['psd'] at all.

Code: Select all

if ( isset($_POST['pds']) && is_array($_POST['pds']) ) {
  foreach($_POST['pds'] as $value) {
    $check_msg .= "<br />- $value\n";
  }
}
webdev
Forum Commoner
Posts: 25
Joined: Tue Jun 05, 2007 2:07 am

Post by webdev »

Perfect...all done!!

Thank you very much, volka. You done a great job!

Cheers, Paul.
Post Reply