cfselect problem.

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

LVL 1
Sri10Asked:
Who is Participating?
 
azadisaryevConnect With a Mentor Commented:
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
 
erikTsomikSystem Architect, CF programmer Commented:
0
 
azadisaryevCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
azadisaryevCommented:
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
 
Sri10Author Commented:
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
 
azadisaryevCommented:
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
 
Sri10Author Commented:
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
 
Sri10Author Commented:
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
 
Sri10Author Commented:
Thank you.. very clear  solution.
0
 
azadisaryevCommented:
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
 
Sri10Author Commented:
Thanks Azadi... Yeah, the problem is with salary is INT. It worked after removing quote.
thank you very much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.