2011-01-25 121 views
0

我正在讀一本書,並通過示例,不幸的是,如果我在提交表單時將字段留空,我將不斷收到錯誤。我查了勘誤,沒有任何東西,我嘗試在論壇上發帖,但沒有得到任何答覆。菜鳥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 = '[email protected]'; //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

哪一行是第60行? – BoltClock 2011-01-25 19:09:35

+1

`$ required`和`$ expected`從哪裏來? – Tomalak 2011-01-25 19:11:01

+0

確切的錯誤信息也會有幫助。 – 2011-01-25 19:11:48

回答

1

很簡單,您的請求中的電子郵件地址爲空,所以變量$ email永遠不會被創建,因爲您只在$ _REQUEST [$ key]不爲空時才執行賦值操作。

此外,我強烈建議不要根據不可信的$ _REQUEST創建變量,因爲這可能會造成嚴重的安全漏洞。一個簡單的例子:打電話給你的表單url並附加類似的東西?subject =該死的

2

在下面的一行:

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

您使用的是變量$電子郵件,但還沒有實際創建該變量的任何地方。

嘗試一個上文剛加入這一行:

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

這是一個if..else語句很短的手形。它檢查$ _POST數組中是否存在電子郵件並使用該值,否則它只是將其設置爲空字符串。無論哪種方式,變量現在「定義」。

1

某些瀏覽器不提交沒有value屬性且value屬性爲空的html字段,因此您必須先檢查post變量是否存在。

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

我會建議確保所有html輸入字段在html代碼中至少有value =「」屬性,問題解決了。