Solved

cfselect problem.

Posted on 2009-05-19
11
366 Views
Last Modified: 2013-12-20
Hi,
I have written he below code.  When I select the empno from the dropdown list, corresponding empname & salary should be filled in the empname & salary text boxes.

Can someone help me how to fill the code for that functionality.
thanks in advance.
<cfquery name="aaa" datasource="site4">
select * from emp
</cfquery>
 
<cfform id="form1" name="form1" method="post" action="">
 
<cfselect name="select1" query="aaa" value="empno" display="empno">
</cfselect>
 
  <p>
    <label>Emp name
      <cfinput type="text" name="empname" id="empname" />
    </label>
  </p>
  <p>
    <label>Salary
      <input type="text" name="sal" id="sal" />
    </label>
  </p>
  <p>
    <label>
      <input type="submit" name="submit" id="submit" value="Submit" />
    </label>
  </p>
</cfform>

Open in new window

0
Comment
Question by:Sri10
  • 5
  • 5
11 Comments
 
LVL 19

Expert Comment

by:erikTsomik
ID: 24428613
0
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24428791
here's how you can do it using javascript. use this option if the total number of employees returned by your query is not too large.

you have not posted the column names from your query, so i assumed that:
a) employee name is in EMPNAME column
b) employee salary is in EMPSALARY column

change these in the javascript code below (where marked by comment) to the actual column names returned by your aaa query!

1) add this javascript AFTER your aaa query:

<script type="text/javascript">
var arrEmps = new Array();
<cfoutput query="aaa">
arrEmps[#aaa.currentrow-1#] = new Emp(#aaa.empno#, '#aaa.ampname#', '#aaa.empsalary#'); // change all aaa.XXXXX to actual column names from your aaa query here
</cfoutput>

var Emp = function(empno, empname, empsalary) {
  this.empno = empno;
  this.empname = empname;
  this.empsalary = empsalary;
};

var onChangeEmp = function(empno) {
  for (var i=0; i<arrEmp.length; i++) {
    if (arrEmp[i].empno == empno) {
      document.getElementById('empname').value = arrEmp[i].empname;
      document.getElementById('sal').value = arrEmp[i].empsalary;
    }
  }
};
</script>

2) add an onChange event to your cfselect to call onChangeemp() js function, passing it currently selected empno:
<cfselect name="select1" query="aaa" value="empno" display="empno" onChange="onChangeEmp(this.value);"></cfselect>

that's it!

Azadi
0
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24428794
PS: post the names of columns returned by your AAA query and i will show you how to do it using a CFC and binding your <cfinput> elements to it.

Azadi
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 1

Author Comment

by:Sri10
ID: 24433921
Hi Azadi,
I have tried the code, but it did not work.

the column names of the table EMP are: empno, empname, salary.

Below is the new code as you said.. But the onchange event does not fie.

<cfquery name="aaa" datasource="site4">
select * from emp
</cfquery>
 
<script type="text/javascript">
var arrEmps = new Array();
<cfoutput query="aaa">
arrEmps[#aaa.currentrow-1#] = new Emp(#aaa.empno#, '#aaa.empname#', '#aaa.salary#');
</cfoutput>

var Emp = function(empno, empname, salary) {
  this.empno = empno;
  this.empname = empname;
  this.salary = salary;
};

var onChangeEmp = function(empno) {
  for (var i=0; i<arrEmp.length; i++) {
    if (arrEmp[i].empno == empno) {
      document.getElementById('empname').value = arrEmp[i].empname;
      document.getElementById('sal').value = arrEmp[i].salary;
    }
  }
};
</script>
 
 
<cfform id="form1" name="form1" method="post" action="cfselect_dropdown.cfm">

<cfselect name="select1" query="aaa" value="empno" display="empno" onChange="onChangeEmp(this.value)">
</cfselect>
 
  <p>
    <label>Emp name
      <cfinput type="text" name="empname" id="empname" />
    </label>
  </p>
  <p>
    <label>Salary
      <input type="text" name="sal" id="sal" />
    </label>
  </p>
  <p>
    <label>
      <input type="submit" name="submit" id="submit" value="Submit" />
    </label>
  </p>
</cfform>

0
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24437573
oops, sorry, some typos in my code... i have created arrEmps array, but then kept calling it as arrEmp in the code...

working code attached.

Azadi
<cfquery name="aaa" datasource="site4">
select * from emp
</cfquery>
 
<script type="text/javascript">
var arrEmps = new Array();
<cfoutput query="aaa">
arrEmps[#aaa.currentrow-1#] = new Emp(#aaa.empno#, '#aaa.empname#', '#aaa.salary#');
</cfoutput>
 
function Emp(empno, empname, salary) {
  this.empno = empno;
  this.empname = empname;
  this.salary = salary;
}
 
function onChangeEmp(empno) {
  for (var i=0; i<arrEmps.length; i++) {
    if (arrEmps[i].empno == empno) {
      document.getElementById('empname').value = arrEmps[i].empname;
      document.getElementById('sal').value = arrEmps[i].salary;
    }
  }
}
</script>
 
 
<cfform id="form1" name="form1" method="post" action="cfselect_dropdown.cfm">
 
<cfselect name="select1" query="aaa" value="empno" display="empno" onChange="onChangeEmp(this.value)">
</cfselect>
 
  <p>
    <label>Emp name
      <cfinput type="text" name="empname" id="empname" />
    </label>
  </p>
  <p>
    <label>Salary
      <input type="text" name="sal" id="sal" />
    </label>
  </p>
  <p>
    <label>
      <input type="submit" name="submit" id="submit" value="Submit" />
    </label>
  </p>
</cfform>

Open in new window

0
 
LVL 1

Author Comment

by:Sri10
ID: 24437917
Hi Azadi,
Thank you very much. It solved my problem. However, if possible can you also send me the code for doing the same thing through binding & cfc stuff as you said.
0
 
LVL 27

Accepted Solution

by:
azadisaryev earned 500 total points
ID: 24438010
something like this:

<cfcomponent output="no">
  <cffunction name="getEmpDetails" access="remote" output="no" returntype="any">
    <cfargument name="empno" type="numeric" requires="yes">
    <cfargument name="detail" type="string" required="no" default="empname">
    <cfset var qEmpDetails = "">
    <cfquery name="qEmpDetails" datasource="site4">
    SELECT empname, salary
    FROM emp
    WHERE empno = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.empno#">
    </cfquery>
    <cfif arguments.detail is 'salary'>
      <cfreturn qEmpDetails.salary />
    <cfelse>
      <cfreturn qEmpDetails.empname />
    </cfif>
  </cffunction>
</cfcomponent>

assuming this cfc filename is emp.cfc and it is in the same folder as the calling page, your form fields will be:

<cfselect name="select1" query="aaa" value="empno" display="empno"></cfselect>

<cfinput type="text" name="empname" id="empname" bind="cfc:emp.getEmpDetails({select1@change})" bindonload="false" />

<cfinput type="text" name="sal" id="sal" bind="cfc:emp.getEmpDetails({select1@change}, 'salary')" bindonload="false" />

Azadi
0
 
LVL 1

Author Comment

by:Sri10
ID: 24438037
Thanks Azadi. I appriciate your help.

I am assigning the points, but can you please look into this below code.

It is the same code, that you have given before(javascript one..). How ever, I have added one update button to it, and when used view the details of particular empno, if he change the name or salary fields and click on update button , it should be updated int he table. But it is not working with the below code.
can you please tell me where i am wrong?


<!--- only this below block I have added...--->
<cfif isdefined("form.submit")>
<cfquery name="update_table" datasource="site4">
update emp set
    empname = '#form.empname#', salary = '#form.salary#'
    where empno = #form.empno#
</cfquery>
</cfif>
<!--- block ends--->

<cfquery name="aaa" datasource="site4">
select * from emp
</cfquery>
 
<script type="text/javascript">
var arrEmps = new Array();

<cfoutput query="aaa">
arrEmps[#aaa.currentrow-1#] = new Emp(#aaa.empno#, '#aaa.empname#', '#aaa.salary#');
</cfoutput>
 
function Emp(empno, empname, salary) {
  this.empno = empno;
  this.empname = empname;
  this.salary = salary;
}
 
function onChangeEmp(empno) {
  for (var i=0; i<arrEmps.length; i++) {
    if (arrEmps[i].empno == empno) {
      document.getElementById('empname').value = arrEmps[i].empname;
      document.getElementById('salary').value = arrEmps[i].salary;

    }
  }
}
</script>


 
<cfform id="form1" name="form1" method="post" action="cfselect_dropdown.cfm">

<cfselect name="empno" query="aaa" value="empno" display="empno" onChange="onChangeEmp(this.value);">
</cfselect>
 
  <p>
    <label>Emp name
      <cfinput type="text" name="empname" id="empname" />
    </label>
  </p>
  <p>
    <label>Salary
      <input type="text" name="salary" id="salary" />
    </label>
  </p>
  <p>
    <label>
      <input type="submit" name="submit" id="submit" value="update" />
    </label>
  </p>
</cfform>
0
 
LVL 1

Author Closing Comment

by:Sri10
ID: 31583355
Thank you.. very clear  solution.
0
 
LVL 27

Expert Comment

by:azadisaryev
ID: 24438095
what exactly is not working? does cf throw any errors? what do they say?

check the datatype of SALARY column in the emp table - is it a text-based type (i.e. text/varchar) or a numeric type (int/number/smallint/float/decimal/etc)?

if it is numeric type, then DO NOT enclose the #form.sal# in single quotes in your update_table query.

[and you should really use <cfqueryparam> for the form values in that query...]

Azadi
0
 
LVL 1

Author Comment

by:Sri10
ID: 24443388
Thanks Azadi... Yeah, the problem is with salary is INT. It worked after removing quote.
thank you very much
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question