Auto fill form fields using coldfusion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bonneylake
    Contributor
    • Aug 2008
    • 769

    Auto fill form fields using coldfusion

    Hey Everyone,

    Well i don't know if my question should be in javascript/ajax or coldfusion, i figure this is more of a coldfusion question. But if this is in the wrong section let me know an all gladly delete my question an put it in the correct section.

    Well what i am trying to do is pretty simple. I have a input field and someone starts typing in a customer number. As they type in the customer number the drop down box is populated. They then click on the drop down box an the rest of the form is populated (it gets all the information from a database).

    I found a script online that does almost exactly what i want to do except it is in php. I have found an example of this in every language except coldfusion and well i must admit i am not good at converting code from one language to another.

    But anyway i was wondering if anyone could provide me with an example on how i would do this in coldfusion instead of php? or if you know of a tutorial,forum, or example online i could look at?

    Here is the example page


    an here is the code


    i already created the drop down box part of this (since it is missing in the
    the example above), but like i said i just can't figure out how i would populate it using my coldfusion database. Here is what i got for the drop down box

    here is coldfusion/html
    Code:
    <cfquery name="getcustnum" datasource="CustomerSupport">
    usp_CS_Getcontacts
    </cfquery>
    <HTML>
    <HEAD>
    	<TITLE>JavaScript Toolbox - Auto-Complete</TITLE>
    <SCRIPT LANGUAGE="JavaScript" SRC="autocomplete.js"></SCRIPT>
    </HEAD>
    <BODY BGCOLOR=#FFFFFF LINK="#00615F" VLINK="#00615F" ALINK="#00615F">
    <FORM>
    Customer Number*
    <INPUT TYPE="text" NAME="input1" VALUE="" ONKEYUP="autoComplete(this,this.form.options,'value',true)" onChange="validate(this.form.custnum,'Customer Number')">
    <SELECT NAME="options" 
    onChange="this.form.input1.value=this.options[this.selectedIndex].value" onclick="">
    <cfoutput query="getcustnum">
    <option value="#fk_custNum#">#fk_custNum#</option>
    </cfoutput>
    </SELECT>
    Company Name:<input type="text" name="compname"  size="20" id="compname"/>
    First Name:<input type="text" name="fname"  size="20" id="fname"/>
    Last Name:<input type="text" name="lname"size="20" id="lname"/>
    </FORM>
    </BODY>
    </HTML>

    here is the javascript
    Code:
    function autoComplete (field, select, property, forcematch) {
    	var found = false;
    	for (var i = 0; i < select.options.length; i++) {
    	if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
    		found=true; break;
    		}
    	}
    	if (found) { select.selectedIndex = i; }
    	else { select.selectedIndex = -1; }
    	if (field.createTextRange) {
    		if (forcematch && !found) {
    			field.value=field.value.substring(0,field.value.length-1); 
    			return;
    			}
    		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
    		if (cursorKeys.indexOf(event.keyCode+";") == -1) {
    			var r1 = field.createTextRange();
    			var oldValue = r1.text;
    			var newValue = found ? select.options[i][property] : oldValue;
    			if (newValue != field.value) {
    				field.value = newValue;
    				var rNew = field.createTextRange();
    				rNew.moveStart('character', oldValue.length) ;
    				rNew.select();
    				}
    			}
    		}
    	}
    But thank you in advance :),
    Rach
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    For the populating of the dropdown box and the text boxes, you will need JavaScript. If you're having problems with that part, start a new thread in the JavaScript forum.

    For getting the values from the database, you will obviously need Coldfusion. If you want to convert that PHP file to Coldfusion, it's not that difficult, though I don't like how it's done. There's a better way if you're interested.

    Comment

    • bonneylake
      Contributor
      • Aug 2008
      • 769

      #3
      Hey Acoder,

      definately interested if you have a better way to go about getting the values from the database. I think i can figure out the javascript part, just very lost on the coldfusion part.

      Thanks in advance,
      Rach

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        The better way would involve not outputting JavaScript to be eval-ed as in the example you linked to. You can output HTML to be inserted directly, or XML or JSON to be parsed.

        From what you've described, it seems you're going to make two requests. One to populate the drop down and the second one to populate the fields. Are the options from a database too?

        Comment

        • bonneylake
          Contributor
          • Aug 2008
          • 769

          #5
          yes the options come from the database as well.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            You'll need to files then. Let's deal with populating the drop down first.

            You have a simple query which gets the values for the options, then you use cfloop to loop over and display the options, e.g.
            [code=cfm]<cfloop ...>
            <option value="<cfoutpu t>#val#</cfoutput>"><cfo utput>#opttext# </cfoutput>
            </cfloop>[/code]then in your client-side code, you just set the innerHTML of the select element. An alternative is to output XML or JSON which you can parse.

            Comment

            • bonneylake
              Contributor
              • Aug 2008
              • 769

              #7
              Acoder,

              Well i sorta understand what your saying. But wanted to ask would this drop down i had already created work? because this gets the information from the table for the drop down the way i have it, just kinda curious what the difference is.

              Code:
              <cfquery name="getcustnum" datasource="CustomerSupport">
              usp_CS_Getcontacts
              </cfquery>
              
              <SELECT NAME="options" >
              <cfoutput query="getcustnum">
              <option value="#fk_custNum#">#fk_custNum#</option>
              </cfoutput>
              </SELECT>
              an with the innerHTML i am a bit baffled on how i would go about that. But i am going to do some research on what you have said :).

              But thank you for all the help you have given me :)
              Rach

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Oh I see. So you already have that working. In that case, you only need to make one request onchange. Now what you output in the Coldfusion file depends on how you're going to code the client-side JavaScript part. You could use XML or JSON, or you could just output a string delimited by a special character and use the split() method in JavaScript to split the string into an array to fill the separate fields.

                Comment

                • bonneylake
                  Contributor
                  • Aug 2008
                  • 769

                  #9
                  Hey Acoder,

                  Sorry for the slow response. Well the only thing i am not understanding is the coldfusion side of this. I am pretty sure i could figure out the javascript side if i understood the coldfusion side.

                  What i am confused on is how would i get that information from the database? Like the only stored procedures i have created so far is a insert and a update. An i am use to having my form on one page an click submit an takes me to another page to insert (i am still very new to coldfusion as you can tell).I don't know if this would help but this is my cfquery for inserting the contact which shows the felds i am dealing with

                  Code:
                  <cfquery name="insertcontacts" datasource="CustomerSupport">
                  exec usp_CS_Insertcontacts  '#Form.custnum#','#Form.compname#','#Form.fname#',
                  '#Form.lname#','#Form.address#','#Form.city#','
                  #Form.state#','#Form.zip#','#Form.email#','#Form.priphone#',
                  '#Form.secphone#','#Form.custnotes#'
                  </cfquery>
                  but sorry if this don't make much since, been a long weekend. If you need me to explain anymore just ask an all gladely try to.

                  Thank you very much for all the help,
                  Rach

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    You need a select query and pass in the customer number, e.g. #url.custnum#, so when you make an Ajax request, say getCustDetails. cfm?custnum=123 , the code would output the values.

                    Comment

                    • bonneylake
                      Contributor
                      • Aug 2008
                      • 769

                      #11
                      Hey Acoder,

                      I am trying. but i must admit this is way over my head. I am pretty new to coldfusion an javascript combined an i must admit i am really lost.This is what i have done, which i know it don't work or is right. But this is how much i am understanding.

                      Code:
                      <script type="text/javascript">
                      function add(){
                      <cfquery name="custnum" datasource="CustomerSupport">
                      select * from tbl_CS_contacts
                      where fk_custNum=fk_custnum
                      </cfquery>
                      }
                      </script>
                      <SELECT NAME="options" onclick="javascript:add();">
                      <cfoutput query="getcustnum">
                      <option value="#fk_custNum#">#fk_custNum#</option>
                      </cfoutput>
                      </SELECT>
                      Company Name:<input type="text" name="compname"  size="20" id="compname"/>
                      First Name:<input type="text" name="fname"  size="20" id="fname"/>
                      Last Name:<input type="text" name="lname"size="20" id="lname"/>
                      but wanted to ask if you knew of a website that could explain select query better to me? or even a suggested way i could search for my problem online? because i must admit i am very lost.

                      Thanks again,
                      Rach

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Let's start at the beginning then. You've got something that works, so let's use that instead. Coldfusion can only run while the page is loading, not afterwards (unless you use Ajax to call a separate Coldfusion page). Let's get that working first, then you can worry about the JavaScript later.

                        The "custnum" query is fine except that it should be on a separate page (rather like the PHP file) and you need to use ## signs to refer to the passed value, e.g.
                        [code=cfm]<cfquery name="custnum" datasource="Cus tomerSupport">
                        select * from tbl_CS_contacts
                        where fk_custNum=#url .custnum#
                        </cfquery>[/code]Then use cfoutput to loop over the query to output the values. This file you can call getClient.cfm to replace getClient.php.

                        Comment

                        • bonneylake
                          Contributor
                          • Aug 2008
                          • 769

                          #13
                          Acoder,

                          First off thank you for all the patience you are having with me, really appreciate it :).

                          Ok so i tried to do what you said (not at work today so i can't test it). But here is what i came up with, this is on my getClient.cfm page

                          Code:
                           <cfquery name="custnum" datasource="CustomerSupport">
                          select * from tbl_CS_contacts
                          where fk_custNum=#url.custnum#
                          </cfquery>
                          
                          <cfoutput query = "CustomerSupport">
                          #compname#
                          #fname#
                          #lname#
                          #add1#
                          #city#
                          #state#
                          #zip#
                          #email#
                          #pri_phone#
                          #sec_phone#
                          #notes#
                          </cfoutput>

                          I am not sure if i did this right (if i didn't i am sorry in advance). But i was wondering when i do the cfoutput query inbetween like where i have #fname# is that suppose to be the name of the column in the database or is it suppose to be the name of the field in the form? because when i do a stored procedure an i do an insert i am use to doing #Form.(name i gave the input)#.

                          An also wanted to ask do i need to put fk_custNum in the cfoutput as well? An well not sure if i did the loop part right, use to doing it a very different way. But i looked up what you said an it said this loops through the query. This is the example of the website i found http://livedocs.adobe.com/coldfusion...s/tags-b12.htm

                          But wanted to also mention this one part, probably should of mentioned it earlyer. but i been trying to do it by myself before asking for help.what i am trying to do is create a stored procedure for the contacts table that if the customer number already exists it wont create a record in the table, but if it doesn't exist it will create a record in the table (trying to prevent duplicates). But wanted to ask if what we are doing on will affect this or if i need the stored procedure before this? an also wanted to ask if you knew of an example that does something similar to what i am doing, was thinking about trying if exists, an looked up if exists. but most of the examples show if exists create other wise update an don't need it to update.

                          But thank you in advance for all the help an the patience you are having with me you have no idea how much i appreciate this :) Thank you
                          Rach



                          Originally posted by acoder
                          Let's start at the beginning then. You've got something that works, so let's use that instead. Coldfusion can only run while the page is loading, not afterwards (unless you use Ajax to call a separate Coldfusion page). Let's get that working first, then you can worry about the JavaScript later.

                          The "custnum" query is fine except that it should be on a separate page (rather like the PHP file) and you need to use ## signs to refer to the passed value, e.g.
                          [code=cfm]<cfquery name="custnum" datasource="Cus tomerSupport">
                          select * from tbl_CS_contacts
                          where fk_custNum=#url .custnum#
                          </cfquery>[/code]Then use cfoutput to loop over the query to output the values. This file you can call getClient.cfm to replace getClient.php.

                          Comment

                          • bonneylake
                            Contributor
                            • Aug 2008
                            • 769

                            #14
                            Acoder,

                            First off thank you for all the patience you are having with me, really appreciate it :).

                            Ok so i tried to do what you said (not at work today so i can't test it). But here is what i came up with, this is on my getClient.cfm page

                            Code:
                             <cfquery name="custnum" datasource="CustomerSupport">
                            select * from tbl_CS_contacts
                            where fk_custNum=#url.custnum#
                            </cfquery>
                            
                            <cfoutput query = "CustomerSupport">
                            #compname#
                            #fname#
                            #lname#
                            #add1#
                            #city#
                            #state#
                            #zip#
                            #email#
                            #pri_phone#
                            #sec_phone#
                            #notes#
                            </cfoutput>

                            I am not sure if i did this right (if i didn't i am sorry in advance). But i was wondering when i do the cfoutput query inbetween like where i have #fname# is that suppose to be the name of the column in the database or is it suppose to be the name of the field in the form? because when i do a stored procedure an i do an insert i am use to doing #Form.(name i gave the input)#.

                            An also wanted to ask do i need to put fk_custNum in the cfoutput as well? An well not sure if i did the loop part right, use to doing it a very different way. But i looked up what you said an it said this loops through the query. This is the example of the website i found http://livedocs.adobe.com/coldfusion...s/tags-b12.htm

                            But wanted to also mention this one part, probably should of mentioned it earlyer. but i been trying to do it by myself before asking for help.what i am trying to do is create a stored procedure for the contacts table that if the customer number already exists it wont create a record in the table, but if it doesn't exist it will create a record in the table (trying to prevent duplicates). But wanted to ask if what we are doing on will affect this or if i need the stored procedure before this? an also wanted to ask if you knew of an example that does something similar to what i am doing, was thinking about trying if exists, an looked up if exists. but most of the examples show if exists create other wise update an don't need it to update.

                            But thank you in advance for all the help an the patience you are having with me you have no idea how much i appreciate this :) Thank you
                            Rach



                            Originally posted by acoder
                            Let's start at the beginning then. You've got something that works, so let's use that instead. Coldfusion can only run while the page is loading, not afterwards (unless you use Ajax to call a separate Coldfusion page). Let's get that working first, then you can worry about the JavaScript later.

                            The "custnum" query is fine except that it should be on a separate page (rather like the PHP file) and you need to use ## signs to refer to the passed value, e.g.
                            [code=cfm]<cfquery name="custnum" datasource="Cus tomerSupport">
                            select * from tbl_CS_contacts
                            where fk_custNum=#url .custnum#
                            </cfquery>[/code]Then use cfoutput to loop over the query to output the values. This file you can call getClient.cfm to replace getClient.php.

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Originally posted by bonneylake
                              But i was wondering when i do the cfoutput query inbetween like where i have #fname# is that suppose to be the name of the column in the database or is it suppose to be the name of the field in the form?
                              The name of the column in the table.
                              Originally posted by bonneylake
                              An also wanted to ask do i need to put fk_custNum in the cfoutput as well?
                              The loop looks OK, but will need to be modified for use in the JavaScript. Try this page on its own without Ajax with example input (custnum=***) and see what you get.
                              Originally posted by bonneylake
                              But wanted to also mention this one part...
                              For this question, ask in the relevant database forum. Which db are you using?

                              Comment

                              Working...