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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>
<?
}
?>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.