-1

当我测试我的 PHP 联系表单时,我不断收到“警告:在第 102 行为 foreach() 提供的参数无效”消息。我最近重建了我的网站,但我使用的 PHP 联系表单代码与我在旧网站上使用的完全相同,并且旧网站的表单仍然可以正常工作。我已经逐字复制并粘贴了它,所以我不确定为什么会出现此错误。任何帮助将不胜感激。

这是我得到假定错误的行(第 102 行):

foreach($errors as $value) {

这是整个表单脚本(错误行在底部):

<?php 

// 
if(isset($_POST["submit"]))
{

    //This is checking if the string length in the firstname field was greater than 1 character
    if(strlen($_POST["firstname"]) > 1)
    {

        $firstname = $_POST["firstname"];
        //echo $firstname;
    }

    else
    {
        //echo "You did not type in first name";
        $errors["firstname"] = "<span class=\"error\">You did not type in a first name.</span>";
    }


    if(strlen($_POST["lastname"]) > 1)
    {

        $lastname = $_POST["lastname"];
    }

    else
    {
        $errors["lastname"] = "<span class=\"error\">You did not type in a last name.</span>";
    }


    if(strlen($_POST["email"]) > 1)
    {

        $email = $_POST["email"];

        if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $email = $email;
        }

        else {
            $errors["email"] = "<span class=\"error\">Email is invalid.</span>";
        }
    }

    else
    {
        $errors["email"] = "<span class=\"error\">You did not type in an email address.</span>";
    }

    //This is checking if the string length in the message field was greater than 1 character
    if(strlen($_POST["message"]) > 1)
    {

        $message = $_POST["message"];
        //echo $firstname;
    }

    else
    {

        $errors["message"] = "<span class=\"error\">You did not type in a message.</span>";
    }


    // Code to tell form NOT to send form if there are any errors.
    if($errors < 1)
    {
            $to = "example@email.com";
            $from = $email;
            // headers makes sure you have a reply-to address
            $headers = "From: {$from}" . "\r\n";
            $headers .= 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1';

            $subject =  
                "From: ($from)" . "<br />" .
                "First name: ($firstname)" . "<br />" .
                "Last name: ($lastname)" . "<br />" .
                "Email: ($email)" . "<br />" .
                "Message: ($message)";

            if(mail($to, $from, $subject, $headers))
            {
                $finalMsg = "<p class=\"success\">Thank you! Your email was sent.</p>";
            }

            else {
                $finalMsg = "<p class=\"error\">Your email was NOT sent. Please try again.</p>";
            }

}

?>

<?php

//each error is displayed

foreach($errors as $value) {

echo "<span>$value</span><br />";

}
    }
?>
4

4 回答 4

2
<?php 

$errors = array();
if(isset($_POST["submit"]))
{
    ...

您必须定义所有变量。 你这样做是为了$firstname?所以你也应该拥有它$errors

另外,在定义错误时不要添加格式,在输出时添加。它将为您节省大量打字。

于 2013-04-28T07:31:10.577 回答
0

您正在对不是数组的东西进行 foreach 。foreach您应该使用is_array函数检查您传递给的是一个数组

如果您不确定它是否是一个数组,您可以随时使用以下 PHP 示例代码进行检查:

if (is_array($errors)) {

  foreach ($errors as $error) {
   //do something
  }
}

笔记

foreach 构造提供了一种迭代数组的简单方法。foreach 仅适用于数组和对象,当您尝试在具有不同数据类型的变量或未初始化的变量上使用它时会发出错误。

于 2013-04-28T07:26:34.060 回答
0

尝试if(is_array($errors))在你的 foreach 循环周围添加。

看看 PHP 的 error_reporting 级别: http: //php.net/manual/en/function.error-reporting.php

很可能您的旧 Web 服务器配置了较低的错误报告级别。

于 2013-04-28T07:26:45.703 回答
0

您的变量$errors未事先设置,这会导致尝试迭代的错误。正如其他人和我同时建议的那样,$errors事先声明应该可以解决这个特定问题。

$errors = array();在你的第一个之前if-statement那样的东西可以解决问题。否则也类似于其他人的建议,做issetor is_array(稍微好一点,因为它检查它是否也是一个数组)会有所帮助。

PHP foreach 文档

于 2013-04-28T07:26:56.767 回答