我有一个index.html页面,用户输入他们的用户名和密码。举个例子,我有3个数据库表: 1. CLient 2.现场工作人员 3.公司。
我想要一个index.html页面,用户输入用户名和密码。他/她可以来自任何一个数据库表。现在检查用户是否来自任何一个表,我想将它们重定向到单独的页面。例如,如果用户是客户端,我想重定向到client.html。
以下是代码:
HTML
<script>
function PostData() {
// 1. Create XHR instance - Start
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
throw new Error("Ajax is not supported by this browser");
}
// 1. Create XHR instance - End
// 2. Define what to do when XHR feed you the response from the server - Start
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status == 200 && xhr.status < 300) {
document.getElementById('div1').innerHTML = xhr.responseText;
}
}
}
// 2. Define what to do when XHR feed you the response from the server - Start
var userid = document.getElementById("userid").value;
var pid = document.getElementById("pid").value;
// 3. Specify your action, location and Send to the server - Start
xhr.open('POST', 'login.php');
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("userid=" + userid + "&pid=" + pid);
//xhr.send("&pid=" + pid);
// 3. Specify your action, location and Send to the server - End
}
</script>
</head>
<body>
<form>
<label for="userid">User ID :</label><br/>
<input type="text" name ="userid" id="userid" /><br/>
<label for="pid">Password :</label><br/>
<input type="password" name="password" id="pid" /><br><br/>
<div id="div1">
<input type="button" value ="Login" onClick="PostData()" />
</div>
</form>
PHP
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
//session_start();
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($_POST['userid'],$_POST['pid']))
{
$userid = trim($_POST["userid"]);
$pid = trim($_POST["pid"]);
$sql = "SELECT * FROM tablename WHERE uname = '$userid' and pword = '$pid'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result);
echo "公司".'<br/>';
echo $row['client'].'<br/>'.'<br/>';
echo "第".'<br/>';
echo '<a href="preview.html"/>'.$row['day1'].'</a>'.'<br/>';
?>
答案 0 :(得分:0)
您可以使用SELECT COUNT(*) FROM tablename WHERE username = userinput
检查用户是否存在。然后,您可以使用header()
重定向。
答案 1 :(得分:-1)
您可能希望在数据库表上添加“字段”行以确定用户的字段 e.g。
userID |名字|年龄|字段|
a232 | meimei | 22 |客户|
然后选择'MeiMei'的用户字段,即'Client' 然后创建一个switch语句
switch ($userField) {
case 'Client':
// code here
break;
case 'Field Worker':
// code here
break;
case 'Company':
// code here
break;
default:
// code to be executed if $user is different from all labels;
}