2017-04-25 27 views
0

我創建了三個php表單。第一個(form.php)包含表單標籤,第二個(form1.php) 是信息所在的位置。最後一個(formerror.php),我創建時,我找不到 的方式在第一頁(form.php)上顯示一條錯誤消息。我想知道的是,這樣做是否合適? - 創建與第一個完全相同的頁面以顯示錯誤消息。在PHP中顯示錯誤消息的更好方法是什麼?

enter image description here

這是第一頁。

enter image description here

這是最後一頁,我必須準確地在第一頁,以顯示錯誤消息複製。

第一頁有從我進入信息,並在第二頁是form1.php

CODE FROM THE FIRST PAGE(form.php): 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Form</title> 
    <style type="text/css"> 
     input { 
      display: block; 
      margin: 10px; 
      padding: 10px; 
     } 
     form { 
      border: 0px solid rgba(0,0,0,0.5); 
      width: 19%; 
      padding: 20px; 
      background-color: rgba(5,25,9,0.2); 
     } 
    </style> 
</head> 
<body> 
//This is the form from form.php 
    <form action="form1.php" method="post"> 
     <input type="text" name="username" placeholder="Enter username"> 
     <input type="email" name="email" placeholder="Enter email address"> 
     <input type="password" name="password" placeholder="Enter password"> 
     <input type="password" name="cpassword" placeholder="Enter password again"> 
     <input type="submit" name="submit_btn" value="Submit"> 
     <input type="reset"> 
    </form> 

    <p style="color: red;" class="erreur"></p> 


    </body> 
    </html> 

    //This is the second page 

一旦從form.php的,這裏的到來,進入信息顯示得到,因爲我們給他們一種形式已經給出了用戶名和電子郵件地址 代碼的第二頁(form1.php):

<!DOCTYPE html> 
    <html> 
    <head> 
     <title>FORM 1</title> 
    </head> 
    <body> 

    <?php 
     $username = $_POST['username']; 
     $email = $_POST['email']; 
     $submit_btn = $_POST['submit_btn']; 
     $password = $_POST['password']; 
     $cpassword = $_POST['cpassword']; 

     if (isset($_POST['submit_btn'])) { 
      if ((empty($_POST['username'])) || (empty($_POST['email'])) || (empty($_POST['password'])) || (empty($_POST['cpassword']))) { 
       // header("Location:form.php"); 
       header("Location:formerror.php"); 
      }else{ 
       echo "Welcome $username, your email address is $email"; 
      } 
     } 

    ?> 



    </body> 
    </html> 

,這是我爲了顯示錯誤創建頁面。我所做的是,我複製了與form.php完全相同的頁面,但是在底部,我添加了一條消息,如圖所示。

AND CODE FROM THE THIRD PAGE(formerror.php): 

    <!DOCTYPE html> 
    <html> 
    <head> 
     <title>Form</title> 
     <style type="text/css"> 
      input { 
       display: block; 
       margin: 10px; 
       padding: 10px; 
      } 
      form { 
       border: 0px solid rgba(0,0,0,0.5); 
       width: 19%; 
       padding: 20px; 
       background-color: rgba(5,25,9,0.2); 
      } 
     </style> 
    </head> 
    <body> 

    <form action="form1.php" method="post"> 
     <input type="text" name="username" placeholder="Enter username"> 
     <input type="email" name="email" placeholder="Enter email address"> 
     <input type="password" name="password" placeholder="Enter password"> 
     <input type="password" name="cpassword" placeholder="Enter password again"> 
     <input type="submit" name="submit_btn" value="Submit"> 
     <input type="reset"> 
    </form> 

    <p style="color: red;">Please fill the form</p> 


    </body> 
    </html> 

正如你所看到的,頁面在那裏。而且我也想知道我的代碼是否做得很好,雖然他們完美地工作。

回答

0

試試這個。放在你的php文件上面。

$server = "localhost"; 
$username= "root"; 
$pass = ""; 
$dbname =""; 
$conn = new mysqli($server, $username, $pass, $dbname); 

if($conn -> connect_error){ 
    echo "Connection Failed!, " . $conn -> connect_error; 
} 
0

您可以通過僅使用一個文件來解決問題。將您的表單數據發送到同一頁面。 像這樣在你的文件開始之前添加你的PHP腳本。

<?php   
    $error = ''; 
    if (isset($_POST['submit_btn'])) 
    { 
     $server = "localhost"; 
     $username= ""; // your username 
     $pass = ""; // your password 
     $dbname =""; // your database name 
     $conn = new mysqli($server, $username, $pass, $dbname); 
     if($conn -> connect_error){ 
      echo "Connection Failed!, " . $conn -> connect_error; 

     if(checkValidation()) 
      // insert the data into the database and redirect to a new page.      
    } 

    function checkValidation() 
    { 
     $username = $_POST['username']; 
     $email = $_POST['email']; 
     $password = $_POST['password']; 
     $cpassword = $_POST['cpassword']; 
     if(empty($username)) 
     { 
      $error = "Username field can't be empty";  
      return false;      
     } 
     if(empty($email)) 
     { 
      $error = "Email field can't be empty";  
      return false;      
     } 
     if(empty($password)) 
     { 
      $error = "Password can't be empty";       
      return false; 
     } 
     if(empty($cpassword) || ($password != $cpassword)) 
     { 
      $error = "Passwords don't match";       
      return false; 
     } 
      return true; 
    } 
?> 

然後添加您的html代碼。

<html> 
<head> 
    <title>Form</title> 
    <style type="text/css"> 
     input { 
      display: block; 
      margin: 10px; 
      padding: 10px; 
     } 
     form { 
      border: 0px solid rgba(0,0,0,0.5); 
      width: 19%; 
      padding: 20px; 
      background-color: rgba(5,25,9,0.2); 
     } 
    </style> 
</head> 
<body> 
<?php> 
    if(!empty($error)) 
    { 
     echo "<p style='color: red;'></p>"; 
    } 
?> 
<form action="" method="post"> 
    <input type="text" name="username" placeholder="Enter username"> 
    <input type="email" name="email" placeholder="Enter email address"> 
    <input type="password" name="password" placeholder="Enter password"> 
    <input type="password" name="cpassword" placeholder="Enter password again"> 
    <input type="submit" name="submit_btn" value="Submit"> 
    <input type="reset"> 
</form> 
</body> 
</html> 
相關問題