0

I hope this is an easy fix but here is my situation.

The user enters a record number and retrieves back a form with with X number of rows (the data feed is from an external data feed so I cannot control how the data is entered). I'm using jQuery to serialize the form vars and then sending this information to another page via an ajax call. On the action page, I'm looping over the data by the & sign (coldfusion cfloop). The problem that appeared today is that in some cases, one of the form fields may contain the ampersand (&) which breaks the loop on the action page.

Anyone have any suggestions on how to get around this?

Here is my ajax function

 var str = $("#ResourceRequest").serialize();    
 $.ajax({
    type: "POST",
    url: "index.cfm?do=misc.submitRequest&wddxString=" + encodeURIComponent(str),
    data: str,
    contentType: "application/json; charset=utf-8",
    dataType: "html",
    beforeSend: function() {
        //  $('#spinner').css("visibility","visible");
    },
    success: function(data) {
        //  $('#spinner').css("visibility","hidden");
        $('#submitRequest_status').html(data);
        $('#btnSave').html('Submit Request').attr('disabled','');
    },
    error: function() {
        $('#btnSave').html('Submit Request').attr('disabled','');
        //  $('#spinner').css("visibility","hidden");
        alert("We apologize for the inconvenience but\nwe were unable to process your request. [error: 258]");
        //  $('#submitRequest_status').html('');                                
    }

});

And here is my CFLOOP

<cfset var i=0>
<cfloop list="#s#" index=curr_val delimiters="&">
<cfset attr=ListGetAt(curr_val & " ",1,"=")>
<cfset val=ListGetAt(curr_val & " ",2,"=")>

<cfif attr EQ "vpmo">
    <cfset PROJVPMO=ListAppend(PROJVPMO,val)>
<cfelseif attr EQ "projectname">
    <cfset PROJNAM=ListAppend(PROJNAM,val)>
<cfelseif attr EQ "pmt">
    <cfset PROJPMT=ListAppend(PROJPMT,val)>
<cfelseif attr EQ "kickoff">
    <cfset PROJKIC=ListAppend(PROJKIC,val)>
<cfelseif attr EQ "goLive">
    <cfset SYSGOLI=ListAppend(SYSGOLI,val)>
</cfif>

HPWD
  • 2,232
  • 4
  • 31
  • 61
  • I've added a routine to check the form fields for data. I should be able to check that the val() for the & and replace it. `$(this).text().replace('&', 'gocowboys');` isn't working and neither is `$(this).val().replace('&', 'gocowboys');` – HPWD Mar 23 '11 at 19:39
  • I got the above idea from [link](http://stackoverflow.com/questions/2950357/jquery-removing-character-from-string). – HPWD Mar 23 '11 at 19:41
  • `$(this).val().replace(/&/g, 'gocowboys')` did the trick. – HPWD Mar 23 '11 at 20:13
  • UPDATED: `$(this).val().replace(/&/g, '^')` worked better. I needed a single character to avoid truncation in the database. – HPWD Mar 25 '11 at 16:04

2 Answers2

1

As you are doing a POST the data provided in "str" should be parsed by ColdFusion and put in the form scope. Try dumping the form scope and see what you get:

<cfdump var="#form#">
Sam Farmer
  • 4,108
  • 17
  • 20
  • I believe this is more of a javascript issue than a CF parsing issue. I'm using FF and firebug to see what I'm submitting and I can see the & being passed. If I remove the & from the input field, the process works and the cfloops perform as intended. Somehow, I need jQuery to change the & to a distinct character string such as ƒ in the form field BEFORE it gets run through the jQuery serialize function. Once the data is passed into the loop, I can have cf restore the ƒ character back to the &... At least this is my thought process. :) – HPWD Mar 23 '11 at 19:29
0

$(this).val().replace(/&/g, '^') worked. I needed a single character to avoid truncation in the database.

HPWD
  • 2,232
  • 4
  • 31
  • 61