contact.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Contact Form
  2. function validateForm() {
  3. var name = document.forms["myForm"]["name"].value;
  4. var email = document.forms["myForm"]["email"].value;
  5. var subject = document.forms["myForm"]["subject"].value;
  6. var comments = document.forms["myForm"]["comments"].value;
  7. document.getElementById("error-msg").style.opacity = 0;
  8. document.getElementById('error-msg').innerHTML = "";
  9. if (name == "" || name == null) {
  10. document.getElementById('error-msg').innerHTML = "<div class='alert alert-warning error_message'>*Please enter a Name*</div>";
  11. fadeIn();
  12. return false;
  13. }
  14. if (email == "" || email == null) {
  15. document.getElementById('error-msg').innerHTML = "<div class='alert alert-warning error_message'>*Please enter a Email*</div>";
  16. fadeIn();
  17. return false;
  18. }
  19. if (subject == "" || subject == null) {
  20. document.getElementById('error-msg').innerHTML = "<div class='alert alert-warning error_message'>*Please enter a Subject*</div>";
  21. fadeIn();
  22. return false;
  23. }
  24. if (comments == "" || comments == null) {
  25. document.getElementById('error-msg').innerHTML = "<div class='alert alert-warning error_message'>*Please enter a Comments*</div>";
  26. fadeIn();
  27. return false;
  28. }
  29. var xhttp = new XMLHttpRequest();
  30. xhttp.onreadystatechange = function () {
  31. if (this.readyState == 4 && this.status == 200) {
  32. document.getElementById("simple-msg").innerHTML = this.responseText;
  33. document.forms["myForm"]["name"].value = "";
  34. document.forms["myForm"]["email"].value = "";
  35. document.forms["myForm"]["subject"].value = "";
  36. document.forms["myForm"]["comments"].value = "";
  37. }
  38. };
  39. xhttp.open("POST", "php/contact.php", true);
  40. xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  41. xhttp.send("name=" + name + "&email=" + email + "&subject=" + subject + "&comments=" + comments);
  42. return false;
  43. }
  44. function fadeIn() {
  45. var fade = document.getElementById("error-msg");
  46. var opacity = 0;
  47. var intervalID = setInterval(function () {
  48. if (opacity < 1) {
  49. opacity = opacity + 0.5
  50. fade.style.opacity = opacity;
  51. } else {
  52. clearInterval(intervalID);
  53. }
  54. }, 200);
  55. }