Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $("#contactform").submit( function (e) {
- e.returnValue=false;
- e.preventDefault();
- alert('form submitted');
- if (checkValidation()) {
- //if valid, send ajax
- var name=$('#form-name').val();
- var email=$('#form-email').val();
- var contact_number=$('#form-contact-number').val();
- var message=$('#form-message').val();
- alert(name+email+contact_number+message);
- $.ajax({
- type: 'POST',
- url: 'mail.php',
- //data: JSON.stringify(parameters),
- contentType: "json",
- // data: $(this).serialize(),
- data:{'name':name,'email':email,'contact_number':contact_number,'message':message},
- dataType: "text",
- cache: false,
- success: function(data) {
- // do something with ajax data
- $('.form-response').css({color:'black',backgroundColor:'white',textAlign:'center'}).text('Thank you, we will contact you shortly.').show();
- $('input').val('').trigger('blur');
- $('textarea').val('').trigger('blur');
- setTimeout(function(){
- $('.form-response').hide();
- scroll_to_top();
- },3000);
- },
- error:function (xhr, ajaxOptions, thrownError){
- console.log('error...', xhr);
- //error logging
- },
- complete: function(){
- //afer ajax call is completed
- }
- });
- } else {
- alert('Please re-enter your input and try again.');
- $('input').val('');
- $('textarea').val('');
- $("input").trigger("blur");
- $("textarea").trigger("blur");
- $('#form-name').focus();
- }
- });
- <form class="form-style validate-form clearfix" id="contactform" action="mail.php" method="POST" role="form">
- <div class="col-md-6"><div class="form-group"><input type="text" class="text-field form-control validate-field required" data-validation-type="string" id="form-name" placeholder="Full Name" name="name"></div>
- <div class="form-group"><input type="email" class="text-field form-control validate-field required" data-validation-type="email" id="form-email" placeholder="Email Address" name="email"></div>
- <div class="form-group"><input type="tel" class="text-field form-control validate-field phone" data-validation-type="phone" id="form-contact-number" placeholder="Contact Number" name="contact_number">
- <input type="text" id="address-input" name="address" style="display: none!important;"></div></div><div class="col-md-6">
- <div class="form-group"><textarea placeholder="Message..." id="form-message" class="form-control validate-field required" name="message"></textarea></div>
- <div class="form-group"><button type="submit" id="submitBtn" class="btn btn-sm btn-outline-inverse">Submit</button></div></div>
- </form>
- <?php
- if ( !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' )
- {
- # is ajax
- if (empty($_POST["address"])) {
- $from = $_POST["name"]; // sender
- $subject = 'From: ' . $from;
- $email = $_POST["email"];
- $tel = $_POST["contact_number"];
- $message = $_POST["message"];
- // message lines should not exceed 70 characters (PHP rule), so wrap it
- $message = wordwrap($message, 70);
- $textToSend = 'From: ' . $from . "n";
- $textToSend .= 'Email: ' . $email . "n";
- $textToSend .= "Phone: " . $tel . "n";
- $textToSend .= 'Message: ' . $message . "n";
- // send mail
- mail("contact@domain.net", $subject, $textToSend, "From: $fromn");
- echo "Thank you, we will contact you shortly.";
- echo '
- <script>
- $("input").val("");
- $("textarea").val("");
- setTimeout(function(){
- scroll_to_top();
- },3000);
- </script>
- ';
- } else {
- echo 'Thank you, we will contact you shortly.';
- echo '
- <script>
- $("input").val("");
- $("textarea").val("");
- setTimeout(function(){
- scroll_to_top();
- },3000);
- </script>
- ';
- }
- }else{
- header( 'Location: http://www.myhomepage.net' ) ;
- die();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement