Solved

Coldfusion $.ajax delete from data string of values

Posted on 2014-03-08
6
390 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 300 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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