2015-04-17 165 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();">