Guest User

Untitled

a guest
Jun 8th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. <form method="post" action="script.php" id="test" novalidate>
  2. <div class="detail">
  3. <label>name:</label>
  4. <input type="text" name="user_name" data-validation="name" />
  5. </div><!--detail-->
  6. <div class="detail">
  7. <label>Email:</label>
  8. <input type="email" name="user_email" data-validation="email"/>
  9. </div><!--detail-->
  10. <div class="detail">
  11. <label>phone</label>
  12. <input type="number" name="user_phone" data-validation="phone" />
  13. </div><!--detail-->
  14. <div class="detail">
  15. <label></label>
  16. <input type="text" name="user_enquiry" data-validation="message" />
  17. </div><!--detail-->
  18. <div class="detail message">
  19. <label>Message:</label>
  20. <textarea name="user_message" cols="30" rows="15" data-validation="message"></textarea>
  21. </div><!--detail-->
  22. <p><input type="submit" name="send" id="send" value="Send" onclick="formSubmit();" /></p>
  23. <div class="success_msg">
  24. <p>Form submitted Successfully</p>
  25. </div>
  26. </form>
  27.  
  28. function formSubmit(){
  29. var Validator = function(formObject) {
  30. this.form = $(formObject);
  31. var Elements = {
  32. name: {
  33. reg: /^[a-zA-Z]{2,20}$/,
  34. require : true,
  35. error: "Not a valid name.",
  36. },
  37.  
  38. email: {
  39. reg: /^[a-z-0-9_+.-]+@([a-z0-9-]+.)+[a-z0-9]{2,7}$/i,
  40. error: "Not a valid e-mail address.",
  41. },
  42. phone: {
  43. reg: /^(?([0-9]{3}))?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/,
  44. error: "Not a valid number.",
  45. },
  46.  
  47. message: {
  48. reg: /^(?!s*$).+/,
  49. error: "Message field cannot be empty.",
  50. },
  51. gender: {
  52. error: "gender is required",
  53. },
  54. selectOption: {
  55. error: "this field is required",
  56. required: true
  57. }
  58. };
  59.  
  60. var handleError = function(element, message) {
  61. element.addClass('input-error');
  62. var $err_msg = element.parent('div');
  63. $err_msg.find('.error').remove();
  64.  
  65. var error = $('<div class="error"></div>').text(message);
  66. error.appendTo($err_msg);
  67. console.log(element);
  68.  
  69.  
  70. element.on('keypress change', function() {
  71. $(error).fadeOut(1000, function() {
  72. console.log(element);
  73. element.removeClass('input-error');
  74. });
  75. });
  76.  
  77. };
  78.  
  79. /* Select Option */
  80.  
  81. this.validate = function() {
  82. var errorCount = 0;
  83.  
  84. this.form.find("select").each(function(index, field){
  85. var type = $(field).data("validation");
  86. var validation = Elements[type];
  87. if($(field).val() == "") {
  88. errorCount++;
  89. handleError($(field), validation.error);
  90. }
  91. });
  92.  
  93. this.form.find("input, textarea").each(function(index, field){
  94. var type = $(field).data("validation");
  95. var validation = Elements[type];
  96. if(validation !== undefined) {
  97. var re = new RegExp(validation.reg);
  98. if (validation){
  99. if (!re.test($(field).val())){
  100. errorCount++;
  101. handleError($(field), validation.error);
  102. }
  103. }
  104. }
  105. })
  106.  
  107. /* Radio button */
  108.  
  109. var radioList = $('input:radio');
  110. var radioNameList = new Array();
  111. var radioUniqueNameList = new Array();
  112. var notCompleted = 0;
  113. for(var i=0; i< radioList.length; i++){
  114. radioNameList.push(radioList[i].name);
  115. }
  116. radioUniqueNameList = jQuery.unique( radioNameList );
  117. console.log(radioUniqueNameList);
  118. for(var i=0; i< radioUniqueNameList.length; i++){
  119. var field = $('#' + radioUniqueNameList[i]);
  120. var type = field.data("validation");
  121. var validation = Elements[type];
  122. if($('input[name='+type+']:checked', '#test').val() == undefined) {
  123. errorCount++;
  124. handleError($(field), validation.error);
  125. }
  126. }
  127.  
  128. return errorCount == 0;
  129. };
  130. };
  131.  
  132. /* Submit form*/
  133.  
  134. $(function(){
  135.  
  136. $('form#test').on('submit', function (e) {
  137. var NoErrors = new Validator(this).validate();
  138. if(NoErrors == true) {
  139. $.ajax({
  140. url: this.action,
  141. type: this.method,
  142. data: $(this).serialize(),
  143. success: function() {
  144. // AJAX request finished, handle the results and error msg
  145. $('.success_msg').fadeIn().delay(3000).fadeOut();
  146. //$('input[type=text], input[type=number], input[type=email], textarea').val('').removeClass('error');
  147. $('input[type!="submit"], textarea').val('').removeClass('error');
  148. //this.reset();
  149. }
  150. });
  151.  
  152. }
  153. return false;
  154. })
  155.  
  156. })
  157. }//formSubmit
  158.  
  159. <?php
  160.  
  161. include "classes/class.phpmailer.php";
  162.  
  163. $mail = new PHPMailer; // Passing `true` enables exceptions
  164. $mail->IsSMTP(); // Set mailer to use SMTP
  165. $mail->SMTPDebug = 1; // Enable verbose debug output
  166. $mail->SMTPAuth = true; // Enable SMTP authentication
  167. $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
  168.  
  169. $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
  170. $mail->Port = 587; // TCP port to connect to
  171. $mail->IsHTML(true); // Set email format to HTML
  172. $mail->Username = '***********'; // SMTP username
  173. $mail->Password = '**********'; // SMTP password
  174. $mail->SetFrom('**************');
  175.  
  176.  
  177. $msg = 'Name : ' . $_POST["user_name"] . '<br> Email : ' . $_POST["user_email"] . '<br> Phone : ' . $_POST["user_phone"] . '<br> Enquiry : ' . $_POST["user_enquiry"] . '<br> Message : ' . $_POST["user_message"];
  178. $mail->Subject = "subject here";
  179.  
  180.  
  181. $mail->Body = $msg;
  182. $mail->AddAddress($_POST["user_email"]);
  183.  
  184. if(!$mail->Send()){
  185. echo 'Mailer Error: ' . $mail->ErrorInfo;
  186. }else{
  187. echo 'Message has been sent with using SMTP.';
  188. }
  189.  
  190. ?>
Add Comment
Please, Sign In to add comment