2015-04-15 60 views
0

爲用戶設置一些指導,讓用戶按照輸入進行操作,我相信我的語法和編碼是正確的,但也許我沒有正確引用第二個php文件?我是新來的PHP,所以我的第一個直覺是爲我的文本框使用onfocus或onblur函數,但這是錯誤的,所以任何幫助將非常有用。謝謝。PHP錯誤檢查問題

的index.php

<span class ="error"> * required field </span> 
    <form name ="myForm" action ="display.php<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" 
     method = "POST"><br> 

    Name: <input type="text" name="userName" value ="<?php echo $name;?>" 
       <span class ="error"> *<?php echo $nameErr;?></span><br> 
    University: <input type="text" name="userUniv" value ="<?php echo $university;?>" 
       <span class ="error"> *<?php echo $univErr;?></span><br> 
    Email: <input type ="text" name ="userEmail" value ="<?php echo $email;?>" 
       <span class ="error"> *<?php echo $emailErr;?></span><br><br> 

Display.php的

<?php 
$nameErr = $emailErr = $univErr = ""; 
$name = $email = $university = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["userName"])) { 
     $nameErr = "Name is required"; 
    } else { 
     $name = test_input($_POST["userName"]); 
     if (!preg_match("/^[a-zA-Z ]*$/", $name)) { 
      $nameErr = "Invalid name"; 
     } 
    } 

    if (empty($_POST["userEmail"])) { 
     $emailErr = "Email is required"; 
    } else { 
     $email = test_input($_POST["userEmail"]); 
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
      $emailErr = "Invalid email format"; 
     } 
    } 
    if (empty($_POST["userUniv"])) { 
     $univErr = "University is required"; 
    } else { 
     $university = test_input($_POST["userUniv"]); 
     if (!preg_match("/^[a-zA-Z ]*$/", $university)) { 
      $univErr = "Invalid University"; 
     } 
    } 
} 

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
?> 
+0

這是什麼'action =「display.php <?php echo htmlspecialchars($ _ SERVER ['PHP_SELF']);?>」'?它不應該是'second.php'嗎? – AbraCadaver

+0

對不起,我的實際文件名是display.php – StevieP

+0

你想讓你的錯誤變量從display.php出現在index.php的表單中嗎?在你的display.php中沒有任何跡象表明你在處理這些錯誤消息。如果是這樣的話,你需要index.php裏面的display.php代碼,然後用post或者表單提交。 – eomer

回答

0

當你的表單動作是display.php你從形式重定向遠,因此沒有參照佈局了,也有您可以通過幾種方式來顯示數據。將操作作爲當前頁面並執行所有內部邏輯或將錯誤消息保存到會話中,並將頁面重定向回窗體並通過ajax進行顯示或驗證。

我會證明前者。你也想刪除$_SERVER['PHP_SELF'],這會使你的行動像display.php/display.php假設它在根。

<?php 
    $nameErr = $emailErr = $univErr = ""; 
    $name = $email = $university = ""; 

    if ($_SERVER["REQUEST_METHOD"] == "POST") { 
     if (empty($_POST["userName"])) { 
      $nameErr = "Name is required"; 
     } else { 
      $name = test_input($_POST["userName"]); 
      if (!preg_match("/^[a-zA-Z ]*$/", $name)) { 
       $nameErr = "Invalid name"; 
      } 
     } 

     if (empty($_POST["userEmail"])) { 
      $emailErr = "Email is required"; 
     } else { 
      $email = test_input($_POST["userEmail"]); 
      if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
       $emailErr = "Invalid email format"; 
      } 
     } 
     if (empty($_POST["userUniv"])) { 
      $univErr = "University is required"; 
     } else { 
      $university = test_input($_POST["userUniv"]); 
      if (!preg_match("/^[a-zA-Z ]*$/", $university)) { 
       $univErr = "Invalid University"; 
      } 
     } 
    } 

    function test_input($data) { 
     $data = trim($data); 
     $data = stripslashes($data); 
     $data = htmlspecialchars($data); 
     return $data; 
    } 
?> 


<span class ="error"> * required field </span> 
<form name="myForm" action="" method = "POST"> 
    <br> 
    Name: <input type="text" name="userName" value ="<?php echo $name;?>" 
      <span class ="error"> *<?php echo $nameErr;?></span><br> 
    University: <input type="text" name="userUniv" value ="<?php echo $university;?>" 
      <span class ="error"> *<?php echo $univErr;?></span><br> 
    Email: <input type ="text" name ="userEmail" value ="<?php echo $email;?>" 
      <span class ="error"> *<?php echo $emailErr;?></span><br><br> 
      <input type="submit" value="submit me"> 
</form>