473,804 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extract info from a textbox using Onblur

31 New Member
I'm trying to extract data from 3 textboxes to another line in the same page using the onblur command.

My client inputs their 5 digit zipcode and OnBlur fills the city and state from a database without refreshing.


I want to be able to extract or grab the info from the textboxes: zipcode, city, and state and place it at the top of the form at the same time when OnBlur is executed, in other words I want Onbllur to execute the auto fill in of the city and state and also do the update and show all together as follows:

example:
Client: inputs: zipcode box:95051
database fills: city box and state box

Santa Clara, CA 95051 <-- This is the result without a textbox tha I wan to achive to be located in the same form.

Expand|Select|Wrap|Line Numbers
  1. Tag: <input type="text" size="30" maxlength="5" name="zip" value="" id="zip" onBlur="updateCityState();"> 


Any help or suggestions that might point me in the right direction will be appreciated :)

Do I need to use additional tags to acomplish this? can you show me the code?

Ed
Jul 21 '07 #1
15 3987
pbmods
5,821 Recognized Expert Expert
Heya, Ed.

What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Jul 21 '07 #2
edwire
31 New Member
Hi pbmods, here's the code:

the php:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /**
  3.  * Connects to the database.
  4.  * Return false if connection failed.
  5.  * Be sure to change the $database_name. $database_username , and 
  6.  * $database_password values  to reflect your database settings.
  7.  */
  8. function db_connect() {
  9.   $database_name = 'XXX'; // Set this to your Database Name
  10.   $database_username = 'XXX'; // Set this to your MySQL username
  11.   $database_password = 'XXX'; // Set this to your MySQL password
  12.   $result = mysql_pconnect('localhost',$database_username, $database_password); 
  13.   if (!$result) return false;
  14.   if (!mysql_select_db($database_name)) return false;
  15.   return $result;
  16. }
  17. $conn = db_connect(); // Connect to database
  18. if ($conn) {
  19.   $zipcode = $_GET['param']; // The parameter passed to us
  20.   $query = "select * from zipcodes where zipcode = '$zipcode'";
  21.   $result = mysql_query($query,$conn);
  22.   $count = mysql_num_rows($result);
  23.   if ($count > 0) {
  24.     $city = mysql_result($result,0,'city');
  25.     $state = mysql_result($result,0,'state');
  26.     $areacode = mysql_result($result,0,'areacode');
  27.   }
  28. }
  29. if (isset($city) && isset($state) && isset($areacode)) { 
  30.   $return_value = $city . "," . $state . "," . $areacode; 
  31. }
  32. else {  
  33.   $return_value = "not correct".",".$_GET['param']; // Include Zip for debugging purposes
  34. }
  35. echo $return_value; // This will become the response value for the XMLHttpRequest object
  36. ?>

***the html:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4. <title>ZIP Code to City and State using XmlHttpRequest</title>
  5. <script language="javascript"  type="text/javascript">
  6. var url = "CityState.php?param="; // The server-side script
  7. function handleHttpResponse() {
  8.   if (http.readyState == 4) {
  9.     // Split the comma delimited response into an array
  10.     results = http.responseText.split(",");
  11.     document.getElementById('city').value = results[0];
  12.        document.getElementById('state').value = results[1];
  13.       document.getElementById('areacode').value = results[2];
  14.   }
  15. }
  16. function updateCityState() {
  17.   var zipValue = document.getElementById("zip").value;
  18.   http.open("GET", url + escape(zipValue), true);
  19.   http.onreadystatechange = handleHttpResponse;
  20.   http.send(null);
  21. }
  22. function getHTTPObject() {
  23.   var xmlhttp;
  24.   /*@cc_on
  25.   @if (@_jscript_version >= 5)
  26.     try {
  27.       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  28.     } catch (e) {
  29.       try {
  30.         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  31.       } catch (E) {
  32.         xmlhttp = false;
  33.       }
  34.     }
  35.   @else
  36.   xmlhttp = false;
  37.   @end @*/
  38.   if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  39.     try {
  40.       xmlhttp = new XMLHttpRequest();
  41.     } catch (e) {
  42.       xmlhttp = false;
  43.     }
  44.   }
  45.   return xmlhttp;
  46. }
  47. var http = getHTTPObject(); // We create the HTTP Object
  48. </script>
  49. <style type="text/css">
  50. <!--
  51. .style1 {
  52.     font-size: 16px;
  53.     font-weight: bold;
  54.     font-style: italic;
  55. }
  56. -->
  57. </style>
  58. </head>
  59. <body>
  60. <p>&nbsp;</p>
  61. <p>&nbsp;</p>
  62. <p class="style1"><fieldset>
  63. <legend></legend>
  64. <legend></legend>
  65. <legend></legend>
  66. <legend>
  67. Computer information:
  68. </legend>
  69. <input type="text" name="city" id="city" /><input type="text" name="areacode" id="areacode" />
  70. </fieldset></p>
  71.  
  72. <p class="style1">Schedule your services now!</p>
  73. <p>&nbsp;</p>
  74. <p>&nbsp;</p>
  75. <p>&nbsp;</p>
  76. <p>&nbsp; </p>
  77. <form action="post">
  78.   <p>ZIP code:
  79.     <input type="text" size="5" name="zip" id="zip" onblur="updateCityState();" />
  80.   </p>
  81.   City:
  82.   <input type="text" name="city" id="city" />
  83.   State:
  84.   <input type="text" size="2" name="state" id="state" />
  85. </form>
  86. </body>
  87. </html>
right now it shows inside an input text, I'd like to show it without the box.

Thank you in advance!

Ed
Jul 21 '07 #3
pbmods
5,821 Recognized Expert Expert
Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Jul 21 '07 #4
edwire
31 New Member
Hi pbmods here again trying to find an answer to my problem.

This is a working test page: http://www.echildcarem anagement.com/prototype/step6.html

Here you can see that it fills in the state and city as soon as you input the zipcode but the result I'd like show is as follows:

Computer information:
Santa Clara, California
95051


Thanks again for your time.

Ed
Jul 21 '07 #5
pbmods
5,821 Recognized Expert Expert
Heya, Ed.

Ah. Ok. I see what you're getting at.

Alrighty. This will be fairly easy to accomplish. First, you need to create two elements:
  • a DIV to hold the city/state, and
  • a DIV to hold the zip code.

Something like this:
Expand|Select|Wrap|Line Numbers
  1. <div id="cityState">&nbsp;</div>
  2. <div id="zipCode">&nbsp;</div>
  3.  
The non-breaking spaces are used so that your code will validate. But I digress.

Once you find the values that are associated with the ZIP code, you simply change the nodeValue of the #text node inside each of the DIVs:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('cityState').firstChild.nodeValue = city + ', ' + state;
  2. document.getElementById('zipCode').firstChild.nodeValue = zip;
  3.  
Bingo.

Incidentally, why 'firstChild'? Welcome to DOM!
Jul 22 '07 #6
edwire
31 New Member
Thnaks for your quick responde pbmods!
I have a question;
document.getEle mentById('zipCo de').firstChild .node Value = zip;

on this code line is there space or spaces between "firstChild.nod e" and "Value"? or is all together with out spaces?

Thanks again.

Ed
Jul 22 '07 #7
edwire
31 New Member
Hi pbmods, I'm still not able to include the code. Now is not showing anything at all, here is the code + I've added the your suggestion:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4. <title>ZIP Code to City and State using XmlHttpRequest</title>
  5. <script language="javascript"  type="text/javascript">
  6. var url = "CityState.php?param="; // The server-side script
  7. function handleHttpResponse() {
  8.   if (http.readyState == 4) {
  9.     // Split the comma delimited response into an array
  10.     results = http.responseText.split(",");
  11.     document.getElementById('city').value = results[0];
  12.        document.getElementById('state').value = results[1];
  13.       document.getElementById('areacode').value = results[2];
  14.       document.getElementById('cityState').firstChild.node Value = city + ', ' + state + zip;
  15.       document.getElementById('areaCode').firstChild.node Value = areacode;
  16.   }
  17. }
  18. function updateCityState() {
  19.   var zipValue = document.getElementById("zip").value;
  20.   http.open("GET", url + escape(zipValue), true);
  21.   http.onreadystatechange = handleHttpResponse;
  22.   http.send(null);
  23. }
  24. function getHTTPObject() {
  25.   var xmlhttp;
  26.   /*@cc_on
  27.   @if (@_jscript_version >= 5)
  28.     try {
  29.       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  30.     } catch (e) {
  31.       try {
  32.         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  33.       } catch (E) {
  34.         xmlhttp = false;
  35.       }
  36.     }
  37.   @else
  38.   xmlhttp = false;
  39.   @end @*/
  40.   if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  41.     try {
  42.       xmlhttp = new XMLHttpRequest();
  43.     } catch (e) {
  44.       xmlhttp = false;
  45.     }
  46.   }
  47.   return xmlhttp;
  48. }
  49. var http = getHTTPObject(); // We create the HTTP Object
  50. </script>
  51. <style type="text/css">
  52. <!--
  53. .style1 {
  54.     font-size: 16px;
  55.     font-weight: bold;
  56.     font-style: italic;
  57. }
  58. -->
  59. </style>
  60. </head>
  61. <body>
  62. <p>&nbsp;</p>
  63. <p>&nbsp;</p>
  64. <p class="style1"><fieldset>
  65. <legend>Computer information:</legend>
  66. <legend>
  67. <div id="cityState">&nbsp;</div>
  68. <div id="areaCode">&nbsp;</div>
  69. </legend>
  70. </fieldset></p>
  71.  
  72. <p class="style1">Schedule your services now!</p>
  73. <p>&nbsp;</p>
  74. <p>&nbsp;</p>
  75. <p>&nbsp;</p>
  76. <p>&nbsp; </p>
  77. <form action="post">
  78.   <p>ZIP code:
  79.     <input type="text" size="5" name="zip" id="zip" onblur="updateCityState();" />
  80.   </p>
  81.   City:
  82.   <input type="text" name="city" id="city" />
  83.   State:
  84.   <input type="text" size="2" name="state" id="state" />
  85. </form>
  86. </body>
  87. </html>
The Idea is to show the Following:

Computer Information:
Santa Clara, CA 95051
Northen California District

am I missing something? or am I putting the code in the wrong place?


Thanks for your help.

Ed
Jul 22 '07 #8
edwire
31 New Member
pbmods sorry! disregard my last 2 replies please. Here's a good example:

Got to http://www.echildcaremanagement.com/...ype/step6.html and input any valid zipcode the onblur comand will fill in the city and state. Right above the zipcode is where I want to show the the following:

Computer Information:
Santa Clara, CA 95051
Northern District

Where do I input your code? is there spaces between node and Value?

Here is the code without any modificatins:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4. <title>ZIP Code to City and State using XmlHttpRequest</title>
  5. <script language="javascript"  type="text/javascript">
  6. var url = "CityState.php?param="; // The server-side script
  7. function handleHttpResponse() {
  8.   if (http.readyState == 4) {
  9.     // Split the comma delimited response into an array
  10.     results = http.responseText.split(",");
  11.     document.getElementById('city').value = results[0];
  12.        document.getElementById('state').value = results[1];
  13.       document.getElementById('areacode').value = results[2];
  14.   }
  15. }
  16. function updateCityState() {
  17.   var zipValue = document.getElementById("zip").value;
  18.   http.open("GET", url + escape(zipValue), true);
  19.   http.onreadystatechange = handleHttpResponse;
  20.   http.send(null);
  21. }
  22. function getHTTPObject() {
  23.   var xmlhttp;
  24.   /*@cc_on
  25.   @if (@_jscript_version >= 5)
  26.     try {
  27.       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  28.     } catch (e) {
  29.       try {
  30.         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  31.       } catch (E) {
  32.         xmlhttp = false;
  33.       }
  34.     }
  35.   @else
  36.   xmlhttp = false;
  37.   @end @*/
  38.   if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  39.     try {
  40.       xmlhttp = new XMLHttpRequest();
  41.     } catch (e) {
  42.       xmlhttp = false;
  43.     }
  44.   }
  45.   return xmlhttp;
  46. }
  47. var http = getHTTPObject(); // We create the HTTP Object
  48. </script>
  49. <style type="text/css">
  50. <!--
  51. .style1 {
  52.     font-size: 16px;
  53.     font-weight: bold;
  54.     font-style: italic;
  55. }
  56. -->
  57. </style>
  58. </head>
  59. <body>
  60. <p>&nbsp;</p>
  61. <p>&nbsp;</p>
  62. <p class="style1"><fieldset>
  63. <legend>Computer information:</legend>
  64. <legend></legend>
  65. <legend></legend>
  66. <legend>
  67. </legend>
  68. </fieldset></p>
  69.  
  70. <p class="style1">Schedule your services now!</p>
  71. <p>&nbsp;</p>
  72. <p>&nbsp;</p>
  73. <p>&nbsp;</p>
  74. <p>&nbsp; </p>
  75. <form action="post">
  76.   <p>ZIP code:
  77.     <input type="text" size="5" name="zip" id="zip" onblur="updateCityState();" />
  78.   </p>
  79.   City:
  80.   <input type="text" name="city" id="city" />
  81.   State:
  82.   <input type="text" size="2" name="state" id="state" />
  83. </form>
  84. </body>
  85. </html>

Thanks for your help!

Ed
Jul 22 '07 #9
edwire
31 New Member
Also where do I find the values? are you talking about the values from the database?

"Once you find the values that are associated with the ZIP code, you simply change the nodeValue of the #text node inside each of the DIVs:"


Ed
Jul 22 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

9
10122
by: Jerry | last post by:
In limiting textbox input to 500 characters I would like to include a dynamic count of characters input while the user is typing into a textbox. This would obviously be a client side control, possibly a custom validator with a function written in javascript. Has anyone done this? Does someone have an example? Regards
2
1930
by: Oleg Ogurok | last post by:
Hi there, I have a modified ASP.NET TextBox control that formats its output as the phone number, e.g. if you enter "1234567890" it'll change the value to "123-456-7890". This is done by a javascript function called on "onblur" event of the textbox. However, it seems validation happens before the onblur event, therefore if I enter "1234567890", the validator that checks the phone number is set to "Invalid" state. How can I format the...
5
4591
by: ian | last post by:
Hi, I am currently using a Javascript function to dissallow the enter key on my ASP.NET (2.0) web page, as follows: function fnTrapKP(){ if (document.all) { if (event.keyCode == 13) {
3
1984
by: musosdev | last post by:
Hi I want to have a textbox on my webpage that shows 'Type here' in gray. When you click on the textbox, that text disappears and you can type your search, rather than having to select it and press delete. Be super-cool if it could also change the textcolor as well. Any ideas on how to do this? I dont mind using another control, as long as its free (its for a charity intranet).
2
3093
by: ThunderMusic | last post by:
Hi, I have a form in which there are many <asp:textbox> tags. When I run the form, we use tab to navigate across the textboxes, but one of our client noticed that when we navigate to some readonly textboxes, the cursor (text cursor, not mouse cursor) disapears and when we tab to the next textbox and come back (shift-tab), not it's ok. Notice that when we navigate to each textbox, the text is automaticaly selected which is not the case for...
0
1792
by: venu | last post by:
Hi All I need some help from u guys. iam working on aspx page.in this page i have lot fields. text boxes and dropdown lists .. First filed should be a autocomplete filed. this text has onblur event also based on this text box when onblur event is fired that time other fields will populate. This time this textbox should be a auto-complete . this is not happend in my page. this page has lot of onblur event's also.
4
8684
by: Dabbler | last post by:
Is there a way to mark the text in a TextBox control as selected so when the user types a new value the existing text is replaced? Thanks
1
1996
by: bsm | last post by:
I have created one User controls which contains 1. Textbox and 2. ImageButton (to display calendar) (I tried to create my own datetime control). I have created "onblur" event on textbox event, so that It will validate that Textbox should not empty. I shown calendar window when user clicks the ImageButton. My problem is when user enters into Textbox and he has to fill the
1
2612
by: Duncan | last post by:
Hi all, I'm tearing my hair out on this! I have a simple ASP.NET 2 page with a TextBox control, in the PreRender I set ClientSide events:- Response.Attributes.Add("onchange", "javascript:alert('Changed');"); Response.Attributes.Add("onfocus",
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10070
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.