新手PHP,收到一个未定义的变量错误

时间:2011-01-25 19:08:04

标签: php

我正在读一本书并浏览这些例子,不幸的是,如果我在提交表格时将字段留空,我会继续收到错误。我检查了勘误表,没有任何内容,我试图在书籍论坛上发帖,但我没有得到任何回复。

我相信也许我必须首先声明变量,但这会从自动生成的变量中消失。

任何帮助都会非常感激,我是一个尝试学习php的菜鸟。

根据请求,这是$ required和$ expected。谢谢!

$expected = array('name', 'email', 'comments');
$required = array('name', 'email', 'comments');

<?php
foreach ($_POST as $key => $value){
    //assign to temporary variable and strip whitespace if not an array
    $temp = is_array($value) ? $value : trim($value);

    //if empty and required, add to $missing array
    if (empty($temp) && in_array($key, $required)){
        $missing[] = $key;
    } elseif(in_array($key, $expected)){
        //otherwise, assign to a variable of the same name as $key
        ${$key} = $temp;
    }
}

<?php 
include('./includes/title.inc.php'); 
$errors = array();
$missing = array();

//Check to see if the form has been submitted
if (isset($_POST['send'])){
    //email processing script
    $to = 'drewdin1@hotmail.com'; //use your email address
    $subject = 'Feedback from Japan Journey';

    //list expecting fields
    $expected = array('name', 'email', 'comments');

    //set required fields
    $required = array('name', 'email', 'comments');
    include('./includes/processmail.inc.php'); 
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset=utf-8">
<title>Japan Journey<?php if (isset($title)){echo "&#8212;{$title}";} ?></title>
<link href="styles/journey.css" rel="stylesheet" type="text/css" media="screen">
</head>

<body>
<div id="header">
    <h1>Japan Journey</h1>
</div>
<div id="wrapper">
    <?php include('./includes/menu.inc.php'); ?>
    <div id="maincontent">
        <h2>Contact Us</h2>
        <?php if ($missing || $errors){ ?>
            <p class="warning">Please fix the item(s) indicated.</p>
        <?php } ?>
      <p>Ut enim ad minim veniam, quis nostrud exercitation consectetur adipisicing elit. Velit esse cillum dolore ullamco laboris nisi in reprehenderit in voluptate. Mollit anim id est laborum. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
        <form id="feedback" method="POST" action="">
            <p>
                <label for="name">Name:
                    <?php if ($missing && in_array('name', $missing)){ ?>
                        <span class="warning">Please enter your name</span>
                    <?php } ?>
                </label>
                <input name="name" id="name" type="text" class="formbox"<?php
                    if ($missing || $errors){
                        echo ' value="' . htmlentities($name, ENT_COMPAT, 'UTF-8') . '" ';
                    } ?> />
            </p>
            <p>
                <label for="email">Email:
                    <?php if ($missing && in_array('email', $missing)){ ?>
                        <span class="warning">Please enter your email address</span>
                    <?php } ?>
                </label>
                <input name="email" id="email" type="text" class="formbox"<?php
                    if ($missing || $errors){
                        echo ' value="' . htmlentities($email, ENT_COMPAT, 'UTF-8') . '" ';
                    } ?> />
            </p>
            <p>
                <label for="comments">Comments:
                    <?php if ($missing && in_array('comments', $missing)){ ?>
                        <span class="warning">Please enter your comments</span>
                    <?php } ?>
                </label>
                <textarea name="comments" id="comments" cols="60" rows="8"><?php
                    if ($missing || $errors){
                        echo htmlentities($comments, ENT_COMPAT, 'UTF-8');
                    } ?></textarea>
            </p>
            <p>
                <input name="send" id="send" type="submit" value="Send message">
            </p>
        </form>

        <pre>
            <?php if ($_POST && $missing) {print_r($_POST);} ?>
        </pre>
    </div>
    <?php include('./includes/footer.inc.php'); ?>
</div>
</body>
</html>

enter image description here

3 个答案:

答案 0 :(得分:2)

在以下行中:

echo ' value="' . htmlentities($email, ENT_COMPAT, 'UTF-8') . '" ';

您正在使用变量$ email,但实际上并未在任何地方创建该变量。

尝试在该行上方添加此行:

$email = isset($_POST['email'])?$_POST['email']:'';

这是if..else语句的简写形式。它检查$ _POST数组中是否存在电子邮件并使用该值,否则它只是将其设置为空字符串。无论哪种方式,变量现在都是“已定义”。

答案 1 :(得分:1)

某些浏览器不提交没有value属性的html字段且value属性为空,因此您必须首先检查post变量是否存在。

if ($missing || $errors){
    if (isset($email)) 
        echo ' value="' . htmlentities($email, ENT_COMPAT, 'UTF-8') . '" ';
} 

我建议确保所有html输入字段在html代码中至少具有value =“”属性,问题解决了。

答案 2 :(得分:1)

很简单,电子邮件在您的请求中输入为空,因此如果$ _REQUEST [$ key]不为空,则只会执行变量$ email,因为您只执行分配。

此外,我强烈建议不要根据不受信任的$ _REQUEST创建变量,因为这可能会打开严重的安全漏洞。一个简单的例子:调用你的表单url并添加类似?subject = damn

的内容