我有这样的代码
ajax.js
var xmlHttp = buatObjekXmlHttp();
function buatObjekXmlHttp()
{
var obj = null;
if(window.ActiveXObject)
obj = new ActiveXObject("Microsoft.XMLHTTP");
else
if(window.XMLHttpRequest)
obj = new XMLHttpRequest();
if(obj == null)
document.write("Browser tidak mendukung XMLHttpRequest");
return obj;
}
function ambilData(sumber_data, id_elemen)
{
if(xmlHttp != null)
{
var obj = document.getElementById(id_elemen);
xmlHttp.open("GET", sumber_data);
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
obj.innerHTML = xmlHttp.responseText;
}
}
xmlHttp.send(null);
}
cekpass.php
<!DOCTYPE HTML>
<html>
<head>
<title>
Memeriksa Password
</title>
<script src="ajax.js"></script>
<script>
function prosesData(sumber_data, id_elemen)
{
if(xmlHttp != null)
{
var elemen_div = document.getElementById(id_elemen);
var elemen_user_id = document.getElementById("user_id");
var elemen_password = document.getElementById("password");
var url = sumber_data + "?user_id = " + elemen_user_id.value + "&password = " + elemen_password.value;
xmlHttp.open("GET", url);
xmlHttp.onreadystatechange =
function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
if(xmlHttp.responseText == "OK")
{
elemen_div.innerHTML = "OK";
location.href="infoweb.php";
}
else
elemen_div.innerHTML = "User_id atau/dan password salah";
}
}
xmlHttp.send(null);
}
}
</script>
</head>
<body>
<form>
User ID : <input type="text" name="textuser_id" id="user_id">
<br>
Password : <input type="password" name="textpassword" id="password">
<br>
<input value="Login" type="button" onclick="prosesData('password.php', 'id_info');">
</form>
<div id="id_info"></div>
<p id="demo"></p>
</body>
</html>
这个
password.php
<?php
header("Cache-Control : no-cache, must-revalidates");
header("Expires : Mon, 26 Jul 1997 00:00:00 GMT");
//Data user-id dan password
$user[] = "anton"; $pass[] = "biola";
$user[] = "ahmad"; $pass[] = "gitar";
$user[] = "dewi"; $pass[] = "piano";
$user[] = "santi"; $pass[] = "keyboard";
$user[] = "salman"; $pass[] = "perkusi";
//Peroleh variable URL
$user_id = $_GET["user_id"];
$password = $_GET["password"];
$ok = FALSE;
for($i=0; $i < count($user); $i++)
{
if(($user_id == $user[$i]) && ($password == $pass[$i]))
{
$ok = TRUE;
break;
}
}
session_start(); //Buat sesi
$_SESSION["ok"] = $ok;
if($ok)
{
$_SESSION["user_id"] = $user_id;
$_SESSION["password"] = $password;
print("OK");
}
else
print("NOT OK");
?>
我想使用带有javascript的ajax进行简单登录,不是使用数据库而是使用数组,如果正确,它将链接到另一个页面,但问题是,当我输入正确答案时,它总是说我错了: (有人可以告诉我我的代码出了什么问题:(我还是新的:(
答案 0 :(得分:1)
您的网址格式错误。通过更改
删除不必要的空格 var url = sumber_data + "?user_id = " + elemen_user_id.value + "&password = " + elemen_password.value;
到
var url = sumber_data + "?user_id=" + elemen_user_id.value + "&password=" + elemen_password.value;
答案 1 :(得分:1)
我没有找到匹配数组索引问题的可能解决方案,而是向您展示一个可行的解决方案。
<?php
header("Cache-Control : no-cache, must-revalidates");
header("Expires : Mon, 26 Jul 1997 00:00:00 GMT");
//Data user-id dan password
$user["anton"] = "biola";
$user["ahmad"] = "gitar";
$user["dewi"] = "piano";
$user["santi"] = "keyboard";
$user["salman"] = "perkusi";
//Peroleh variable URL
$user_id = $_GET["user_id"];
$password = $_GET["password"];
$ok = FALSE;
if($user[$user_id] === $password){
$ok = TRUE;
}
session_start(); //Buat sesi
$_SESSION["ok"] = $ok;
if($ok)
{
$_SESSION["user_id"] = $user_id;
$_SESSION["password"] = $password;
print("OK");
}
else
print("NOT OK");
?>
这不比现有方法更不安全,而且有效。