| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||||||||||||||||||
|
||||||||||||||||||||
|
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:
It then queries the database asp Code:
If a valid record is found it writes them to variables asp Code:
The content to be included is then output asp Code:
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:
ajax_populate_form_getDetails.asp asp Code:
See attachment for Scripts and Database |
|
#2
|
|||||
|
|||||
|
Just a little code snippet to call the script from a dropdown
asp Code:
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]
__________________
CyberTechHelp |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > AJAX - Populate a form dynamically |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|