Does cfquery execute on every page load? I ask because I'm getting a sequence number with the query and then using it in a form. Unfortunatly, the query seems to execute every time the page loads. I don't want that to happen. I also tried putting it inside of a cffunction and then calling it out of the onSubmit parameter of of the cfinput box that uses the sequence number, but it still calls the sequence.
Here are examples of the way I've tried to do this:
<cfquery name="payment_seq_num" datasource="ORCL">
select ratner01.payment_id_seq.nextval as seq from dual
</cfquery>
<cfset paymentid = payment_seq_num.seq>
And
<cffunction name="getVetSeq" output="false">
<cfquery name="vet_seq_num" datasource="ORCL">
select ratner01.vet_id_seq.nextval as seq from dual
</cfquery>
<cfset vet_form.VET_ID = vet_seq_num.seq>
</cffunction>
I get why the first one keeps incrementing...it's in the head and is called everytime. But why would the second one execute every page load?
Here's how I'm calling it:
<cfform action="vet_output.cfm" method="post" format="html" class="cfform" name="vet_form">
<fieldset>
<legend>Add a Veterinarian to the Databse</legend>
<table>
<tr><cfoutput>
<td><cfinput type="hidden" name="VET_ID" onsubmit="#getVetSeq()#"></td></cfoutput>
</tr>
<tr>
<td>Vet First Name:<br/> <cfinput type="text" name="VET_FNAME" maxlength="35"></td>
<td>Vet Last name: <br/><cfinput type="text" name="VET_LNAME" maxlength="50"></td>
</tr>
<td><cfinput type="submit" value="Insert" name="vetSubmit"></td>
</table>
</fieldset>
</cfform>
So I added this into the output page and removed all related code from the input page, thanks to some suggestions, and it worked... :
<cfquery name="vet_seq_num" datasource="ORCL">
select ratner01.vet_id_seq.nextval as seq from dual
</cfquery>
<cfset FORM.VET_ID = vet_seq_num.seq>
<cfinsert name="insert_vet" datasource="ORCL" username="XX" password="XX"
tablename="VET"
formfields="VET_ID, VET_FNAME, VET_LNAME">
Yeah, so every time this page is loaded you will call that function and get a new sequence number. Because everytime you load the page #getVetSeq()# will be executed by ColdFusion.
I know you put it in onSubmit() but onSubmit() is a JavaScript event, which has no knowledge of ColdFusion. By the time JavaScript sees that code the function has already been called. If you look you'll probably see JS errors because when you click submit you are actually calling a non-existant function. Because your code renders as something like:
onsubmit="1234"
If you only want it called when the form is submitted then do it in your output.cfm instead of in your form.
If for some reason you need to do it on this page instead of in your processing page, then you'll need to look at doing it as an Ajax call so that it only executes onSubmit().