2014-02-20 27 views
0

我做諮詢時形成字段爲空注意:未定義的變量:名稱在C:的appserv WWW ContactUs.php上線217

的問題是顯示錯誤,我儘量做到當填補一些字段等,空字段和填充字段中的錯誤顯示仍有其數據。

每次我得到這個錯誤時

&:

Notice: Undefined variable: name in C:\AppServ\www\ContactUs.php on line 217

這是我的代碼:

<?php 
      if (!empty($_POST['action']) && $_POST['action'] == "send") { 

       $name = $_POST ['name']; 

       if (!$name || $name == '') { 
        $name_error = 'Please insert name'; 
       } else { 
        $name_error = ''; 

      } 
      ?> 

也是這個

<div title="Send" style="padding: 5px; text-align: left"> 
<form action="" method="post"> 
    <input type="hidden" name="action" value="send" /> 
    <table style="border: 0px;"> 
     <tr> 
      <td style="color: white">Full name:<a class="notemptycolor">*</a></td> 
      <td> 
       <input type="text" value="<?= $name ?>" name="name"/> 
       <?= !empty($name_error) ? '<div style="margin-top: 10px; margin-bottom: 10px; color: red;background-color: white" >' . $name_error . '</div>' : '' ?> 
      </td> 
     </tr> 
    </table> 
    <input type="image" name="submit" src="images/sendBtn.gif" style="float: right; margin-top:9px; margin-right:20px;"> 
</form> 

回答

0

嘗試:

  <?php 
      $name = ""; 
      if (!empty($_POST['action']) && $_POST['action'] == "send") { 

       $name = $_POST ['name']; 

       if (!$name || $name == '') { 
        $name_error = 'Please insert name'; 
       } else { 
        $name_error = ''; 

      } 
      ?> 
+0

這是解決問題....謝謝.....但我不被這將通過name的值時,將其發送到郵件或它將是空的?! – user3127434

0

您在表單中使用$ name,但如果表單未提交,則未定義。

我的建議是:

<?php 
$name = ''; 
$name_error = ''; 

if (isset($_POST['action']) && $_POST['action'] == "send") { 

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

    if (empty($name)) 
     $name_error = 'Please insert name'; 

} ?> 
相關問題