Solved

Coldfusion $.ajax delete from data string of values

Posted on 2014-03-08
6
397 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 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
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

632 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