2015-04-17 197 views
-1

我的HTML:顯示錯誤消息

<form name="loginForm" method="post" action="login.php" onsubmit="return(validate());" > 
    <p><label><b>Username</b></label> 
     <input name = "Username" type = "text" maxlength = "30"> 
    </p> 

    <p><label><b>Password</b></label> 
     <input name = "Password" type = "text" maxlength = "30"> 
    </p> 
    <p> 
     <input type = "submit" value = "Login"> 
     <input type = "reset" value = "Clear"> 
     <span class="errorMsg" id="validation"></span> 
    </p> 
</form> 

我的PHP代碼:

echo " <script type= \'text/javascript\'> 
      document.getElementById('validation').innerHTML = 'Username or Password is Incorrect'; 
     </script>"; 

我想實現的是顯示附近的提交按鈕,錯誤消息。

html和php代碼位於2個不同的文件中。 index.html和login.php都在同一個文件夾中。

+0

包括在HTML你的PHP文件將解決這個問題:http://php.net/manual/en/function.include.php – Michelangelo

回答

0
<?php 
$msg=""; 
// put user name and password validation here 
// if login fails set $msg value 
// set $msg value 
?> 
<form name="loginForm" method="post" action="" onsubmit="return validate();" > 
      <p><label><b>Username</b></label> 
       <input name = "Username" type = "text" maxlength = "30"> 
      </p> 

       <p><label><b>Password</b></label> 
        <input name = "Password" type = "text" maxlength = "30"> 
       </p> 
       <p> 
       <input type = "submit" value = "Login"> 
       <input type = "reset" value = "Clear"> 
       <?php if($msg!="") { ?> 
       <span class="errorMsg" id="validation">Username or Password is Incorrect</span> 
       <?php } ?> 
      </p> 
     </form> 
0

首先,你需要改變你的索引文件的擴展名:從index.htmlindex.php到能夠解析PHP代碼。

然後在你的索引文件的地方文件包含的頂部:

<?php include('login.php');?> 

BTW,你不需要用JavaScript來做到這一點,你可以如直接在你的「驗證」跨度迴應此消息像這樣:

<span class="errorMsg" id="validation"><?php echo $message;?></span> 

,然後在login.php中,你包括文件的頂部,你這個文本分配給$消息變量:

$message = 'Username or Password is Incorrect';

0

你可以解決它無javascript還有:

登錄表格:

<form name="loginForm" method="post" action="login.php" onsubmit="return(validate());" > 
     <p><label><b>Username</b></label> 
      <input name = "Username" type = "text" maxlength = "30"> 
     </p> 

      <p><label><b>Password</b></label> 
       <input name = "Password" type = "text" maxlength = "30"> 
      </p> 
      <p> 
      <input type = "submit" value = "Login"> 
      <input type = "reset" value = "Clear"> 
      <?php if (isset($_GET['errorcode']) && $_GET['errorcode'] == "loginFailed") : ?> 
       <span class="errorMsg" id="validation">Username or Password is Incorrect</span> 
      <?php endif; ?> 
     </p> 
    </form> 

而且login.php中:

if(!validateUserPassword()){ 
     header("Location: index.php?errorcode=loginFailed"); 
    } 

    //TODO: if valide show loginpage. 

    function validateUserPassword(){ 
     if(isset($_POST['Username']) && isset($_POST['Password'])){ 
      //TODO: validate username & password and return true if valide 
      return false; 
     } 
     return false; 
    } 
0

如果你將需要JavaScript和沒有動作回退,只需要validate()函數處理響應本身。例如,只需評估PHP的響應。

<script type = "text/javascript"> 
 
    function validate() { 
 
    // perform ajax request via your preference 
 
    //...get response 
 
    document.getElementById('validation').innerHTML = 'message based on response...'; 
 
    return false 
 
    } </script>
<form ... onsubmit="validate();">