image1 image2 image3

WELCOME TO GETNETRIX|WE ARE PROFESSIONAL|WE LOVE TO DO CREATIVE THINGS|IGNORE THE BASICS, LEARN DIVINE

How to create a contact form using ajax and php?

I've heard this question many times and of course, the solution is simple!...


To post a data using Ajax, you can use this below function.


function postajax(form1)
{
$.ajax({
url:form1.attr('action'),
type:"POST",
cache:none,
timeout:20000,
data:form1.serialize(),
success:function(data){

alert(data);
},
error:function(data){
alert(data);
}
});

}

This function accepts a form and post the  data!
Function include necessary codes for posting all data from the form and an alert() to show the returned message... You could alternatively use this data for other purposes!...


Working

URL to be posted will be taken from the action attribute of the form.
type: the type is assigned as POST, you could change it to GET or add form1.attr('method') to get the default specified method.

data: the data is assigned using the serialized() function.
NB: - Read more about serialize() function from the official website.

success:function() : is executed when ajax successfully returns data from the php file.

error:function() : is executed when an error is received while posting data.


How to pass form to this function?

code:

   $('#id').submit(function(e){

 e.preventDefault();
}

create a submit function where #id is the id of the form .The submit method will be called when you submit the form. You could check this event manually by using the submit() function of JQuery.

on submit method use the function  e.preventDefault(); will prevent the default of the form , that is posting !...


in this method call our postajax(form1) function with this...


   $('#id').submit(function(e){
    postajax(this);

 e.preventDefault();
}

PHP Code

PHP mail() function can be used for sending an email. It is a straightforward and easy process in php.


 $name       = @trim(stripslashes($_POST['name'])); 
 $from       = @trim(stripslashes($_POST['email'])); 
 $subject    = @trim(stripslashes($_POST['subject'])); 
 $message    = @trim(stripslashes($_POST['message'])); 
 $to   = 'kapil.mo@outlook.com';//replace with your email

 $headers   = "MIME-Version: 1.0". "\r\n" . "Content-type: text/plain; charset=iso-8859-1" . "\r\n". "From: {$name} <{$from}>". "\r\n". "Reply-To: <{$from}>". "\r\n" . "Subject: {$subject}" . "\r\n" . "X-Mailer: PHP/".phpversion();

 mail($to, $subject, $message, $headers);
echo $headers;

die;
?>

The headers are used to set the reply, from, subject and content type!...
Content-type can be changed to text/html , if you want to include HTML content in email!...
Headers is a must in many servers.

Read more about mail() function from the official website.



Other Ways and examples:



        $('#contactus').submit(function(e){
            e.preventDefault();
           
       
             $.ajax({
url: $(this).attr('action'),
            type: "POST",
            cache: false,
            timeout: 20000,
            data:$('#contactus').serialize(),
beforeSend: function(){
                 $('.mails').addClass("bg-danger");
$('.mailc').html("Mail is being send...").fadeIn();
},
            success: function (data) {
             // alert(data);
                $('.mails').addClass("bg-success");
           $('.mailc').html("Mail Send").delay(3000).fadeOut();
                 $('#contactus').find("input[type=text], textarea,input[type=email]").val("");

            }

});

        });
        




Don't forget to post your comments @getnetrix © genetrix.blogspot.com

Share this:

CONVERSATION

1 Comments:

  1. Cảm ơn vi một bài viết xuất sắc. Mình cũng muốn giới thiệu về một thương hiệu dịch thuật uy tín: Công ty CP dịch thuật miền trung - MIDtrans địa chỉ 02 Hoàng Diệu, TP Đồng Hới, tỉnh Quảng Bình có Giấy phép kinh doanh số 3101023866 cấp ngày 9/12/2016 là đơn vị chuyên cung cấp dịch vụ dịch thuật, phiên dịch dành các cá nhân. Hệ thống thương hiệu và các Công ty dịch thuật con trực thuộc: công ty dịch thuật sài gòn 247 địa chỉ 47 Điện Biên Phủ, Phường Đakao, Quận 1 TP HCM, dịch thuật bình dương : địa chỉ 123 , Lê trọng tấn, dĩ an, bình dương là nhà cung ứng dịch vụ dịch thuật uy tín hàng đầu tại bình dương viet translate : dịch vụ dịch thuật cho người nước ngoài có nhu cầu, giao diện tiếng Anh dễ sử dụng; dịch thuật công chứng quận 7 (bảy) : nhà cung ứng dịch vụ dịch vụ dịch thuật phiên dịch hàng đầu tại Quận 7, TP HCM; công ty dịch thuật Đà Nẵng : Địa chỉ 54 Đinh Tiên Hoàng, Quận Hải Châu, TP Đà Nẵng chuyên cung cấp dịch vụ dịch thuật công chứng, dịch thuật chuyên ngành tại Đà Nẵng. Chúng tôi chuyên cung cấp các dịch vụ biên dịch và phiên dịch, dịch thuật công chứng chất lượng cao hơn 50 ngôn ngữ khác nhau như tiếng Anh, Nhật, Hàn, Trung, Pháp, Đức, Nga, Tây Ban Nha, Bồ Đào Nha, Ý, Ba Lan, Phần Lan, Thái Lan, Hà Lan, Rumani, Lào, Campuchia, Philippin, Indonesia, La Tinh, Thụy Điển, Malaysia, Thổ Nhĩ Kỳ..vv... Dịch thuật MIDtrans tự hào với đội ngũ lãnh đạo với niềm đam mê, khát khao vươn tầm cao trong lĩnh vực dịch thuật, đội ngũ nhân sự cống hiến và luôn sẵn sàng cháy hết mình. Chúng tôi phục vụ từ sự tậm tâm và cố gắng từ trái tim những người dịch giả.Tự hào là công ty cung cấp dịch thuật chuyên ngành hàng đầu với các đối tác lớn tại Việt nam trong các chuyên ngành hẹp như: y dược (bao gồm bệnh lý), xây dựng (kiến trúc), hóa chất, thủy nhiệt điện, ngân hàng, tài chính, kế toán. Các dự án đã triển khai của Công ty dịch thuật chuyên nghiệp MIDtrans đều được Khách hàng đánh giá cao và đạt được sự tín nhiệm về chất lượng biên phiên dịch đặc biệt đối với dịch hồ sơ thầu , dịch thuật tài liệu tài chính ngân hàng, dịch thuật tài liệu y khoa đa ngữ chuyên sâu. Đó là kết quả của một hệ thống quản lý chất lượng dịch thuật chuyên nghiệp, những tâm huyết và kinh nghiệm biên phiên dịch nhiều năm của đội ngũ dịch giả của chúng tôi. Hotline: 0947688883. email: info@dichthuatmientrung.com.vn . Các bạn ghé thăm site ủng hộ nhé. Cám ơn nhiều

    ReplyDelete