Solved

cfselect problem.

Posted on 2009-05-19
11
367 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question to locate the problem 18 127
System Analysis 5 61
popup load of page and setting up of session 8 42
app server have enough resources... 2 52
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

789 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