Solved

Coldfusion $.ajax delete from data string of values

Posted on 2014-03-08
6
381 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now