Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript and Cold fusion

Status
Not open for further replies.

Idee

Programmer
Jul 8, 2003
51
NZ
Hi all

I submit a form and after form is submitted, check for the record in database with the given id and if exist, alerts the user using javascript and only if user confirms .. save the information in database else just go to a different location.

But what is happening- is that it alerts the message but in the background it already saves the values in database. What I want is that only if user confirms the alert message, it should save the form.

Any help is appreciated.

Thanks



 
The problem you are having is because JS is client-side and CF is server-side, so CF has finished processing before the browser renders the page and the JS kicks in.

You could show the alert before they submit the form or you could show the alert as you currently do, but send them to a different page for it to store the details to the database. There is probably an obvious solution I'm missing, but tiredness does that to you. [smile]

Hope this helps

Wullie

Fresh Look - Quality Coldfusion Hosting

The pessimist complains about the wind. The optimist expects it to change. The leader adjusts the sails. - John Maxwell
 
Thanks Wullie

Thats exactly what is happening. But I can not alert before I submit as I have to run a query based on the form values and then based on this query, I have to alert.

Yes, this is right that putting saving process in a seperate file should solve my problem but isn't there any other way - maybe using some wddx. Not sure.. how?

Thanks again
 
you have to add a step.

1)submit original form.

2) do check with CF

3) create new form that submits only if the user confirms

4) write to DB using second form values.

A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Now that i have some time let me elaborate.

what I normaly do is submit the form to its self.

<cfif isdefined("form.submit")>
process form
<cflocation url = "bla.cfm">
</cfif>
display form page


Under normal sircumstances this would work fine. but you want to show an error to the user.

if you do something like this you'll have better results.

<cfif isdefined("form.submit")>
process form.
check for existing ID
<cfquery>
select ...
where ...
</cfquery>
<cfif query.recordCount neq 0 or form.confirm eq "yes">
do all the form stuff write everything to the DB life is good
<cflocation url = "nextPage.cfm">
<cfelse>
<cfset confirmMsg = "This already exists are you sure you want to continue?>
</cfif>
</cfif>

show form with with something like this in the head
<head>
<cfif isdefined("confirmMsg")>
<script language = "javascript">
function confirmSubmit(){
action = confirm('<cfoutput>#confirmMsg#</cfoutput>');
if(action){
document.formName.hiddenConfirmFlag.value = 'Yes';
document.formName.submit();
}
}
</cfif>
</head>
<body <cfif isdefined("confirmMsg")>onload = ""confirmSubmit();"</cfif>>

<form>
<input type = "text" value = "<cfif isdefined("form.thisFieldName")><cfoutput>#form.thisFieldName#</cfoutput><cfelse><cfoutput>#queryName.thisFieldName#</cfoutput></cfif>">
<input type = "hidden" value = "No" name = "hiddenConfirmFlag">

</form>
</body>

the input field i assumed it was an edit page where the fields would be populated by a query. if not and they start blank you don't have to do the else part. You still want to make sure you populate the form if it was already submitted once though, so the first part is required to pass the variables again.


A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
-Douglas Adams (1952-2001)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top