Skip to content

Instantly share code, notes, and snippets.

@lanxan
Created April 29, 2012 12:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lanxan/2550185 to your computer and use it in GitHub Desktop.
Save lanxan/2550185 to your computer and use it in GitHub Desktop.
login & register with ajax
<html>
<head>
<title>login</title>
<script type="text/javascript" >
function id(id){
return document.getElementById(id);
}
function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
function login(form){
if(form.username.value == ""){
id("note1").textContent="please enter the username";
form.username.focus();
return;
}
else
id("note1").textContent="";
if(form.password.value == ""){
id("note2").textContent="please enter the password";
form.password.focus();
return;
}
else
id("note2").textContent="";
var ajax=new XMLHttpRequest();
ajax.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
var check = this.responseText;
alert(check);
if(check == "true"){
setCookie('username',form.username.value,10);
location.replace("index.html");
}
else if(check == "false"){
id("response").textContent="wrong username or password";
return;
}
else{
id("response").textContent="...error";
return;
}
}
}
ajax.open("POST","login.php");
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send("username="+form.username.value+"&password="+form.password.value);
}
</script>
</head>
<body>
<form>
<fieldset>
<legend>login</legend>
username:<input type="text" name="username" /><span id="note1"></span><br />
password:<input type="password" name="password" /><span id="note2"></span><br />
<input type="button" value="login" onclick="login(this.form)"/>
<p id="response"></p>
</fieldset>
</form>
<a href="register.html">register</a>
<a href="index.html">index</a>
</body>
</html>
<html>
<head>
<title>register</title>
<script type="text/javascript">
function id(id){
return document.getElementById(id);
}
function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
function checkpw(form){
if(form.password.value != form.chkpasswd.value){
id("note3").textContent="password and checkpassword not same";
form.username.focus();
return;
}
else
id("note3").textContent="";
}
function register(form){
if(form.username.value == ""){
id("note1").textContent="please enter the username";
form.username.focus();
return;
}
else
id("note1").textContent="";
if(form.password.value == ""){
id("note2").textContent="please enter the password";
form.password.focus();
return;
}
else
id("note1").textContent="";
var ajax=new XMLHttpRequest();
ajax.onreadystatechange = function(){
if(this.readyState == 4 && this.status ==200){
var check = this.responseText;
if(check == "true"){
setCookie('username',form.username.value,10);
alert("success");
location.replace("index.html");
}
else if(check == "same"){
id("check").textContent="this id has been used";
return;
}
else if(check == "false"){
id("check").textContent="wrong username or password";
return;
}
else{
id("check").textContent=check;
return;
}
}
}
ajax.open("POST","register.php");
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.send("username="+form.username.value+"&password="+form.password.value);
}
</script>
</head>
<body>
<form>
<fieldset>
<legend>register</legend>
username:<input type="text" name="username" /><sapn id="note1"></sapn><br />
password:<input type="password" name="password" value=""/><span id="note2"></span><br />
checkpassword:<input type="password" name="chkpasswd" value="" onblur="checkpw(this.form)" /><span id="note3"></span><br />
<input type="button" value="sent" onclick="register(this.form)" />
<p id="check"></p>
</fieldset>
</form>
<a href="login.html">Back</a>
<a href="index.html">index</a>
</body>
</html>
@vedantrajkul
Copy link

where is login.php file

@vedantrajkul
Copy link

??

@vedantrajkul
Copy link

i need login.php file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment