Ask a Jedi: Why isn't my form reloading the whole page?
This blog entry actually covers something I've covered a few times, but as I keep getting questions, I figure it can't hurt to cover it again. Rual asks:
I have a simple question about AJAX, CFDIV and links, I have a little page that uses the CFDIV tag to include a file depending on the selected value in a combobox, currently works great with the BIND attribute, but when the included page in the CFDIV have a cfform and a submit button, the page reloads "inside" the CFDIV and doesnt replace the whole page, how can I make it so it replaces the page? links on the included page in the CFDIV will in fact replace the whole page but the submit buttons of the form, it wont.
Is this a bug?
No, it's a feature! Seriously. When it comes to CFDIV (and other things you can put content in, like the CFWINDOW, etc), Adobe used the following rules:
- For forms that use the FORM tag, the post will replace the entire page.
- For forms that use the CFFORM tag, the post will replace just the contents of the div/window/etc.
- Normal HTML links will replace the entire page.
- Links generated via the AjaxLink function will stay inside the div/window/etc.
So the idea is - you have some control over whether or not you want to stay inside the element.
Comments
Just a beginners question tbh
Peter
Then if I click the submit button again the form is submitted but not in the cfdiv and I lose the rest of my page and end up with just the form taking up the whole page.
I'll see if I can create a demo - it may be a quirk somewhere else in my code that is causing to jump out of the cfdiv.
cfdivtest.cfm:
<cfajaximport tags="cfform,cfdiv">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona...;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>CF DIV form test</title>
</head>
<body>
<h1>Cfdiv test</h1>
<p>This text is above the cfdiv</p>
<cfdiv style="clear: both;" id="divtest" bind="url:cfdivform.cfm" />
<p>This text is below the cfdiv</p>
</body>
</html>
cfdivform.cfm:
<cfif isdefined("url.testtext")>
<p>Submitted</p>
<cfoutput><p>#url.testtext#</p></cfoutput>
</cfif>
<cfform name="testfrm" id="testfrm" action="cfdivformresult.cfm" method="post">
<label for="testtext">Enter Text:</label> <cfinput type="text" name="testtext" id="testtext" size="20" maxlength="255" />
<input type="submit" name="submitbtn" id="submitbtn" value="Submit Test" />
</cfform>
cfdivformresult.cfm:
<cflocation url="cfdivform.cfm?testtext=#form.testtext#" addtoken="false" />
Submit the form then submit the form a second time and you will see that the surrounding text disappears.
Is this a bug or a 'feature' and if so how do we fix it? I would rather not have to remove the cflocation which seems to be the culprit.
Or - don't submit at all. Use ColdFusion.Ajax.submitForm() to do it.
I like the idea of staying in the div, but sometimes you just want out.
form post, ajaxlink() works fine now. but thing is, when i reinit app over this URL http://localhost/index.cfm/reinit/1, the ajaxified area performs INFINITE LOOPS.
do you have face same kind of problem?

