简体   繁体   English

ColdFusion,设置POST变量

[英]ColdFusion, Setting a POST variable

Im editing my first ColdFusion script .... I have a form which has <input type="hidden" name="name" value="1"> . 我正在编辑我的第一个ColdFusion脚本....我有一个具有<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. 在处理页面上,我想获取该值并将其设置为POST变量,以便可以将其发送到另一个页面。

I know how to do it in PHP, like so 我知道如何用PHP做到这一点

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

How would i do that in CF? 我将如何在CF中做到这一点?

Following the idiom in your php code, you can do something like this: 按照您的php代码中的习惯用法,您可以执行以下操作:

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

...or, if in cfscript: ...或者,如果在cfscript中:

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

If you're concerned about the existence of the variable, you can precede the assignment with <cfparam> : 如果您担心变量的存在,可以在赋值之前加上<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. ColdFusion语法相似。 "Post" variables are available in the system structure FORM, and "Get" variables in the system structure URL. 系统结构FORM中提供了“发布”变量,系统结构URL中提供了“获取”变量。 Like in PHP, values can be accessed using associative array notation. 像在PHP中一样,可以使用关联数组符号访问值。 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. 我想采用该值并将其设置为POST变量,以便可以将其发送到另一个页面。

I am not quite sure what you mean there. 我不太确定你在那里的意思。 Typically, you do not need to reassign FORM or URL values. 通常,您不需要重新分配FORM或URL值。 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. 您可以通过检查是否设置了post变量,然后将其存储在FORM范围内来进行尝试。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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