2017-04-04 74 views
0

我想執行一個Ajax POST請求,結果從PHP返回echo。我剛剛注意到結果是返回兩個額外的未知字符。Php回聲返回兩個額外的未知字符

這是我的JavaScript:

$.ajax({ 
      type: "POST", 
      url: "php/login.php", 
      data: loginDataString, 
      cache: false, 
      success: function(result){ 
       alert(result); 
       swal("Incorrect", result, "error"); 
      } 
     }); 

而我的整個PHP

<?php 
$con = mysqli_connect("localhost","root","","EugeneStore"); 
if(!$con){ 
    die("Connection error: " . mysqli_error()); 
} 
if($_SERVER['REQUEST_METHOD']=='POST'){ 
     $UN = $_POST['login_username']; 
     $PW = $_POST['login_password']; 
     date_default_timezone_set('Asia/Manila'); 
     $t=time(); $timeDAY = date('d',$t); $timeMONTH = date('m',$t); $timeYEAR = date('Y',$t); $timeYEAR2 = date('y',$t); $CURRENTDATE = "$timeDAY/$timeMONTH/$timeYEAR"; $a2 = date('H',$t); $a3 = date('i',$t); $ampm = ""; 
        if ($a2 >= 0 && $a2 <= 11){ 
         $ampm = "AM"; 
        } 
        if ($a2 >= 12 && $a2 <= 23){ 
         $ampm = "PM"; 
        } 
        if ($a2 == 13){ 
         $a2 = 1; 
        } 
        if ($a2 == 14){ 
         $a2 = 2; 
        } 
        if ($a2 == 15){ 
         $a2 = 3; 
        } 
        if ($a2 == 16){ 
         $a2 = 4; 
        } 
        if ($a2 == 17){ 
         $a2 = 5; 
        } 
        if ($a2 == 18){ 
         $a2 = 6; 
        } 
        if ($a2 == 19){ 
         $a2 = 7; 
        } 
        if ($a2 == 20){ 
         $a2 = 8; 
        } 
        if ($a2 == 21){ 
         $a2 = 9; 
        } 
        if ($a2 == 22){ 
         $a2 = 10; 
        } 
        if ($a2 == 23){ 
         $a2 = 11; 
        } 

     $CURRENTTIME = "$a2:$a3 $ampm"; 

     $sql = "SELECT * FROM Users WHERE Username='".$UN."' AND Password='".$PW."'"; 
     $result = mysqli_query($con,$sql); 
     $count = mysqli_num_rows($result); 
     if($count==1) { 
      $rows00 = mysqli_fetch_array($result); 
      if($rows00['UserType'] == "Admin") { 
       $ADDSYSREC = mysqli_query($con, "INSERT INTO SystemLogs(Username, Date, Time, Information, Type) 
       VALUES('".$rows00['Username']."', '".$CURRENTDATE."', '".$CURRENTTIME."', '".$rows00['Username']." Logged into the system', 'Admin')"); 
       echo "Login Correct Admin"; 

      } 
      if($rows00['UserType'] == "Member") { 
       $ADDSYSREC = mysqli_query($con, "INSERT INTO SystemLogs(Username, Date, Time, Information, Type) 
       VALUES('".$rows00['Username']."', '".$CURRENTDATE."', '".$CURRENTTIME."', '".$rows00['Username']." Logged into the system', 'Member')"); 
       echo "Login Correct Member"; 

      } 
     } else {  
      echo 'Wrong username or password'; 
     } 
} else { 
    echo "Something is wrong with the system. Try again Later"; 
} 
?> 

這裏

enter image description here enter image description here

+0

沒有顯示確切的PHP代碼,很難說。當然,文件末尾沒有任何內容? –

+0

文件末尾必須有東西或者存在編碼問題... – Zeke

+0

從PHP文件的末尾刪除'?>'。 – miken32

回答

2

結果只要你有一個PHP只能對文件,你應該離開closin g ?>標籤。 This question有一些解釋爲什麼,但最重要的原因是確保Web服務器不輸出空白或隱藏的字符。