John Kim
John Kim

Reputation: 41

How to use a <form> tag within a coldfusion <cfscript>

Trying to convert a <cfif> statement into it's if script counterpart but it's body contains a <form> tag.

This is located within an onRequestStart method within my application.cfc file.

CF

<cfscript>
    if (!isdefined("session.username")) {
        include "TEMP_Head.cfm";
        </cfscript>
        <form action="index.cfm" method="Post">
                        <input type="submit" name="LogOutUser" value="Log Out">
                </form>
        <cfscript>
        include "TEMP_Foot.cfm";
    }
</cfscript>

I expected this to run but I get the error message:

The start tag must have a matching end tag. An explicit end tag can be provided by adding </cfscript>. If the body of the tag is empty, you can use the shortcut <cfscript .../>.

Upvotes: 2

Views: 583

Answers (3)

Saravana Kumar
Saravana Kumar

Reputation: 178

You can use code like below ( Include another cfm page, within that page write your HTML form code ) It will not give any error.

    <cfscript>
      if (!isdefined("session.username")) {
        include "TEMP_Head.cfm";  //Your Header part
        include "form_include.cfm"; // Your Form
        include "TEMP_Foot.cfm";   //Your Footer part 
      }
   </cfscript>

Within form_include.cfm use your HTML form code.

    <form action="index.cfm" method="Post">
      <input type="submit" name="LogOutUser" value="Log Out">
    </form>

REsult as screenshot

Upvotes: 2

Adrian J. Moreno
Adrian J. Moreno

Reputation: 14859

If you're dead set on this, as @Ageax commented, the form is just a string of HTML. You can try this:

<cfscript>
    if (structKeyExists(session, "username")) {
        var loginForm = '<form action="index.cfm" method="Post">';
        loginForm &= '<input type="submit" name="LogOutUser" value="Log Out">';
        loginForm &= '</form>';

        include "TEMP_Head.cfm";
        writeOutput(loginForm);
        include "TEMP_Foot.cfm";
    }
</cfscript>

Personally, I'd put the form in its own include in order to avoid putting presentation code in Application.cfc.

Also, per James' answer, live in the now! Stop using isDefined() and use structKeyExists() or structname.keyExists() depending on which version of CF you're using.

Upvotes: 4

James A Mohler
James A Mohler

Reputation: 11120

I wouldn't even do cfscript on this on

<cfif NOT isdefined("session.username")>
    <cfinclude template="TEMP_Head.cfm">
    <form action="index.cfm" method="Post">
        <input type="submit" name="LogOutUser" value="Log Out">
    </form>
    <cfinclude template="TEMP_Foot.cfm">
</cfif>

OffTopic

IsDefined() isn't the cleanest way to check for variables. Consider

<cfif NOT session.keyExists("username")>

Upvotes: 3

Related Questions