欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

简单示例AJAX结合PHP代码实现登录效果代码_PHP教程

程序员文章站 2022-04-07 09:05:17
...
HTML部分:



function postRequest(strURL){
var xmlHttp;
if(window.XMLHttpRequest){ // For Mozilla, Safari, ...
var xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject){ // For Internet Explorer
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open('POST', strURL, true);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4){
updatepage(xmlHttp.responseText);
}
}
xmlHttp.send(strURL);
}

function updatepage(str){
if(str=="yes"){
alert("Welcome User");
}else{
alert("Invalid Login! Please try again!");
}
}

function call_login(){
var username = window.document.f1.username.value;
var password = window.document.f1.password.value;
var url = "login.php?username=" + username + "&password=" +password ;
postRequest(url);
}
īpt>





















Login
User
Name:
Password:
ōnClick="call_login()">








PHP脚本部分login.php:


$username=$_GET["username"];
$password=$_GET["password"];
if($username=="admin" && $password=="admin"){
echo "yes";
}else{
echo "No";
}
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/319162.htmlTechArticleHTML部分: html head scrīptlanguage="javascrīpt" functionpostRequest(strURL){ varxmlHttp; if(window.XMLHttpRequest){//ForMozilla,Safari,... varxmlHttp=newXMLHttpRequest(); } e...