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>

4
  • 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'); Commented Mar 23, 2011 at 19:39
  • I got the above idea from link. Commented Mar 23, 2011 at 19:41
  • $(this).val().replace(/&/g, 'gocowboys') did the trick. Commented Mar 23, 2011 at 20:13
  • UPDATED: $(this).val().replace(/&/g, '^') worked better. I needed a single character to avoid truncation in the database. Commented Mar 25, 2011 at 16:04

2 Answers 2

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#">
Sign up to request clarification or add additional context in comments.

1 Comment

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. :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.