0

I have a JS function that creates a variable based on what the user is selecting from drop down list. I need to use that JS variable back on my CF code to be able to use it on operations other than submitting the form.

This is my JS Function

function handleProcedureChange(procedureid,recID)
    {
        procedureid= document.form1.procedure.value;
        recID = document.form1.recID.value;
        state = document.form1.state.value;
        state = state.trim();
        city = document.form1.city.value;
        city = city.trim();

        var url ="../../../../cf_modules/components/customDescriptions.cfc?method=procedureDescription&"; 
        url=url+"ProcedureID="+procedureid+"&recID=" + recID + "&state=" + state + "&city="+city;

        $.get(url, function(procedureResult) {
             procedureResult = procedureResult.replace(/^"+|"+$/g, "");
            $("#procedureDescription").text(procedureResult);
        });


    }

and this is my Form

      <select name="procedure" onChange="handleProcedureChange();">
            <option value="">Select Procedure</option>
            <cfloop query="procedures">
                <option value="#procedureId#">#procedureName#</option> 
            </cfloop>
        </select>   

 <textarea name="procedureDescription" id="procedureDescription" cols="80" rows="6"></textarea><br />

I need to use the url2 variable on this link that controls a couple of server side functions

These are the links sequences

<cfoutput>[<a href="cityEdit.cfm?recID=#recID#&delete1=#recID#&state=#state#&city=#city.getCity()#&procedureid=#procedure#">delete</a>]</cfoutput>

<cfif delete1 neq "">
    Are you sure?
<cfoutput> [ <a href="cityEdit.cfm?recID=#recID#&delete1=#delete1#&delete2=#delete1#&procedureid=#procedure#&state=#state#&city=#city.getCity()#">Yes</a>&nbsp;|&nbsp;<a href="cityEdit.cfm?recID=#recID#">No</a>]</cfoutput>
</cfif>

and these are the functions the above links control:

<cfparam name="delete1" default="">
<cfparam name="delete2" default="">


<cfif delete1 neq "">
            <cfquery name="procFinder" datasource="#ds#">
                select * from psp_customDescriptions where recID = '#delete1#'
                and procedureID = '#procedure#' and city = '#city#' and state='#state#'
            </cfquery>


        </cfif>

        <cfif delete2 neq "">
            <cfquery name="deletepractice" datasource="#ds#">
                delete from psp_customDescriptions where recID = '#delete2#'
                 and procedureID = '#procedure#' and city = '#city#' and state='#state#'
            </cfquery>

            <cflocation url="cityEdit.cfm?recID=#recID#" addtoken="no">
        </cfif> 
3
  • once that link is clicked, they will be available in the URL scope in ColdFuison. Does this answer your question? Or do you want to post that var to CF via AJAX in the background?
    – Henry
    May 22, 2012 at 15:56
  • I will need to post that to CF via AJAX I suppose
    – Geo
    May 22, 2012 at 16:03
  • the easiest way would be using <cfajaxproxy> and send the variable across in JSON via AJAX. If you know jQuery, you can use jQuery as well. Just post that to a .cfm expecting that url2 variable in JSON, or to a remote method.
    – Henry
    May 22, 2012 at 16:23

3 Answers 3

4

What about moving the logic to run when the link is clicked? You could also consider using something like jQuery to get values and set up event handlers. As mentioned, you ought to consider using a post to modify/delete data.

<script>
function handleDeleteLink()
{
    procedureid= document.form1.procedure.value;
    recID = document.form1.recID.value;
    state = document.form1.state.value;
    state = state.trim();
    city = document.form1.city.value;
    city = city.trim();

var url2 ="ProcedureID="+procedureid+"&recID=" + recID + "&state=" + state + "&city="+city;

    document.deleteLink.href="cityEdit.cfm?"+url2;
    }
</script>    

<a id="deleteLink" href="#" onclick="">Delete</a>
2
  • 1
    I was just trying to remember how to do that rather than just a location. May 22, 2012 at 16:03
  • 1
    You'll also want to keep in mind some sort of fallback for if the user doesn't have JavaScript... I know it's a rare occurrence, but code for the worst.
    – Dave Long
    May 22, 2012 at 16:26
1

I'm not sure about your question. On the surface it seems obvious. If someone clicks on the link above.. the one that reads:

cityEdit.cfm?recid=123&delete1=123&procedureid=345....

The variables will be passed to ColdFusion in the "URL" scope. So you will have:

<Cfoutput>
#url.recid#
#url.delete1#
#url.procedureID#
</cfoutput>

Does that answer your question?


If your question is about the JS side then you need a function as a result of your link. Instead of "cityedit.cfm" as your link you would add a listener or a click handler so that when the user clicks on delete it fires yours JS function. Then inside the JS function you would create your URL and do a document.href.location to 'cityEdit.cfm?' + url2;

is that what you meant?

FYI - pro forma you should be doing a POST to update data in your DB - especially "delete". It's a little more failsafe (and conforms to W3C).

6
  • Well my problem is that i cannot pass URL2 from being a JS variable to my CF code. The variables that you are outputting in your example do not really exist as individual but rather as a single string string. I am thinking this over for the last 2 days so I apologize if I sound confused
    – Geo
    May 22, 2012 at 16:05
  • So... are people clicking on your link? And does the link look like the one in your third code sample (cityEdit.cfm?recID=123&delete1=123&procedureid=345&city=SOMECITY&state=SOMESTATE). If so then they should show up as URL variables IF you are issuing a GET request (or even a post request would put them in the URL scope). Using barny's example or my JS example you should be able to pass those exact variables in that way. May 22, 2012 at 16:13
  • well it does look like that BUT procedureid is empty. I am doing all of these because i need to get the procedure value from the form i posted on my question.
    – Geo
    May 22, 2012 at 16:20
  • Yes... your code for getting that value is wrong. To get a value from a select list you need something like: document.form1.procedure.options[document.form1.procedure.selectedIndex].text The value of the selected item is indexed in an array - so you have to extract it. Tons of samples out there - google "get value from select box javascript" and you'll get a bunch of samples to work from. May 22, 2012 at 16:26
  • I will include all of my code because I think I got you and me confused over this :)
    – Geo
    May 22, 2012 at 16:37
0

The only thing that I needed to after all was to add this single line of code into my JS function

$("#deleteProc1").html('<a href="cityEdit.cfm?recID='+recID+'&delete1='+recID+'&delete2='+recID+'&state='+state+'&city='+city+'&procedureid='+procedureid+"\">Delete</a>");

This link helps me to access my CF function and delete the procedures that I have selected from my drop down list.

Thanks to all that commented on my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.