Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Coldfusion $.ajax delete from data string of values

Posted on 2014-03-08
6
Medium Priority
?
411 Views
Last Modified: 2014-03-08
I'm submitting two types of fields...

1. Main Categories
2. Sub-Categories

I've created a form that submits the data just fine to the function below. The example data might be...

cat_48=0&rec_174=1&cat_47=0&rec_172=1&rec_173=0

I'm a little stuck on separating the two out with deleting them from two separate tables on my func_data.cfm page. So if it's "cat_..." delete from one table. If it's "rec_..." delete from the other. I was thinking I could treat the data as a list and loop through it but it's not a list

function delete_cats_and_records() {

    myDataString = $("#userData").serialize();

    $.ajax({
        type: "POST",
        url: "func/func_data.cfm",
        data: myDataString,
        cache: false,
        success: function() {
            getData();
        },
        error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
        }
    })
};

Open in new window

0
Comment
Question by:brihol44
  • 3
  • 3
6 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 39915088
Yeah, since the fields have unique names you need to loop through the URL parameters and search for patterns in the name, like this (not tested):

       <cfloop collection="#URL#" item="param">
           <!-- split out field name and id:  "cat" and "48">
           <cfset name = getToken(param, 1, "_")>
           <cfset id = getToken(param, 2, "_")>
           <cfset value = URL[param]>

           <!--- its a category with a valid ID ...--->
           <cfif name eq "cat" and ID gt 0>
                  CATEGORY: do something with #id# and #value# ...

           <cfelseif name eq "rec" and ID gt 0>
                  REC: do something with #id# and #value# ...
           </cfif>

       </cfloop>

But I'm wondering if there's an easier way ... I'm assuming the 48 in "cat_48=0" represents a category id. What does the value ie 0/1 represent?
0
 

Author Comment

by:brihol44
ID: 39915109
thx for the help. 0 = don't delete (default value), 1 = delete.
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 1200 total points
ID: 39915127
(Edit - fixed code typo)

Silly question. If 0 means don't delete/do nothing, why pass those fields at all? It's simpler to only pass id's to be deleted as a list.  Use one for "cat" and one for "rec".  So instead of this:

        cat_48=0&rec_174=1&cat_47=0&rec_172=1&rec_173=0

Pass two lists:
       catIDList=(empty string)&recIDList = 174,172

I don't know what type of fields you're using, but if they're checkboxes it's simple. Just use the same name and set the value to the cat/recID.

         <input type="checkbox" name="catIDList" value="48" ...
         <input type="checkbox" name="catIDList" value="99" ...

         <input type="checkbox" name="recIDList" value="174" ...
         <input type="checkbox" name="recIDList" value="172" ...

On the action page the field will contain just id's that were selected.  Then you can use list functions or pass the id's into a WHERE IN (...) clause if your'e deleting them from a table, ie
      <cfparam name="FORM.catIDList" default="">
      <cfparam name="FORM.recIDList" default="">

      <cfif ListLen(FORM.catIDList)>
            <cfquery ...>
              DELETE FROM Table
             WHERE  categoryID IN ( <cfqueryparam value="#catList#" list="true" ....> )
           </cfquery>
      </cfif>
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:brihol44
ID: 39915157
Ok agx.. that makes sense, cleaner and something I'm more familiar on how to do on the func_data.cfm page. I guess I didn't know how to do that (lists) using jquery with using the

myDataString = $("#userData").serialize(); to grab the values.

function delete_cats_and_records() {

    myDataString = $("#userData").serialize();

    $.ajax({
        type: "POST",
        url: "func/func_data.cfm",
        data: myDataString,
        cache: false,
        success: function() {
            getData();
        },
        error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
        }
    })
};

Open in new window

0
 
LVL 52

Expert Comment

by:_agx_
ID: 39915400
Sorry I had to run out.  Hm.. I'm not sure serialize will work here if it assigns elements unique names. Might have to generate the list yourself using something like this:

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/Q_24225776.html
0
 

Author Closing Comment

by:brihol44
ID: 39915458
thx Agx... I figured out the jquery list thing on my own.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question