Advertisement
princesse

contact form

Jul 24th, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 12.32 KB | None | 0 0
  1. hey there,
  2.  
  3. I 'm trying to have a proper contact form that will validate my form "on the fly" using jquery & bootstrap validator and at the end will send an email to my mail adress.
  4.  
  5. 1 HTML
  6.  
  7.   <p><b>  Specific projects* </b></p>
  8.              <div  id="alert" class="alert alert-success alert-dismissible" style="display:none" role="alert">
  9.  <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
  10.   <strong>Thank you for contacting me!</strong>I have received your enquiry and will respond to you within 24 hours.  For urgent enquiries please call me or leave a message on skype. :)
  11. </div>
  12.  
  13.  
  14.  
  15.                     <form id="contactForm" method="post" class="form-horizontal" action="_/components/php/contactformSend.php" method="POST">
  16.                         <div class="form-group">
  17.                             <label class="col-lg-3 control-label">Full name</label>
  18.                             <div class="col-lg-3">
  19.                                 <input type="text" class="form-control" name="firstName" placeholder="FirstName" />
  20.                             </div>
  21.                             <div class="col-lg-3">
  22.                                 <input type="text" class="form-control" name="lastName" placeholder="LastName" />
  23.                             </div>
  24.                         </div>
  25.                      
  26.                         <div class="form-group">
  27.                             <label class="col-lg-3 control-label">Email address</label>
  28.                             <div class="col-lg-5">
  29.                                 <input type="text" class="form-control" name="email" />
  30.                             </div>
  31.                         </div>
  32.                        
  33.                       <div class="form-group">
  34.                                 <label class="col-lg-3 control-label">Website</label>
  35.                                 <div class="col-lg-5">
  36.                                     <input type="text" class="form-control" name="website" placeholder="http://" />
  37.                                 </div>
  38.                             </div>
  39.  
  40.                        
  41.  
  42.                      
  43.                         <div class="form-group">
  44.                             <label class="col-lg-3 control-label">What is this regarding?</label>
  45.                             <div class="col-lg-5">
  46.                                 <div class="checkbox">
  47.                                     <label>
  48.                                         <input type="checkbox" name="regarding[]" value="Web Development" /> Web Development
  49.                                     </label>
  50.                                 </div>
  51.                                 <div class="checkbox">
  52.                                     <label>
  53.                                         <input type="checkbox" name="regarding[]" value="SEO" /> SEO
  54.                                     </label>
  55.                                 </div>
  56.                                 <div class="checkbox">
  57.                                     <label>
  58.                                         <input type="checkbox" name="regarding[]" value="Adwords" /> Adwords
  59.                                     </label>
  60.                                 </div>
  61.                                 </div>
  62.                                 </div>
  63.                                
  64.                               <div class="form-group">
  65.                                 <label class="col-lg-3 control-label">Budget</label>
  66.                                 <div class="col-lg-5">
  67.                                     <select  class="form-control" name="budget">
  68.                                       <option value="Less than 200">Less than 200&#36;</option>
  69.                                       <option value="200-350">200&#36;-350&#36;</option>
  70.                                       <option value="350-500">300&#36;-500&#36;</option>
  71.                                       <option value="500-1000">500&#36;-1000&#36;</option>
  72.                                       <option value="1000-1500">1000&#36;-1500&#36;</option>
  73.                                     </select>
  74.                                
  75.                                 </div>
  76.                               </div>
  77.                                
  78.                                <div class="form-group">
  79.                                 <label class="col-lg-3 control-label">TimeFrame</label>
  80.                                 <div class="col-lg-5">
  81.                                     <select class="form-control" name="timeframe">
  82.                                         <option value="">Select the timeframe </option>
  83.                                         <option value="one week">1 week</option>
  84.                                         <option value="two weeks">2 weeks </option>
  85.                                         <option value="tree weeks or more">3 weeks or more </option>
  86.                                        
  87.                                     </select>
  88.                                 </div>
  89.                             </div>
  90.                              <div class="form-group">
  91.                                 <label class="col-lg-3 control-label">Brief project description</label>
  92.                                 <div class="col-lg-5">
  93.                                     <textarea rows="5" class="form-control" name="description"></textarea>
  94.                                 </div>
  95.                             </div>                          
  96.                                                    
  97.                                                    
  98.                            
  99.                            
  100.  
  101.                         <div class="form-group">
  102.                             <label class="col-lg-3 control-label" id="captchaOperation"></label>
  103.                             <div class="col-lg-2">
  104.                                 <input type="text" class="form-control" name="captcha" />
  105.                             </div>
  106.                         </div>
  107.  
  108.                         <div class="form-group">
  109.                        
  110.                         <button id="trimite" type="submit" class="col-lg-3 col-lg-offset-8 col-md-2 col-md-offset-8 btn btn-default btn-sm" name="signup" value="Sign up"> <b>SEND</b> </button>
  111.                             <div class="col-lg-9 col-lg-offset-3">
  112. <!--                                <button type="submit" class="btn btn-primary" name="signup" value="Sign up">Sign up</button>                                 <button type="button" class="btn btn-info" id="validateBtn">Manual validate</button>-->
  113.                                 <button type="button" class="btn btn-link" id="resetBtn">Reset form</button>
  114.                             </div>
  115.                         </div>
  116.                     </form>
  117.          
  118.  
  119. <script type="text/javascript">
  120. $(document).ready(function() {
  121.     // Generate a simple captcha
  122.     function randomNumber(min, max) {
  123.         return Math.floor(Math.random() * (max - min + 1) + min);
  124.     };
  125.     $('#captchaOperation').html([randomNumber(1, 100), '+', randomNumber(1, 200), '='].join(' '));
  126.  
  127.     $('#contactForm').bootstrapValidator({
  128. //        live: 'disabled',
  129.         message: 'This value is not valid',
  130.         feedbackIcons: {
  131.             valid: 'glyphicon glyphicon-ok',
  132.             invalid: 'glyphicon glyphicon-remove',
  133.             validating: 'glyphicon glyphicon-refresh'
  134.         },
  135.         fields: {
  136.             firstName: {
  137.                 validators: {
  138.                     notEmpty: {
  139.                         message: 'The first name is required and cannot be empty'
  140.                     }
  141.                 }
  142.             },
  143.             lastName: {
  144.                 validators: {
  145.                     notEmpty: {
  146.                         message: 'The last name is required and cannot be empty'
  147.                     }
  148.                 }
  149.             },
  150.  
  151.             email: {
  152.                 validators: {
  153.                     emailAddress: {
  154.                         message: 'The input is not a valid email address'
  155.                     }
  156.                 }
  157.             },
  158.              website: {
  159.                 validators: {
  160.                     uri: {
  161.                         message: 'The input is not a valid URL'
  162.                     }
  163.                 }
  164.             },
  165.            
  166.             'regarding[]': {
  167.                 validators: {
  168.                     notEmpty: {
  169.                         message: 'Please specific what is this regarding'
  170.                     }
  171.                 }
  172.             },
  173.            budget: {
  174.                 validators: {
  175.                     notEmpty: {
  176.                         message: 'Please select the estimated time'
  177.                     }
  178.                 }
  179.             },
  180.            
  181.              timeframe: {
  182.                 validators: {
  183.                     notEmpty: {
  184.                         message: 'Please select the estimated time'
  185.                     }
  186.                 }
  187.             },
  188.            
  189.             description: {
  190.                 trigger: 'blur',
  191.                 validators: {
  192.                     notEmpty: {
  193.                         message: 'The brief description of the future project is required'
  194.                     }
  195.                 }
  196.             },
  197.  
  198.             captcha: {
  199.                 validators: {
  200.                     callback: {
  201.                         message: 'Wrong answer',
  202.                         callback: function(value, validator) {
  203.                             var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]);
  204.                             return value == sum;
  205.                         }
  206.                     }
  207.                 }
  208.             }
  209.         }
  210.     });
  211.  
  212.     // Validate the form manually
  213.    // $('#validateBtn').click(function() {
  214.    //     $('#contactForm').bootstrapValidator('validate');
  215.    // });
  216.  
  217.     $('#resetBtn').click(function() {
  218.         $('#contactForm').data('bootstrapValidator').resetForm(true);
  219.     });
  220. });
  221. </script>
  222. <script type="text/javascript" src="_/js/ajaxscript.js"></script>
  223. </div>
  224.  
  225.  
  226. 2 ajaxscript.js
  227.  
  228. url = '_/components/php/contactformSend.php';
  229.  
  230. function collectData(){
  231.  
  232.     firstName = $("input[name='firstName']").val();
  233.     lastName = $("input[name='lastName']").val();
  234.     email = $("input[name='email']").val();
  235.     website = $("input[name='website']").val();
  236.     budget  = $("input[name='budget']").val();
  237.     timeframe  = $("select[name='timeframe']").val();
  238.     description = $("textarea[name='description']").val();
  239.    
  240.     var valcbx = [];
  241.     $("input[name='regarding[]']:checkbox:checked").each(function(i){
  242.       valcbx[i] = $(this).val();
  243.     });
  244.     regarding = valcbx;
  245.    
  246.     //create object
  247.     return obj =
  248.     {'firstName' : firstName,
  249.     'lastName' : lastName,
  250.     'email' : email,
  251.     'website' : website,
  252.     'budget' : budget,
  253.     'timeframe' : timeframe,
  254.     'description' : description,
  255.     'regarding' : regarding};
  256. }
  257.  
  258.  
  259. function errorFunction(){
  260. }
  261.  
  262. function alwaysFunction(){
  263. }
  264.  
  265. function successFunction(){
  266.     $('#alert').css('display', 'block');
  267. }
  268.  
  269. function makeAjaxRequest(cData){
  270.     var promise = $.ajax({
  271.     type: "POST",
  272.     url: url,
  273.     data: cData,
  274.     dataType: 'json',
  275.     async : true
  276.     });
  277.        
  278.     promise.done(successFunction);
  279.     promise.fail(errorFunction);
  280.     promise.always(alwaysFunction);
  281. }
  282.  
  283. function makeRequest(){
  284.     collectedData = collectData();
  285.     data = JSON.stringify(collectedData);
  286.     makeAjaxRequest(data);
  287.    
  288. }
  289.  
  290. $(document).ready(function(){
  291.     $('#trimite').on('click', function(){
  292.         //alert("hhh");
  293.         makeRequest();
  294.         return false;
  295.      //   return true;
  296.  
  297.     })
  298. });
  299.  
  300.  
  301.  
  302. 3  contacformSend.php
  303.  
  304. <?php header('Content-Type: application/json'); ?>
  305. <?php
  306.                  //$rest_json = json_decode(file_get_contents("php://input"), false);
  307. //echo $rest_json;
  308. //$received = json_decode($rest_json);
  309. //print_r($received);
  310.                 //print_r($_POST);
  311. //mail('elizadascal@gmail.com','te-a cautat', print_r($received, true));
  312.  
  313. //mail('elizadascal@gmail.com','te-a cautat', print_r($_POST, true));
  314.  
  315.  
  316. $data = json_decode(file_get_contents("php://input"), false);
  317. //$received = print_r($data);
  318.  
  319.        //json_decode(stripslashes($_POST['data']));
  320.  
  321. $to = "elizadascal@gmail.com";
  322. $header = "Content-Type: text/html\r\nReply-To";
  323. $subject = "This is my Subject Line";
  324.  
  325.  
  326. $body ="<strong>The data is:</strong>".print_r($_POST, true) ;
  327.  
  328.  
  329. if(@mail($to, $subject, $body, $header)) {
  330.     die("true");    
  331.     } else {
  332.         die("There was an error sending the email.");  
  333.     }
  334.    
  335.    
  336.  
  337. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement