2010-03-13 54 views
1
服務器

我期待張貼下列類型的數據使用jQuery &服務器ColdFusion的:發佈靜態和可變長度的數據與jQuery和ColdFusion的

foodID - int 
foodDESC - text html text from a WYSIWYG (CKEDITOR) 
--there will always be just 1 foodID and foodDESC per POST to the server but there can be a variable number of: 
locationID - int 
LocationDesc - text 
--There can be 0-8+ of these. 

我應該在一個郵寄這個或多個帖子? 如果有一篇文章,最聰明的方法是什麼?我以前從來不需要處理這麼多的數據。

謝謝

+0

對於決定不回答的人來說,問題是否清楚? – AnApprentice 2010-03-13 02:00:43

+1

你如何獲得locationID和LocationDesc?它們是分開的(最多8個)輸入框?他們叫什麼名字? (你是否有你的表單示例代碼?) – 2010-03-13 05:24:22

回答

0

您可以在一個POST中發送它們,只要確保輸入字段名稱是唯一的。

+0

我不認爲這是可能的想法,因爲它包含未知長度:[{「locationID」:「16」,「locationDesc」:「XXXX」},{「locationID 「:」111「,」locationDesc「:」XXXX「},{」locationID「:」12「,」locationDesc「:」XXXX「},{」locationID「:」11「,」locationDesc「:」XXXX「} ] – AnApprentice 2010-03-13 02:47:31

+0

你是在談論JSON的AJAX文章,還是普通的HTML表單文章?即使它是JSON,所有CF關心的是它正在接收一個JSON字符串,並且你可以將DeSerializeJSON()返回給CF結構體。 – Henry 2010-03-13 03:17:08

+0

是的,JSON的AJAX文章 – AnApprentice 2010-03-13 03:17:46

0

我不得不猜測你的表格是什麼樣子:

myForm.cfm頁面(假定的jQuery的1.4.x和自定義JS腳本加載)

<form id="myForm"> 
<input type="hidden" name="foodID" id="foodID" value="" /> 
<textarea name="foodDESC" id="foodDESC"></textarea> 

<input type="text" name="locationID1" value="" class="locID" /> 
<input type="text" name="locationDesc1" value="" class="locDesc" /> 

<input type="text" name="locationID2" value="" class="locID" /> 
<input type="text" name="locationDesc2" value="" class="locDesc" /> 

..more locationIDs and LocationDesc inputs as needed.. 
<input type="button" name="save" id="save" value="Save" /> 
</form> 

myScript.js

// I highly recommend validating javascript with www.jslint.com and testing with firebug 
$("#save").click(function() { 
    var serviceUrl, foodID, foodDESC, locIDs, locDescriptions; 
    serviceUrl = "myProcessor.cfc?method=save"; 
    locIDs = []; 
    locDescriptions = []; 

    // get the value of the two easy ones: 
    foodID = $("#foodID").val(); 
    foodDESC = $("#foodDESC").val(); 

    // Reference: http://marcgrabanski.com/article/jquery-select-list-values 
    // Get all locationIDs based on the similar class name and save them to an array 

    $('.locID').each(function (i, item) { 
     locIDs[i] = $(item).val(); 
    }); 

    // likewise for the location descriptions 
    $('.locDesc').each(function (i, item) { 
     locDescriptions[i] = $(item).val(); 
    }); 

    // send form data to server side form processor 
    $.post(serviceUrl, { 
     foodID: foodID, 
     foodDESC: foodDESC, 
     locationIDs: locIDs, 
     locationDescs: locDescriptions 
    }, function (result) { 

      // display result message 
      alert(result); 
     }, "json"); 

}); 

myProcessor.cfc

<cfcomponent output="no"> 
<cfsetting showdebugoutput="no"> 
<cfset ds="myDatasource"> 

    <cffunction name="save" access="remote" returnFormat="JSON" output="no" hint="Saves data to the server db"> 
      <cfargument name="foodID" type="string" required="yes"> 
      <cfargument name="foodDESC" type="string" required="yes"> 
      <cfargument name="locationIDs" type="string" required="yes"> 
      <cfargument name="locationDescs" type="string" required="yes"> 
      <cfset var result = "Success!"> 

      <cftry> 

      <!--- Process arguments here for your server side needs such as a cfquery 
      foodID will be a simple string or integer 
      foodDESC will be a string varchar or text 
      locationIDs and locationDescs will be a javascript array. Can't remember if Coldfusion will process it as a comma delimited list or an array. Either way, it's simple to handle with CF. If it's an array, you will need to modify the argument type above ---> 

      <cfcatch> 
       <!--- Uh oh, something happened, return the message for debugging ---> 
       <cfset result = cfcatch.message & " " & cfcatch.detail> 
      </cfcatch> 
      </cftry> 
      <cfreturn result> 
    </cffunction> 
</cfcomponent>