2015-08-18 82 views
1

如何使用$ .post請求將用戶名和密碼傳遞給服務器url?將用戶名/密碼傳遞給服務器url

我想從該請求返回true或false。

$.post("dtbs.php",{username:userName,password:password},function(result){ 
}); 

上面的代碼表示,但我想真或假的值傳遞給從該請求的文本文件。 我很困惑該怎麼做?下面

是我的腳本:

<script> 
        $(document).ready(function(){ 
         $("#loginBtn").click(function(){ 
         var userName = $("#inputlUsername3").val(); 
         var password = $("#inputlPassword3").val(); 
         if(userName && password) { 
          //your php request goes here and return true or false or any ohter response as per your need 
          $.post("dtbs.php",{username:userName,password:password},function(result){ 

}); 
          $.ajax({url: "response.txt", success: function(result){ 
           if(result === 'true') { 
           alert("Redirect it to dashboard"); 
           } else { 
           alert("Show error"); 
           } 
          }}); 
         } else { 
          alert("Plz fill all the field"); 
         } 
         }); 
        }); 
      </script> 

以下代表dtbs.php

<?php 
$uname=$_POST['txtuname']; 
$pwd2=$_POST['txtpwd2']; 
$con=mysql_connect("localhost","root","") or die(mysql_error()); 
mysql_select_db("onlineshop",$con); 
$r=mysql_query("select password from users where username='$uname'",$con); 
$row = mysql_fetch_row($r); 
    $val=$row[0]; 

if($val!="") 
{ 
if($pwd2==$val) 
{ 
    session_start(); 
    $_SESSION['username']=$uname; 
    $myfile= fopen("response.txt", "w") or die("unable to open file"); 
    $txt="true"; 
    fwrite($myfile, $txt); 
    fclose($myfile); 
} 
else { 

     $myfile= fopen("response.txt", "w") or die("unable to open file"); 
    $txt="false"; 
    fwrite($myfile, $txt); 
    fclose($myfile); 


} 
} 
else { 

$myfile= fopen("response.txt", "w") or die("unable to open file"); 
    $txt="false"; 
    fwrite($myfile, $txt); 
    fclose($myfile); 
} 
?> 

建議我的解決方案。

回答

1

dtbs.php應該返回結果,如果成功的用戶名/密碼,而不是寫入文本文件。

<script> 
    $(document).ready(function(){ 
     $("#loginBtn").click(function(){ 
      var userName = $("#inputlUsername3").val(); 
      var password = $("#inputlPassword3").val(); 
      if(userName && password) { 
       $.post("dtbs.php", username:userName, password:password}, function(result){ 
        if(result === 'true') { 
         alert("Redirect it to dashboard");        
        } 
        else { 
         alert("Show error"); 
        } 
       }); 

      } else { 
       alert("Plz fill all the field"); 
      } 
     }); 
    }); 
</script> 


<?php 
$uname=$_POST['username']; 
$pwd2=$_POST['password']; 
$con=mysql_connect("localhost","root","") or die(mysql_error()); 
mysql_select_db("onlineshop",$con); 
$r=mysql_query("select password from users where username='$uname'",$con); 
$row = mysql_fetch_row($r); 
$val=$row[0]; 

if($val!="") 
{ 
    if($pwd2==$val) 
    { 
     session_start(); 
     $_SESSION['username']=$uname; 
     echo 'true'; 
    } 
    else { 
     echo 'false'; 
    } 
} 
else { 
    echo 'false'; 
} 
?> 

總的來說,有以下問題與您的代碼:

  • 不要使用msql_功能,那些已被棄用。
  • SQL注入問題。
  • 將密碼作爲純文本存儲在數據庫中。
  • 以root用戶身份連接到mySql,無需密碼。
+0

那麼$ .ajax部分呢? –

+0

不,爲什麼?將結果從$ .post返回到dtbs.php。 – AndrewR

+0

所以你的意思是說我使用$ .post而不是$ .ajax? –

相關問題