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);
}
?>
建議我的解決方案。
那麼$ .ajax部分呢? –
不,爲什麼?將結果從$ .post返回到dtbs.php。 – AndrewR
所以你的意思是說我使用$ .post而不是$ .ajax? –