Solved

Jquery AJAX Form Submission/Coldfusion

Posted on 2011-03-07
6
846 Views
Last Modified: 2012-05-11
I have a question about this example:
http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

I am using coldfusion and jquery to have someone fill out the form, validate the name and email address and then insert it into my database. However, I would also like to check the database to make sure that I don't get duplicate email addresses.  How would I go about doing that?    

I am using the same code as shown on that website.  The only difference is that I don't have a return phone field.  I only have name and email address.  
0
Comment
Question by:trifecta2k
[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 8

Accepted Solution

by:
McNetic earned 500 total points
ID: 35061909
There are at least two ways to accomplish that: First, you could modify the sucess() function to check the result return by the server: The function can be defined as follows:
success(data, textStatus, jqXHR)

Open in new window


Data is the data return by your server, textStatus the status the request returned, and jqXHR is the XMLHttpRequest object used for the request. You can have the server return different values when the email was added and when it already existed, and react accordingly in the sucess() function by checking out the data argument.

The second, and perhaps better way would be to let the server return a http error on duplicate email addresses and change the ajax call to include an error handler (similar to the success handler). Something like this:

$.ajax({  
  type: "POST",  
  url: "bin/process.php",  
  data: dataString,  
  success: function() {  
    $('#contact_form').html("<div id='message'></div>");  
    $('#message').html("<h2>Contact Form Submitted!</h2>")  
    .append("<p>We will be in touch soon.</p>")  
    .hide()  
    .fadeIn(1500, function() {  
      $('#message').append("<img id='checkmark' src='images/check.png' />");  
    },
  error: function() {
    [... your error handling code here ...]
    });  
  }  
});  

Open in new window

0
 
LVL 5

Author Comment

by:trifecta2k
ID: 35062058
I really like the 2nd option...but what do you mean by have the server return a http error on a duplicate email?  

Currently I am trying to use a simple query to tell if the email address exists:
<cfquery name="check" datasource="lbdb_">
Select * from tbl_member
where member_email = '#url.email#'
</cfquery>

<cfif check.recordCount eq 0>
    query to insert into database and a message to the user
</cfif>

What would I have to do to get your 2nd option to work?  

Thanks for the help.  
0
 
LVL 8

Assisted Solution

by:McNetic
McNetic earned 500 total points
ID: 35062558
Uh, I'm sorry, I'm not really into coldfusion. It should, however, be possible to make the server return an http error (like the well known error 404 not found) to the browser. Maybe it works like this:

<cfheader statuscode="400" statustext="Bad Request" />
<cfabort />

Open in new window


There are a lot of error codes defined in http, see here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 5

Author Comment

by:trifecta2k
ID: 35069860
Sorry for the delayed response...my day got cut short yesterday.  

I tried using cfheader and it shows the error when I use firebug, but it doesn't actually fire off the error function I defined.  I've attached a snippet of the code, maybe I'm doing something wrong.

Here is main part of my js file
$.ajax({
      type: "POST",
      url: "addMember.cfm?" + dataString,
      data: dataString,
      success: function(result) {
        $('#contact_form').html(result);
      }
	  error: function() {
		alert('hello'); 
	  }
     });

Open in new window


Here is my coldfusion file

<cfset cDate = dateformat(now(), 'mm/dd/yyyy')>
<cfquery name="check" datasource="lbdb_">
Select * from tbl_member
where member_email = '#url.email#'
</cfquery>

<cfif check.recordCount eq 0>
    <cfquery name="addMember" datasource="lbdb_">
        insert into tbl_member (member_name, member_email, member_date)
        values ('#url.name#', '#url.email#', #createODBCDate(cdate)#)
    </cfquery>
    <div id="message">Thank you for subscribing</div>
<cfelse>
    <cfheader statuscode="400" statustext="Bad Request" />
    <cfabort />
</cfif>

Open in new window

0
 
LVL 8

Assisted Solution

by:McNetic
McNetic earned 500 total points
ID: 35072851
In the js code you posted, there is a comma missing at the end of the line before
error: function() {

Open in new window

0
 
LVL 5

Author Closing Comment

by:trifecta2k
ID: 35073812
Thanks for the help.  I got it to work!
0

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

691 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