Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

cfselect problem.

Posted on 2009-05-19
11
Medium Priority
?
374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

 
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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…

722 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