Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Coldfusion $.ajax delete from data string of values

Posted on 2014-03-08
6
Medium Priority
?
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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