Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old March 22nd, 2006, 09:03 AM
degsy degsy is offline
Contributing User
ASP Free God 2nd Plane (6000 - 6499 posts)
 
Join Date: Aug 2005
Location: North East, UK
Posts: 6,191 degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 19 h 41 m 52 sec
Reputation Power: 122
AJAX - Populate a form dynamically

Ajax Populate Form

This is an example of how to type some info into a textfield and populate the rest of the form.


Querying the database via QueryString
The ASP/SQL is a simple QueryString

e.g.
Code:
ajax_populate_form_getDetails.asp?user_name=john


Code:
SELECT user_email, user_id FROM ajax_user WHERE user_name = 'john'

You could also change this to a LIKE statement
Code:
"SELECT user_email, user_id FROM ajax_user WHERE user_name LIKE '" & variable & "%'"



The "getDetails" part of the string grabs the variable from the QueryString
asp Code:
Original - asp Code
  1.  
  2. If (Request.QueryString("user_name") <> "") Then
  3.   getDetails__MMColParam = Request.QueryString("user_name")
  4. End If



It then queries the database
asp Code:
Original - asp Code
  1.  
  2. getDetails.Source = "SELECT user_email, user_id FROM ajax_user WHERE user_name = '" + Replace(getDetails__MMColParam, "'", "''") + "'"


If a valid record is found it writes them to variables
asp Code:
Original - asp Code
  1.  
  2. If Not getDetails.EOF Or Not getDetails.BOF Then
  3.     user_email = getDetails("user_email")
  4.     user_id = getDetails("user_id")
  5. End If



The content to be included is then output
asp Code:
Original - asp Code
  1.  
  2. Email:
  3. <input name="email" type="text" id="email" value="<%=user_email%>">
  4.     <br>
  5.     Id:
  6.     <input name="textfield" type="text" size="3" value="<%=user_id%>">





Ajax/Javascript to query the database
The difference here is that were do not need to submit the page. We can use Ajax/Javascript to make the request dynamically onkeyup (you could use onkeypress or onblur etc).


The Ajax script used can be found here
Dynamic Ajax Content



How to pass the querystring
The important code is to pass the value of the textfield to the ajax script

Code:
<input name="name" type="text" id="name" onKeyUp="ajaxpage('ajax_populate_form_getDetails.asp?user_n  ame=' + this.value,'details')">



The other important part is how and where to show the results.
With the Ajax script above it is basically including the page into the element that we choose.

Code:
function ajaxpage(url, containerid)


So we are calling ajax_populate_form_getDetails.asp plus a querystring and are going to make it appear in the element ID "details"

Code:
<span id="details">
   Email:
    <input name="email" type="text" id="email">
    <br>
    Id:
    <input name="textfield" type="text" size="3">
	</span>

The content of this ID is dummy content. It is just there for show.
When we call the Ajax script we will grab our new content


Code:
function loadpage(page_request, containerid)

This function takes the content of the Ajax response and inserts it into the dumy ID "details"

Code:
document.getElementById(containerid).innerHTML=pag  e_request.responseText






Here are the scripts in full (There is a bit of Dreamweaver coding). Change to your own coding style. The logic is the important thing.


ajax_populate_form.asp
asp Code:
Original - asp Code
  1.  
  2. <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
  3. <%
  4. db = Server.MapPath(".\ajax_populate_form.mdb")
  5. Conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db
  6.  
  7. Dim valid_names
  8. Dim valid_names_numRows
  9.  
  10. Set valid_names = Server.CreateObject("ADODB.Recordset")
  11. valid_names.ActiveConnection = Conn
  12. valid_names.Source = "SELECT user_name FROM ajax_user"
  13. valid_names.CursorType = 0
  14. valid_names.CursorLocation = 2
  15. valid_names.LockType = 1
  16. valid_names.Open()
  17.  
  18. While NOT valid_names.EOF
  19.     names = names & valid_names("user_name") & ", " 'Create a comma delimited string of availabel user names
  20.   valid_names.MoveNext()
  21. Wend
  22.  
  23. valid_names.Close()
  24. Set valid_names = Nothing
  25. %>
  26. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  27. <html>
  28. <head>
  29. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  30. <title>Populate Form 2</title>
  31. <script type="text/javascript">
  32.  
  33. /***********************************************
  34. * Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
  35. * This notice MUST stay intact for legal use
  36. * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
  37. ***********************************************/
  38.  
  39. var loadedobjects=""
  40. var rootdomain="http://"+window.location.hostname
  41.  
  42. function ajaxpage(url, containerid){
  43. var page_request = false
  44.     if (window.XMLHttpRequest){ // if Mozilla, Safari etc
  45.         page_request = new XMLHttpRequest()
  46.     }
  47.     else if (window.ActiveXObject){ // if IE
  48.         try {
  49.             page_request = new ActiveXObject("Msxml2.XMLHTTP")
  50.         }
  51.         catch (e){
  52.             try{
  53.                 page_request = new ActiveXObject("Microsoft.XMLHTTP")
  54.             }
  55.             catch (e){
  56.             }
  57.         }
  58.     }
  59.     else{
  60.         return false
  61.     }
  62.    
  63. page_request.onreadystatechange=function(){
  64.     loadpage(page_request, containerid)
  65. }
  66.    
  67. page_request.open('GET', url, true)
  68. page_request.send(null)
  69. }
  70.  
  71.  
  72. function loadpage(page_request, containerid){
  73.     if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
  74.         document.getElementById(containerid).innerHTML=pag  e_request.responseText
  75.     }
  76. </script>
  77. </head>
  78.  
  79. <body>
  80. <form name="form1" method="post" action="">
  81.   <p>Name:
  82.     <input name="name" type="text" id="name" onKeyUp="ajaxpage('ajax_populate_form_getDetails.asp?user_n  ame=' + this.value,'details')">
  83. <%
  84. ' Write out available user names to use as examples
  85. names = Trim(names)
  86. names = Left(names,Len(names)-1) 'Remove the end comma from the string
  87. Response.Write names
  88. %>
  89.     <br>
  90.   *type a name from the list</p>
  91.   <p>
  92.  
  93.   <span id="details">
  94.    Email:
  95.     <input name="email" type="text" id="email">
  96.     <br>
  97.     Id:
  98.     <input name="textfield" type="text" size="3">
  99.     </span>
  100.   </p>
  101. </form>
  102. </body>
  103. </html>



ajax_populate_form_getDetails.asp
asp Code:
Original - asp Code
  1.  
  2. <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
  3. <%
  4. db = Server.MapPath(".\ajax_populate_form.mdb")
  5. Conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & db
  6.  
  7. Dim getDetails__MMColParam
  8. getDetails__MMColParam = "1"
  9. If (Request.QueryString("user_name") <> "") Then
  10.   getDetails__MMColParam = Request.QueryString("user_name")
  11. End If
  12.  
  13. Dim getDetails
  14. Dim getDetails_numRows
  15.  
  16. Set getDetails = Server.CreateObject("ADODB.Recordset")
  17. getDetails.ActiveConnection = Conn
  18. getDetails.Source = "SELECT user_email, user_id FROM ajax_user WHERE user_name = '" + Replace(getDetails__MMColParam, "'", "''") + "'"
  19. getDetails.CursorType = 0
  20. getDetails.CursorLocation = 2
  21. getDetails.LockType = 1
  22. getDetails.Open()
  23.  
  24. If Not getDetails.EOF Or Not getDetails.BOF Then
  25.     user_email = getDetails("user_email")
  26.     user_id = getDetails("user_id")
  27. End If
  28.  
  29. getDetails.Close()
  30. Set getDetails = Nothing
  31. %>
  32. Email:
  33. <input name="email" type="text" id="email" value="<%=user_email%>">
  34.     <br>
  35.     Id:
  36.     <input name="textfield" type="text" size="3" value="<%=user_id%>">
  37.  



See attachment for Scripts and Database
Attached Files
File Type: zip ajax_populate_form.zip (8.5 KB, 1055 views)

Reply With Quote
  #2  
Old February 28th, 2007, 10:10 AM
degsy degsy is offline
Contributing User
ASP Free God 2nd Plane (6000 - 6499 posts)
 
Join Date: Aug 2005
Location: North East, UK
Posts: 6,191 degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level)degsy User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 19 h 41 m 52 sec
Reputation Power: 122
Just a little code snippet to call the script from a dropdown

asp Code:
Original - asp Code
  1. <p>or choose a name<br>
  2.     <select name="selName" id="selName" onchange="ajaxpage('ajax_populate_form_getDetails.asp?user_n  ame=' + this.value,'details')">
  3.     <option>Select a name</option>
  4.     <% For Each n In Split(names,",") %>
  5.     <option value="<%=Trim(n)%>"><%=n%></option>
  6.     <% Next %>
  7.     </select>
  8.   </p>


Example
Code:
<select name="selName" id="selName" onchange="ajaxpage('ajax_populate_form_getDetails.asp?user_n  ame=' + this.value,'details')">
	<option>Select a name</option>
	
	<option value="John">John</option>
	
	<option value="Paul"> Paul</option>
	
	<option value="George"> George</option>
	
	<option value="Ringo"> Ringo</option>
	
    </select> 


[code]
Comments on this post
D.O.M.I.N.A.T.O.R agrees: I'm .net follower, but good work!
deniz_seasdie agrees: Good work. Thanx
__________________
CyberTechHelp

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > AJAX - Populate a form dynamically


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek