Jump to content

[SOLVED] foreach statement saying not valid argument....whyyyyy?


simcoweb

Recommended Posts

I'm trying to validate the fields in a simple contact form (about 6 fields) and using this code to display the errors:

 

<div align='center'><font color='red'><strong id='errorTitle'><?= !empty($eg_error) ? 'One or more input fields on the form has not been correctly completed.' : '' ?></strong>

<?
// Loop through all errors
if(!empty($err))
{
?>
<ul>
<?
foreach($err as $value)
{
?>
<li id='errorMess'><? echo $value ?></li>
<?
}
?>
</ul>
<?
}
?>

 

And using this to check the fields:

 

<?
// input error checking
    if ($name=="") {
        $err.= "Please enter your name.<br/>";
    }
    if (!$phone==""){
  $err.= "Please enter your phone number.<br/>";
}
if (!$email) {
        $err.= "Please provide your email address<br>";
    }
    if ($email) {
        if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
            $err.= $email. " is not a valid email address.<br/>";
        }
    }
/*    if (!$secure) {
        $err.= "No security code entered<br/>";
    }
    if (($secure!=$match) && ($secure!="")) {
        $err.= "Security code mismatch. Please re-enter.<br/>";
    } */
    if ($err=="") {

// mail the results to admin
send_ebook_mail();

// run the query
ebook_insert();
}
?>

 

I get this error:

 

Warning: Invalid argument supplied for foreach() in /home/content/x/y/z/bbbaley8076/html/ebook.php on line 110

 

Which is, of course, the foreach statement line. Help?

First, thanks for the posts :)

 

My logical mind tells me that $err is set IF there's an error in one of the fields being validated. In other words:

 

if ($name=="") {

        $err.= "Please enter your name.<br/>";

 

Would that not set a value for $err?

You are using $err as a string. foreach() requires an array. If you simply want echo out $err, you don't need to use foreach(), just do something like this:

 

if(!empty($err)){
echo $err;
}

 

However, if you want to separate each error into a list using foreach(), then change your error checking to something like this:

 

<?php

// Create and empty error array
$err = array();

// input error checking
    if ($name=="") {
        $err[] = "Please enter your name.<br/>";
    }
    if (!$phone==""){
  $err[] = "Please enter your phone number.<br/>";
}
if (!$email) {
        $err[] = "Please provide your email address<br>";
    }
    if ($email) {
        if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
            $err[] = $email. " is not a valid email address.<br/>";
        }
    }
/*    if (!$secure) {
        $err[] = "No security code entered<br/>";
    }
    if (($secure!=$match) && ($secure!="")) {
        $err[] = "Security code mismatch. Please re-enter.<br/>";
    } */
    
    if (empty($err)) {

// mail the results to admin
send_ebook_mail();

// run the query
ebook_insert();
} else {

?>
<div align='center'><font color='red'><strong id='errorTitle'><?= !empty($eg_error) ? 'One or more input fields on the form has not been correctly completed.' : '' ?></strong>
<?
// Loop through all errors
?>
<ul>
<?
foreach($err as $value)
{
?>
<li id='errorMess'><? echo $value ?></li>
<?
}
?>
</ul>
<?
}
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.