简单的coldfusion将记录id传递给JS函数?

发布于 2025-01-08 06:21:44 字数 1310 浏览 0 评论 0原文

我试图通过 cfm 页面传递唯一的数据库会话 ID,但似乎无法让它工作。第 1 页有记录 id,我试图将其保存在 recordclick() 函数的 numSession 中,并将其传递到下一页,我在查询中使用它,但它不起作用。

这是我正在做的事情的粗略概述:

第 1 页:

<CFQUERY datasource = "database1" result = "result">
insert into user
set
blah blah
</cfquery>

<html>
<head>   
<title>page1</title>
<script type="text/javascript">
function recordClick(imageid)
{
        document.getElementById("numSend").value =       document.getElementById("numSend").value + imageid;
        document.getElementById("numSession").value = result.generated_key;
}
</script>
</head>

<body>
<FORM action="page2.cfm" method="post">

<img src="1.png" NAME="num1" onclick="recordClick(1)"
    width="100px"
    height="100px"> 
<div name="num1" id="num1"></div>

<input type="hidden" id="numSend" name="numSend" />
<input type ="hidden" id = "numSession" name ="numSession" />
<input type="submit" value="Done" />
</form>

</body>
</html>

--------------------------------------

<cfoutput>
The ID of the row I just inserted was "#numSession#"
</cfoutput>


<CFQUERY datasource = "database1">
update user
set
pin = "#numSend#"
where id= "#numSession#"
</cfquery>

Im trying to pass the unique database session id through cfm pages but cannot seem to get it working. Page 1 has the record id, im trying to save in numSession in the recordclick() function and pass it in the next page, where I use it in my query, but its not working.

Heres a rough outline of what im doing:

Page 1:

<CFQUERY datasource = "database1" result = "result">
insert into user
set
blah blah
</cfquery>

<html>
<head>   
<title>page1</title>
<script type="text/javascript">
function recordClick(imageid)
{
        document.getElementById("numSend").value =       document.getElementById("numSend").value + imageid;
        document.getElementById("numSession").value = result.generated_key;
}
</script>
</head>

<body>
<FORM action="page2.cfm" method="post">

<img src="1.png" NAME="num1" onclick="recordClick(1)"
    width="100px"
    height="100px"> 
<div name="num1" id="num1"></div>

<input type="hidden" id="numSend" name="numSend" />
<input type ="hidden" id = "numSession" name ="numSession" />
<input type="submit" value="Done" />
</form>

</body>
</html>

--------------------------------------

<cfoutput>
The ID of the row I just inserted was "#numSession#"
</cfoutput>


<CFQUERY datasource = "database1">
update user
set
pin = "#numSend#"
where id= "#numSession#"
</cfquery>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再见 2025-01-15 06:21:44

ColdFusion在服务器上执行,JavaScript在客户端上执行。你的 JS 不知道结果是什么。

您需要使用 ColdFusion 的 ToScript()函数

描述

创建分配值的 JavaScript 或 ActionScript 表达式
ColdFusion 变量转换为 JavaScript 或 ActionScript 变量。
该函数可以转换ColdFusion字符串、数字、数组、
结构,以及对 JavaScript 或 ActionScript 语法的查询
定义等效变量和值。

这是一个来自文档的示例:

<cfset thisString="hello world">
<script type="text/javascript" language="JavaScript">
    <cfoutput>
        var #ToScript(thisString, "jsVar")#;
    </cfoutput>
</script>

您的代码(已更正):

function recordClick(imageid)
{
    var <cfoutput>#ToScript(result.generated_key, "generated_key")#;</cfoutput>
    var objNumSend = document.getElementById("numSend");
    objNumSend.value = objNumSend.value + imageid;
    document.getElementById("numSession").value = generated_key;
}

ColdFusion is executed on the server, JavaScript is executed on the client. Your JS has no idea what result is.

You need to use ColdFusion's ToScript() function.

Description

Creates a JavaScript or ActionScript expression that assigns the value
of a ColdFusion variable to a JavaScript or ActionScript variable.
This function can convert ColdFusion strings, numbers, arrays,
structures, and queries to JavaScript or ActionScript syntax that
defines equivalent variables and values.

Here's an example, taken from the docs:

<cfset thisString="hello world">
<script type="text/javascript" language="JavaScript">
    <cfoutput>
        var #ToScript(thisString, "jsVar")#;
    </cfoutput>
</script>

Your code (corrected):

function recordClick(imageid)
{
    var <cfoutput>#ToScript(result.generated_key, "generated_key")#;</cfoutput>
    var objNumSend = document.getElementById("numSend");
    objNumSend.value = objNumSend.value + imageid;
    document.getElementById("numSession").value = generated_key;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文