Forum Moderators: open

Message Too Old, No Replies

Posting to a MySQL table with AJAX/ColdFusion

         

RevoltPuppy

6:41 pm on Nov 21, 2007 (gmt 0)

10+ Year Member



I am trying to work with AJAX and ColdFusion, posting to a MySQL database. I want the user to click a button. The search terms are then pulled from the URL (or somewhere else reasonable) and sent to the database.

Here is what I have now. I'm new to AJAX, but I'm starting to understand what most of the code is doing.

The ajax.

<script type="text/javascript"><!--
function ajaxFunction(searchTerm){
xmlHttp=new XMLHttpRequest(); [i]//I shortened this from the full version for brevity on Webmaster World[/i]
}

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
[i]// Get the data from the server's response[/i]
}
}

xmlHttp.open("POST","failedSearchAJAX.cfm",true);
xmlHttp.send(searchTerm);

function sendData(){
var searchTerm = document.getElementById("searchTerm");
ajaxFunction(searchTerm);
}
//--></script>

The HTML

<form id="failedSearch" action="javascript:(sendData());">
<p>Did you find what you were looking for?
<input id="no-btn" name="no-btn" title="No" type="submit" value="No" />
</p>
<input type="hidden" name="searchTerm" value="#url.criteria#" />
</p>
</form>

The ColdFusion (failedSearchAJAX.cfm)

<cfquery name="AddData" datasource="Arbor" dbtype="ODBC">
INSERT INTO Forms_Failed_Searches (searchTerm)
values ('#form.failed-search#')
</cfquery>

I can get as far as ajaxFunction() when I click the button, but after that, I'm lost.

stajer

12:15 am on Nov 22, 2007 (gmt 0)

10+ Year Member



I think you want to change:

xmlHttp.open("POST","failedSearchAJAX.cfm",true);
xmlHttp.send(searchTerm);

to

xmlHttp.open("POST","failedSearchAJAX.cfm?searchterm=" + searchterm,true);
xmlHttp.send(searchTerm);

and
<cfquery name="AddData" datasource="Arbor" dbtype="ODBC">
INSERT INTO Forms_Failed_Searches (searchTerm)
values ('#form.failed-search#')
</cfquery>

to

<cfquery name="AddData" datasource="Arbor" dbtype="ODBC">
INSERT INTO Forms_Failed_Searches (searchTerm)
values ('#searchterm#')
</cfquery>