Blame view

assets/js/app.js 1.21 KB
57da7fcba   Tarandeep Singh   first commi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  $(function() {
  
  	// Get the form.
  	var form = $('#ajax-contact');
  
  	// Get the messages div.
  	var formMessages = $('#form-messages');
  
  	// Set up an event listener for the contact form.
  	$(form).submit(function(e) {
  		// Stop the browser from submitting the form.
  		e.preventDefault();
  
  		// Serialize the form data.
  		var formData = $(form).serialize();
  
  		// Submit the form using AJAX.
  		$.ajax({
  			type: 'POST',
  			url: $(form).attr('action'),
  			data: formData
  		})
  		.done(function(response) {
  			// Make sure that the formMessages div has the 'success' class.
  			$(formMessages).removeClass('error');
  			$(formMessages).addClass('success');
  
  			// Set the message text.
  			$(formMessages).text(response);
  
  			// Clear the form.
  			$('#name').val('');
  			$('#email').val('');
  			$('#subject').val('');
  			$('#message').val('');
  		})
  		.fail(function(data) {
  			// Make sure that the formMessages div has the 'error' class.
  			$(formMessages).removeClass('success');
  			$(formMessages).addClass('error');
  
  			// Set the message text.
  			if (data.responseText !== '') {
  				$(formMessages).text(data.responseText);
  			} else {
  				$(formMessages).text('Oops! An error occured and your message could not be sent.');
  			}
  		});
  
  	});
  
  });