2013-01-07 50 views
0

我在這裏有index.php裏面有模態窗口和登錄鏈接。模態窗口是用php在mysql中檢查用戶名和密碼。但是當我點擊提交按鈕 - 不輸入 用戶名或密碼它不顯示PHP錯誤 - '你需要輸入用戶名'或'你需要輸入密碼'...什麼時候刷新index.php,我點擊登錄鏈接它顯示... 如何解決這個問題,首先點擊提交按鈕是爲MySQL的PHP​​錯誤cheking?登錄模態窗口提交按鈕php

  <div id="basic-modal-content"> 
      <div id="winsign"> 

      <form action="" method="post" onSubmit="validate();"> 


      <?php 

     include 'core/init.php'; 


     if (empty($_POST) === false) { 
      $username = $_POST['username']; 
      $password = $_POST['password']; 

     if (empty($username) === true || empty($password) === true) { 
     $errors[]= 'You need to enter a username and password'; 
       } else if (user_exists($username) === false) { 
     $errors[]= 'We cant\'t find username. Have you registered?'; 
     } else if (user_active($username) === false) { 
     $errors[]= 'You haven\'t acticated your account!'; 
      } else { 
     $login = login($username, $password); 
     if ($login === false) { 
      $errors[] = 'That username/password combination is incorrect';  
      } else { 
      $_SESSION['user_id'] = $login; 
      header('Location: sign_in.php'); 
      exit(); 
      } 
     } 
      print_r($errors); 
    } 
     ?> 


      <span id="tpos">Username:</span><br> 
      <input type="text" name="username" class="sbutton" id="modalbox" placeholder="Username"> 
      <br><br><span id="tpos">Password:</span> 
      <br><input type="password" name="password" class="sbutton" id="modalbox" placeholder="Password"> 
      <br><br><input type="checkbox" name="checkbox" class="cbutton"><span id="ttext">Remember me</span> 
      <br><input type="submit" value="Sign In" name="signin" id="bsubmit"> 
      </input> 
      </form> 

這是模態窗口,當我點擊提交按鈕,也不是檢查在第一次點擊PHP錯誤,並重新加載的index.php當我點擊第二次是沒有錯誤,但在第一次點擊是不顯示的錯誤,並重新加載頁索引...

+1

深受你的代碼來看,我想說的問題是第6行 –

回答

0

我認爲你正在尋找你的形式的onsubmit。

嘗試這樣:

<form name="formname" action="index.php" method="post" onSubmit="validate();"> 
</form> 
0

您應該首先處理信息並使用empty()檢查它是否爲空。

因此,像這樣

$error=0; $error_msg =''; 
    $username = $_POST['username']; 
    $password = $_POST['password']; 

    if($_POST['submit']) 
{ 
    if(empty($username)){ $error = 1; $error_msg .= //your error msg} 
    if(empty($password)){ $error = 1; $error_msg .= //your error msg} 
    if($error == 0){//do your login stuff} 
} 

希望這有助於