Solved

is it possible to pass data to an insert query in a cfc via javascript?

Posted on 2010-08-17
2
323 Views
Last Modified: 2012-05-10
Hi, I have a cfc page containing an insert statement to a db and on my cfm page, I have various javascript functions, one in which is a save() function. In this function, I want to pass data from my form via javascript to the insert query in my cfc. Is this possible? If so, how are there any examples?

I will be attaching a code snippet shortly cause I am currently away from my pc where the code resides. In the meantime, if someone could give me any examples or references that would be appreciated. Thanks.
0
Comment
Question by:grnmachine
2 Comments
 
LVL 15

Accepted Solution

by:
Tomarse111 earned 500 total points
ID: 33456485
You can do this via an AJAX post to a remote function within your CFC.

Using a library such as jQuery will make this a 100 times easier. See below for some example code
/*AJAX call using Jquery*/

$.post("test.cfc?method=functionName", { name: "John", time: "2pm" },
   function(data){
     alert("Data Loaded: " + data);
   });

/*Code in your CFC*/
<cffunction name="functionName" access="remote" output="no">     
<cfquery datasource="yourDatasource">
    INSERT INTO yourTable
    (Name, Time) VALUES (<cfqueryparam value="#FORM.name#" cfsqltype="cf_sql_varchar">,<cfqueryparam value="#FORM.Time#" cfsqltype="cf_sql_varchar">)
</cfquery>
<cfreturn "Inserted correctly">
</cffunction>

Open in new window

0
 

Author Closing Comment

by:grnmachine
ID: 33456826
Thanks, this works well.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

808 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