Press enter to see results or esc to cancel.

Php Ajax Contact Form with PHPmailer

In this Article we are gonna see how to create a contact form with using Ajax and Php.

In most case people create Contact Form, but it either doesn’t work or goes in Spam Box. That’s why i would like to share the Contact Form, which i use since years and work everytime 100%.

For using this Contact Form you dont have know anything special, just read the Guide and done. I capture a tutorial years ago, but in Turkish, thats why im gonna write down below a Guide.

I hope its helps 🙂

Backup-Video-Link: https://vimeo.com/157353491

phpmailer

What is PHPmailer ?

Phpmailer is a PHP class that can be used in PHP and prepares the appropriate environment by communicating with the mail server with the connection information that you specify to send an email to an email address.

The mail() function, which is the standard mail sending function of the PHP programming language, is currently disabled on all shared hosting services to prevent spam mails. However, it doesnt mean that mail cant be sent through the server. Alternatively, you can use a mailer script that communicates with the server via SMTP (Simple Mail Transfer Protocol) protocol and authenticates on the server.
Phpmailer is the best way to do this 🙂

Play Video

How to use Php Ajax Contact Form

  1. Download the .zip File and unzip it, on your localhost or on FTP-Server.
  2. If you have your own Form, just change your form id="contactForm" and action="send.php"
  3. Than paste script codes down below your form
  4. You have to also change the input id‘s too, but i use standart terms, maybe its same.

Explaining the Codes

  1. First i hide the Error Labels with using display:none

  1. Importants to change:
    1. action If you want to change send.php
    2. form-id When you changed this, you have change in script too!

  1. Importants to change :
    1. data: If you changed form-id
    2. url: If you changed send.php, change this too.
    3. Control if you are using Jquery. If you have already Jquery in your Page, check out which one works for boths. Sometimes there is conflict cause two Jquery Libraries.

  1. Here we are checking again the empty inputs, if someone blocks javascript.
    We are taking precautions against XSS too with using addslashes and strip_tags

    You don’t have to change here anything, if you didn’t changed any input name.

  1. $mail->Host Your SMTP Host. Most it is webmail.sitename.com or mail.sitename.com
  2. $mail->PortIn most case “25” is unsecure Port but fast, “465” secure Port, speed up to your Hosting Provider.
  3. I think the other Codes are obvious, just change the information with yours.

Most Important Part is here.

Just don’t forget to change line 49 public $From to your Mail address.

public $FromName is the Subject, which going to show in Inbox as a Header.

Rating: 5.0/5. From 9 votes.
Please wait...
Comments

14 Comments

Hussin Ahmed

Greetings from Colorado!
I’m bored too death at work so I decided to check out yoour website on my iphone during lunch break.
I really like the knowledge you present here and can’t wait to take a look when I get home.
I’m surprised at how quick your blog loaded on my phone .. I’m not efen using WIFI, just 3G ..
Anyways, fantastic blog!

Azizullah Ozbek

Thank you.(Even if you’re Bot 🙂

Steven

Hallo Azizuallah I have made a web, everything works fine except the contact form that displays “Email was successfully sent”, but in fact it never was sent.
The template came with Ajax and JS query, the web has all the Html but i think it’s the connection between the PHP script I put in …it just does not send.
If I send you the FTP codes and my email address where I would like the contact form to deliver, could you wire it up please and make it work, if you have some free time ? Sincerely Steven

Azizullah Ozbek

Hey Steven, yeah sure. Just send me the contact-form part of your code or as you wish the ftp credentials: [email protected]

max

Hi man. I use your code and is fantastic.

Just one thing, I keep to receive message “there is an error”, but everything is Ok and mail is correctly sended.

How is possible?

Azizullah Ozbek

that means you get from send.php some output:
if($mail->Send())
echo "";
}

Debug the send.php

Daniel

The best tutorial! Thanks a lot, man. Greetings from Brazil.

Daniel

Only an observation:
Fromname should be FromName.

Aziz Ozbek

Thank you for hint 🙂 it’s updated now.

MUSTAFA BAKIR

Aziz dostum merhaba, Türk olduğunu görünce yazmak istedim. PHPMailer ve Java Script kod çakışması ile ilgili bir sorunum var. Rica etsem mail adresimden bana ulaşıp yardımcı olur musun, çok makbule geçer.

Aziz Ozbek

Merhaba, bu sayfanin türkçe versiyonuda var 🙂 Problemini yorum olarak yaz, beraber çözelim, ilerde ayni problemi yasayan biri olursa faydalanir 🙂
https://azizozbek.ch/blog/2017/11/php-ajax-iletisim-formu/

Frederick D

Do you need help with graphic design – brochures, banners, flyers, advertisements, social media posts, logos etc.?

We charge a low fixed monthly fee. Let me know if you’re interested and would like to see our portfolio.

Çift Monitör

hello, how can I add simple captcha numbers at this form?

Aziz Ozbek

Hi, you can use any kind of captcha solution for validation. for example:
https://github.com/LordDashMe/php-simple-captcha


Leave a Comment

Aziz Ozbek

WordPress Expert from Zürich

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close