简体   繁体   中英

ColdFusion, Setting a POST variable

Im editing my first ColdFusion script .... I have a form which has <input type="hidden" name="name" value="1"> .

On the processing page i want to take that value and set it as a POST variable so i can send it onto another page.

I know how to do it in PHP, like so

$_POST['somename'] = $_POST['name']

How would i do that in CF?

Following the idiom in your php code, you can do something like this:

<cfset form['somename'] = form['name']>

...or, if in cfscript:

form['somename'] = form['name'];

If you're concerned about the existence of the variable, you can precede the assignment with <cfparam> :

<cfparam name="form.name" default=""><!--- assuming blank ok as default --->
<cfset form['somename'] = form['name']>

...or in script:

param name='form.name' default='';
form['somename'] = form['name'];   

Of course you can also wrap the assignment in a conditional:

if( structkeyexists(form,'name') ){
  form.somename = form.name; // dot notation alternative to bracket syntax
}

This all begs the question of what exactly you're trying to achieve with this approach.

The ColdFusion syntax is similar. "Post" variables are available in the system structure FORM, and "Get" variables in the system structure URL. Like in PHP, values can be accessed using associative array notation. You can also use dot notation (for valid field names)

    <cfset otherVariable = FORM["variableName"] >
    <cfset otherVariable = FORM.variableName >

i want to take that value and set it as a POST variable so i can send it onto another page.

I am not quite sure what you mean there. Typically, you do not need to reassign FORM or URL values. You simply reference the variable in your code.

 <cfoutput>
    <a href="someOtherPage.cfm?name=#FORM.variableName#">Go To Other Page</a>
 </cfoutput>

You can try this by checking if the post variable is set and then storing it with scope of FORM.

<cfif isdefined ("FORM.name")>
<cfset FORM.somename="#FORM.name#">
</cfif>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM