[英]change login page from jquery to php-mysql
I have made some page for show all data from DB. 我已经做了一些页面来显示数据库中的所有数据。 And also, I can edit the data from this page.
而且,我可以在此页面上编辑数据。 Berofe do some editing, some JQuery Dialog will appear and then ask our user or password.
Berofe做一些编辑,将会出现一些“ JQuery对话框”,然后询问我们的用户或密码。
I have made some simple login use Jquery, like: 我已经使用Jquery进行了一些简单的登录,例如:
$("#enter").click(function(){
if($('#user').val() == "admin" || $('#pass').val() == "qaubuntu") {
$("#dialog").dialog('close');
$("#segment").show();
}
if($('#user').val() == "user" || $('#pass').val() == "user") {
$("#dialog").dialog('close');
$("#content").show();
}
});
But, this only for one user. 但是,这仅适用于一个用户。 I need to add 5 user again for different page access.
我需要为不同的页面访问再次添加5个用户。 how should I do to change this login into php and mysql?
我该怎么做才能将此登录更改为php和mysql? I'm not really understand how to login for different access page for each user.
我不太了解如何为每个用户登录不同的访问页面。
thanks for advance. 感谢前进。
ex:
user = admin ---> just can access #segment
user = user and foo --> can access #content
You culd use jQuery, Php, and mysql. 您可以使用jQuery,Php和mysql。
NOTE: Not tested but it will get you started 注意:未经测试,但可以帮助您入门
Well first off i wuld start by building a usertable in your DB, Lets say you have the following fields: 首先,我将从在您的数据库中建立用户表开始,假设您具有以下字段:
"Username" "password" "privilage" <-( set to INT where 1="admin" 2="user")
“用户名”“密码”“特权” <-(设置为INT,其中1 =“ admin” 2 =“ user”)
Then you shuld create the login form (lets say tou have the inputs id="username" and id="password") and a button "login" with id="login". 然后,您应该创建登录表单(让您说输入具有id =“ username”和id =“ password”的输入)和带有id =“ login”的按钮“ login”。
Now some jQuery: 现在一些jQuery:
$(document).ready(function(){
$('#login').click(function(){ // Grab button "login" on click event
var usr = $("#username").val(); // Grab field value from input "username"
var pas = $("#password").val(); // Grab field value from input "password"
var dataString = "usr="+usr+"&pas="+pas;
//Now send your data to a backend file (Php) using $.ajax
$.ajax({
type:'POST',
data: dataString,
url:'backendfile.php', /// loaction and backend filename
success: function(data){ // Callback
// DO STUFF LATER SEE jquery Callback below
}
});
});
});
And for backend.php 对于backend.php
//Create your DB connection first
// grab POST
$usr = $_POST['usr'];
$pas = $_POST['pas'];
//Sql call
$sql = "SELECT privilage FROM tabel_name WHERE username='".$usr."' AND password='".$pas."'";
if(myslq_query($sql)){
$priv = mysql_fetch_assoc($sql);
/* will return 1 or 2 depending on the privilage set in you DB where 1 is admin and 2 is user */
echo $priv['privilage'];
}else{
echo 0; // If login fails
}
Now go back to jQuery: (handle Callback) 现在回到jQuery :(处理回调)
In the success section of your previous ajax call. 在上一个ajax调用的成功部分中。
success: function(data){ // Callback
switch(data){
case 1:
// Do stuff if privilage is 1 (admin)
$("#dialog").dialog('close');
$("#segment").show();
break;
case 2:
// do stuff if result is 2 (user)
$("#dialog").dialog('close');
$("#content").show();
break;
default:
alert('wrong username or password');
break;
}
}
There you go! 你去! Its not tested, so you may confront some problems but, hey it il get you started.
它未经测试,因此您可能会遇到一些问题,但是,嘿,这会让您入门。
Tip! 小费! encrypt the password
加密密码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.