Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

Ajax Login (PHP+MySQL & ASP+Access)

Ajax Login (PHP+MySQL & ASP+Access) การทำ User Login Form ด้วย Ajax ในตัวอย่างผมจะให้ผู้ใช้กรอก User & Password และทำการตรวจสอบข้อมูลในฐานข้อมูล ซึ่งสามารถตรวจสอบผลลัพธ์แบบเร็วปี๊ด ๆ Code ง่ายไม่ยากกว่าที่คิดครับ

PHP & MySQL

AjaxPHPLoginForm1.php

<?php
	/*** By Weerachai Nukitram***/
	/***  http://www.ThaiCreate.Com ***/	
?>
<html>
<head>
<title>ThaiCreate.Com Ajax Tutorial</title>
</head>
<script language="JavaScript">
	   var HttPRequest = false;

	   function doCallAjax() {
		  HttPRequest = false;
		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
			 HttPRequest = new XMLHttpRequest();
			 if (HttPRequest.overrideMimeType) {
				HttPRequest.overrideMimeType('text/html');
			 }
		  } else if (window.ActiveXObject) { // IE
			 try {
				HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
			 } catch (e) {
				try {
				   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			 }
		  } 
		  
		  if (!HttPRequest) {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }
	
		  var url = 'AjaxPHPLoginForm2.php';
		  var pmeters = "tUsername=" + encodeURI( document.getElementById("txtUsername").value) +
						"&tPassword=" + encodeURI( document.getElementById("txtPassword").value );

			HttPRequest.open('POST',url,true);

			HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			HttPRequest.setRequestHeader("Content-length", pmeters.length);
			HttPRequest.setRequestHeader("Connection", "close");
			HttPRequest.send(pmeters);
			
			
			HttPRequest.onreadystatechange = function()
			{

				if(HttPRequest.readyState == 3)  // Loading Request
				{
					document.getElementById("mySpan").innerHTML = "Now is Loading...";
				}

				if(HttPRequest.readyState == 4) // Return Request
				{
					if(HttPRequest.responseText == 'Y')
					{
						window.location = 'AjaxPHPLoginForm3.php';
					}
					else
					{
						document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
					}
				}
				
			}

	   }
	</script>
<body>
<h1>Login Form</h1>
<form name="frmMain">
<span id="mySpan"></span>
<table width="274" border="1">
  <tr>
    <th width="117">
      <div align="left">Username</div></th>
    <th><input type="text" name="txtUsername" id="txtUsername" size="20"></th>
  </tr>
  <tr>
    <th width="117">
      <div align="left">Password</div></th>
    <th><input type="password" name="txtPassword" id="txtPassword" size="20"></th>
  </tr> 
</table>
<br>
<input name="btnLogin" type="button" id="btnLogin" OnClick="JavaScript:doCallAjax();" value="Login">
</form>
</body>
</html>









AjaxPHPLoginForm2.php

<?php
	/*** By Weerachai Nukitram ***/
	/***  http://www.ThaiCreate.Com ***/

	session_start();

	$strUsername = trim($_POST["tUsername"]);
	$strPassword = trim($_POST["tPassword"]);
	
	//*** Check Username ***//
	if(trim($strUsername) == "")
	{
		echo "<font color=red>**</font> Plase input [Username]";
		exit();
	}
	
	//*** Check Password ***//
	if(trim($strPassword) == "")
	{
		echo "<font color=red>**</font> Plase input [Password]";
		exit();
	}
	

	$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
	$objDB = mysql_select_db("mydatabase");


	//*** Check Username & Password ***//

	$strSQL = "SELECT * FROM account WHERE Username = '".$strUsername."' and Password = '".$strPassword."' ";
	$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
	$objResult = mysql_fetch_array($objQuery);
	if($objResult)
	{
		echo "Y";

		//*** Session ***//
		$_SESSION["Username"] = $strUsername;
		session_write_close();
	}
	else
	{
		echo "<font color=red>**</font> Username & Password is wrong";
	}

	mysql_close($objConnect);
?>


AjaxPHPLoginForm3.php

<?php
	/*** By Weerachai Nukitram ***/
	/***  http://www.ThaiCreate.Com ***/

	session_start();
	if($_SESSION["Username"] == "")
	{
		header("location:AjaxLoginForm.html");
		exit();
	}

	$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
	$objDB = mysql_select_db("mydatabase");


	$strSQL = "SELECT * FROM account WHERE Username = '".$_SESSION["Username"]."'  ";
	$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
	$objResult = mysql_fetch_array($objQuery);
?>
<html>
<head>
<title>ThaiCreate.Com Ajax Tutorial</title>
</head>
<body>
<h1>Welcome [<?php echo $objResult["Name"];?>] to My Website</h1>
</body>
</html>


Screenshot

Ajax Login Form

Ajax Login Form






ASP & Access

AjaxASPLoginForm1.asp

<%
	'*** By Weerachai Nukitram ***'
	'***  http://www.ThaiCreate.Com ***'
%>
<html>
<head>
<title>ThaiCreate.Com Ajax Tutorial</title>
</head>
<script language="JavaScript">
	   var HttPRequest = false;

	   function doCallAjax() {
		  HttPRequest = false;
		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
			 HttPRequest = new XMLHttpRequest();
			 if (HttPRequest.overrideMimeType) {
				HttPRequest.overrideMimeType('text/html');
			 }
		  } else if (window.ActiveXObject) { // IE
			 try {
				HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
			 } catch (e) {
				try {
				   HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			 }
		  } 
		  
		  if (!HttPRequest) {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }
	
		  var url = 'AjaxASPLoginForm2.asp';
		  var pmeters = "tUsername=" + encodeURI( document.getElementById("txtUsername").value) +
						"&tPassword=" + encodeURI( document.getElementById("txtPassword").value );

			HttPRequest.open('POST',url,true);

			HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			HttPRequest.setRequestHeader("Content-length", pmeters.length);
			HttPRequest.setRequestHeader("Connection", "close");
			HttPRequest.send(pmeters);
			
			
			HttPRequest.onreadystatechange = function()
			{

				if(HttPRequest.readyState == 3)  // Loading Request
				{
					document.getElementById("mySpan").innerHTML = "Now is Loading...";
				}

				if(HttPRequest.readyState == 4) // Return Request
				{
					if(HttPRequest.responseText == 'Y')
					{
						window.location = 'AjaxASPLoginForm3.asp';
					}
					else
					{
						document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
					}
				}
				
			}

	   }
	</script>
<body>
<h1>Login Form</h1>
<form name="frmMain">
<span id="mySpan"></span>
<table width="274" border="1">
  <tr>
    <th width="117">
      <div align="left">Username</div></th>
    <th><input type="text" name="txtUsername" id="txtUsername" size="20"></th>
  </tr>
  <tr>
    <th width="117">
      <div align="left">Password</div></th>
    <th><input type="password" name="txtPassword" id="txtPassword" size="20"></th>
  </tr> 
</table>
<br>
<input name="btnLogin" type="button" id="btnLogin" OnClick="JavaScript:doCallAjax();" value="Login">
</form>
</body>
</html>









AjaxASPLoginForm2.asp

<%
	'*** By Weerachai Nukitram ***'
	'***  http://www.ThaiCreate.Com ***'

	Option Explicit

	Dim strUsername,strPassword

	strUsername = Trim(Request.Form("tUsername"))
	strPassword = Trim(Request.Form("tPassword"))
	
	'*** Check Username ***'
	If Trim(strUsername) = "" Then
		Response.Write("<font color=red>**</font> Plase input [Username]")
		Response.End
	End IF
	
	'*** Check Password ***'
	If Trim(strPassword) = "" Then
		Response.Write("<font color=red>**</font> Plase input [Password]")
		Response.End
	End iF
	

	Dim Conn,strSQL,objRec
	Set Conn = Server.Createobject("ADODB.Connection")
	Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/mydatabase.mdb"),"" , ""


	'*** Check Username & Password ***'

	strSQL = "SELECT * FROM account WHERE Username = '"&strUsername&"' and Password = '"&strPassword&"' "
	Set objRec = Conn.Execute(strSQL)
	If Not objRec.EOF Then
		Response.Write("Y")

		'*** Session ***'
		Session("Username") = strUsername
	Else
		Response.Write("<font color=red>**</font> Username & Password is wrong")
	End IF

	objRec.Close()
	Conn.Close()
	Set objRec = Nothing
	Set Conn = Nothing
%>



AjaxASPLoginForm3.asp

<%
	'*** By Weerachai Nukitram ***'
	'***  http://www.ThaiCreate.Com ***'
	
	Option Explicit

	Dim Conn,strSQL,objRec
	Set Conn = Server.Createobject("ADODB.Connection")
	Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/mydatabase.mdb"),"" , ""

	If Trim(Session("Username")) = "" Then
		Response.Redirect("AjaxLoginForm.html")
		Response.End
	End IF

	strSQL = "SELECT * FROM account WHERE Username = '"&Session("Username")&"' "
	Set objRec = Conn.Execute(strSQL)

%>
<html>
<head>
<title>ThaiCreate.Com Ajax Tutorial</title>
<head>
<body>
<h1>Welcome [<%=objRec.Fields("Name").Value%>] to My Website</h1>
</body>
</html>


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2009-01-24 07:54:47 / 2017-03-15 13:53:43
  Download : Download  Ajax Login (PHP+MySQL & ASP+Access)
 Sponsored Links / Related

 
Ajax Introduction
Rating :

 
Ajax innerHTML & InnerText
Rating :

 
Ajax JavaScript & Event
Rating :

 
Ajax visibility hidden & visible
Rating :

 
Ajax Display Element
Rating :

 
Ajax Visibility & Display
Rating :

 
Ajax Disabled Element
Rating :

 
Ajax CreateElement
Rating :

 
Ajax setTimeOut
Rating :

 
Ajax responseText
Rating :

 
Ajax readyState
Rating :

 
Ajax Send Data Method POST (PHP & ASP)
Rating :

 
Ajax Send Data Method GET (PHP & ASP)
Rating :

 
Ajax Register Form (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Dropdownlist and Listmenu (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Upload file (PHP & ASP)
Rating :

 
Ajax Load Content (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Load Web Page (PHP & ASP)
Rating :

 
Ajax Loading Progress Icon
Rating :

 
Ajax List Record (PHP+MySQL & ASP+Access)
Rating :

 
Ajax List Record Paging/Pagination (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Add/Insert Record (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Edit/Update Record (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Delete Record (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Delete Record Part 2 (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Search Record (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Search Record Paging/Pagination (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Chat Room (PHP+Text & ASP+Text)
Rating :

 
Ajax Shopping Cart (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Contact Form (PHP & ASP)
Rating :

 
Ajax Image Gallery (PHP & ASP)
Rating :

 
Ajax Open Text file (PHP & ASP)
Rating :

 
Ajax Autocomplete
Rating :

 
Ajax Check Username (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Auto Fill Textbox (PHP+MySQL & ASP+Access)
Rating :

 
Ajax Realtime (PHP+MySQL & ASP+Access)
Rating :

 
Ajax jQuery
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 01
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่