Html Coldfusion 表单提交中的数据属性

Html data attribute in Coldfusion form submission

我知道如何从 HTML 表单中提取 JQuery 中的数据属性,但是可以从表单传递到的 .cfc 控制器中完成吗?

Page.cfm

<cfform action="controller.cfc" method="getData">
    <cfinput name="uploadMyData" type="submit" data-primaryID="123">
</cfform>

Controller.cfc

function getData(rc){
    writeDump(rc.uploadMyData.?????(primaryId)????);
}

不确定数据属性是否以任何方式存储在结构中。 我在 rc.uploadMyData 上尝试了 getMetaData,但只得到了 java 函数的列表。

更新

Adrian J. Moreno

Data attributes are just markup that impact the internal data object of the element. They are not part of a form request. You wouldn't submit a form to a CFC file either. Using Ajax, you can read the value from the data attribute and pass it to the server as part of the request's data packet.

所以在Page.cfm

<script type="text/javascript">
    var otherData = $("#myForm").serialize();
    function sendForm(){
        var primaryID = $("#submitData").data("primaryID");
        jQuery.post("urlpath/controller/",'{"id":'+''+primaryID+'}', function(dataReturn, status){
            //I want to get my form and submit the other data as well to the controller
        }            
    }
</script>

<form id="myForm" action="sendForm()">
        <input name="uploadMyData" id="submitData" type="submit" data-primaryID="123">
        <input name="importantThing" value="#importantVariable#" type="text">
</form>

pass it to the server as part of the request's data packet.

我如何将它们一起传递? 我希望此表单提交重定向到另一个页面,所以我看不出我怎么不能使用控制器?表单数据应该进入 newPage.cfc 进行处理并准备好在 newPage.cfm 上输出。我正在使用框架一,但变化不大。

感谢您的帮助,您觉得如何?

更新 2

好的,现在我问自己为什么要同时提交 ajax 和 coldfusion 表单提交的表单?我意识到那不是应该做的。我也可以

  1. 通过 JQuery/Ajax 提交表单,无需重新加载页面,并在 post 中包含数据属性 url 并包含序列化的表单数据

Page.cfm

<script type="text/javascript">
    var otherData = $("#myForm").serialize();
    function sendForm(){
        var primaryID = $("#submitData").data("primaryID");
        jQuery.post("controller/method/?primaryId="+primaryID+"",otherData, function(dataReturn, status){
            //do stuff with dataReturn or do a javascript redirect
        }            
    }
</script>

Controller.cfc

function method(requestContext){
    //do stuff with that otherData, all stored in request context. includes the primaryID value i appended to the url
    //send it back to the Page.cfm
    fw.renderData("rawjson",jsonString,200,"");
}

如果它恢复正常,我可以 运行 使用该信息在我的原始视图中返回一个函数。就像在页面上显示它一样。

  1. 以传统方式提交表单,这会导致页面重新加载。包含一个隐藏的输入类型和我要传递的值。

Page.cfm

<cfform action="controller.method" method="getData">
    <cfinput name="uploadMyData" type="submit" value="Submit Form Button">
    <cfinput name="primaryID" type="hidden" value="123">
</cfform>

Controller.cfc

function method(requestContext){
    //do stuff with requestContext.primaryID
    //go to the view method.cfm or do a fw.redirect("new page or back to the page i came from");
}

为什么我们要提交表单两次?只做其中之一。

Matt,我认为您对 HTML、JavaScript 和 jQuery 的许多基本内容感到困惑,这些内容与 ColdFusion 无关。

让我们从 HTML Form 开始。

<form id="{{unique_dom_value}}" action="{{uri}}" method="{{http_verb}}">

  1. unique_dom_value:文档特有的一些字符串。允许您使用 JavaScript.
  2. 引用整个表单对象
  3. uri:您提交表单进行处理的 URI。
  4. http_verb:默认为 GET,但您应该最常使用 POST.

您尝试 post 的 URI 仅在 controller.cfc 中,但这除了将您重定向到 ColdFusion CFC 检查器之外什么也做不了。

在大多数 MVC 框架的上下文中,index.cfm?event=handler.action 使用底层框架在名为 handler 的 CFC 中调用名为 action 的函数。没有框架,您必须手动执行此操作。因此,如果您有一个名为 controller.cfc 的 CFC,并且想将您的表单数据发送到一个名为 myFunction 的函数,您需要说 action="controller.cfc?method=myFunction.

但这也不对。当您提交表单时,浏览器期望加载在 action 属性中找到的 URI。但是您的 CFC 函数不响应 HTML,而是响应其他内容。也许是一个查询对象,也许是 JSON。这就是为什么要使用 jQuery.Ajax() 将表单数据(包括数据属性值)发送到 CFC 的函数,它应该 return 数据在 JSON 中。收到响应后,您可以在同一页面上继续处理或重定向到另一个页面。

但是,由于您使用的是 Framework One,我会查看有关在您的一个处理程序函数中使用 renderData() 的文档。