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 Auto Fill Textbox (PHP+MySQL & ASP+Access)

Ajax Auto Fill Textbox (PHP+MySQL & ASP+Access) ตัวอย่างการเขียน Ajax ในการ Automatically fill in form หรือใส่ค่าอัตโนมัติเมื่อมีการ เลือกข้อมูลที่เป็น Master และ Detail ของข้อมูลจะถูก Auto Fill ลงใน Textbox อย่างอัตโนมัติ

ในตัวอย่างเมื่อมีการกรอก ProductID โปรแกรมจะทำการ Fill ในช่องของ Product Name และ Price อย่างอัตโนมัติ

PHP & MySQL

AjaxAutoFill.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(fProductID,fProductName,fPrice) {
		  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 = 'AjaxGetFill.php';
		  var pmeters = "tProductID=" + encodeURI( document.getElementById(fProductID).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(fProductName).innerHTML = "..";
				//}

				if(HttPRequest.readyState == 4) // Return Request
				{
					var myProduct = HttPRequest.responseText;
					if(myProduct != "")
					{
						var myArr = myProduct.split("|");
						document.getElementById(fProductName).value = myArr[0];
						document.getElementById(fPrice).value = myArr[1];
					}
				}
				
			}

	   }
	</script>
<body>
<h1>Auto Fill Form</h1>
<form name="frmMain">
<table width="390" border="1">
  <tr>
    <th width="84">Product ID </th>
    <th width="185">Product Name </th>
    <th width="99">Price</th>
  </tr>
  <tr>
    <th>
      <div align="center">
        <input type="text" name="txtProductID1" id="txtProductID1" size="5" OnChange="JavaScript:doCallAjax('txtProductID1','txtProductName1','txtPrice1');">
      </div></th>
    <th><input type="text" name="txtProductName1" id="txtProductName1" size="30"></th>
    <th><input type="text" name="txtPrice1" id="txtPrice1" size="10"></th>
  </tr>
  <tr>
    <th>
      <div align="center">
        <input type="text" name="txtProductID2" id="txtProductID2" size="5" OnChange="JavaScript:doCallAjax('txtProductID2','txtProductName2','txtPrice2');">
      </div></th>
    <th><input type="text" name="txtProductName2" id="txtProductName2" size="30"></th>
    <th><input type="text" name="txtPrice2" id="txtPrice2" size="10"></th>
  </tr>
  <tr>
    <th>
      <div align="center">
        <input type="text" name="txtProductID3" id="txtProductID3" size="5" OnChange="JavaScript:doCallAjax('txtProductID3','txtProductName3','txtPrice3');">
      </div></th>
    <th><input type="text" name="txtProductName3" id="txtProductName3" size="30"></th>
    <th><input type="text" name="txtPrice3" id="txtPrice3" size="10"></th>
  </tr>
</table>
</form>
</body>
</html>









AjaxGetFill.php

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

	$strProduct = trim($_POST["tProductID"]);

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

	$strSQL = "SELECT * FROM product WHERE ProductID = '".$strProduct."' ";
	$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
	$objResult = mysql_fetch_array($objQuery);
	if($objResult)
	{
		echo $objResult["ProductName"]."|".$objResult["Price"];
	}

	mysql_close($objConnect);
?>



Screenshot

Ajax Auto Fill Textbox







ASP & Access

AjaxAutoFill.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(fProductID,fProductName,fPrice) {
		  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 = 'AjaxGetFill.asp';
		  var pmeters = "tProductID=" + encodeURI( document.getElementById(fProductID).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(fProductName).innerHTML = "..";
				//}

				if(HttPRequest.readyState == 4) // Return Request
				{
					var myProduct = HttPRequest.responseText;
					if(myProduct != "")
					{
						var myArr = myProduct.split("|");
						document.getElementById(fProductName).value = myArr[0];
						document.getElementById(fPrice).value = myArr[1];
					}
				}
				
			}

	   }
	</script>
<body>
<h1>Auto Fill Form</h1>
<form name="frmMain">
<table width="390" border="1">
  <tr>
    <th width="84">Product ID </th>
    <th width="185">Product Name </th>
    <th width="99">Price</th>
  </tr>
  <tr>
    <th>
      <div align="center">
        <input type="text" name="txtProductID1" id="txtProductID1" size="5" OnChange="JavaScript:doCallAjax('txtProductID1','txtProductName1','txtPrice1');">
      </div></th>
    <th><input type="text" name="txtProductName1" id="txtProductName1" size="30"></th>
    <th><input type="text" name="txtPrice1" id="txtPrice1" size="10"></th>
  </tr>
  <tr>
    <th>
      <div align="center">
        <input type="text" name="txtProductID2" id="txtProductID2" size="5" OnChange="JavaScript:doCallAjax('txtProductID2','txtProductName2','txtPrice2');">
      </div></th>
    <th><input type="text" name="txtProductName2" id="txtProductName2" size="30"></th>
    <th><input type="text" name="txtPrice2" id="txtPrice2" size="10"></th>
  </tr>
  <tr>
    <th>
      <div align="center">
        <input type="text" name="txtProductID3" id="txtProductID3" size="5" OnChange="JavaScript:doCallAjax('txtProductID3','txtProductName3','txtPrice3');">
      </div></th>
    <th><input type="text" name="txtProductName3" id="txtProductName3" size="30"></th>
    <th><input type="text" name="txtPrice3" id="txtPrice3" size="10"></th>
  </tr>
</table>
</form>
</body>
</html>









AjaxGetFill.asp

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

	strProduct = Trim(Request.Form("tProductID"))

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

	strSQL = "SELECT * FROM product WHERE ProductID = '" & strProduct & "' "
	Set objRec = Conn.Execute(strSQL)

	If Not objRec.EOF Then
		Response.write objRec.Fields("ProductName").Value  & "|" & objRec.Fields("Price").Value
	End IF

	Set Conn = Nothing
%>


   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2009-11-14 11:36:04 / 2017-03-15 13:46:11
  Download : Download  Ajax Auto Fill Textbox (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 Login (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 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 อัตราราคา คลิกที่นี่