Solved

cfselect problem.

Posted on 2009-05-19
11
362 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
 
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
The purpose of this video is to demonstrate how to make a WordPress Site faster and smaller in size by cleaning up the database. This will be demonstrated using a Windows 8 PC. Plugin WP Optimize will be used. Go to your WordPress login page. T…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…

862 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now