Single Page Sales Promotion Application Using PHP, jQuery, Ajax and PHPMailer Part -2
This is two part tutorial, So please refer my previous tutorial before continuing this one. In this two part I am going to explain step by step how I have created Smart Invoice System Version 3 Single Page Sales Promotion Application.
After user entered their name and email, We are posting those data to PHP. Where the PHP will sending an email as well as save those details in MySQL database. For sending an email, we are using PHPMailer.
In order to use PHPMailer, Either we need to install via Composer or download directly from PHPMailer GitHub page.
Handling PHP Plugins installation and updation via Composer is very easy, So I am going install PHPMailer using Composer.
Before you start installation via Composer, you must installed Composer before using it. So Please install compose using following installation instructions.
Notes:
- We are sending response back to the front end using proper HTTP STATUS CODE and message.
- We usually send 200 HTTP STATUS code, even though the request we made was bad/exception. Because of that in front end we need to handle everything in the success callback even if it’s error/exception response. That consider to be a bad practice.
- Always sending resposne with proper HTTP STATUS CODE will help front end developer to handle the success and error effectively in their respective callback function.
Step 1: Install PHPMailer Via Composer
Once you successfully installed Composer in your system. Then open your command prompt and navigate to your project root folder using cd D:\wamp\www\projects\promtion-page.
Now run the following Composer Command in your command terminal
PHPMailer Installation Via Composer PHP
If you don’t want to install via Composer, then download PHPMailer from GitHub and add it your project directly.
If you want any of my script need to be customized according to your business requirement,
Please feel free to contact me [at] muni[at]smarttutorials.net
Note: But it will be charged based on your customization requirement
Step 2: Define Site Wide Configurations in config.php
Create config.php file and define the following site wide configs in it. where I am keeping the Gmail SMTP details and MySQL database credentials.
Step 3: Create ajax.php and Include PHPMailer
Next create ajax.php file, which will handle user posted data in PHP (Backend). First include config.php and vendor/autoload.php files in the top of the ajax.php file. Where vendor/autoload.php file generated by Composer, which will handles autoloading of required classes whenever the classes required.
Next define response array with default data in it. This response array will be sent as an ajax response to the front end based on the operation status.
Step 4: Check Ajax Requst In PHP
Next I am checking whether request received by ajax.php file is a valid ajax request or not. If it is not a valid ajax request, then I am sending response back with http_status_code ‘403’ and response array of data.
If it’s valid ajax request, then I am checking whether the request has post data in it or not. If there is no posted data, then I am sending response back with http_status_code ‘400’ and response array of data.
Step 5: Define Reponse Handler Function
Next define Response Handler function which will send response back to the front end with content-type “application/json” and echo $response array using json_encode.
Note: End of the function add exit statement, otherwise it will continue execution unnecessarily.
Step 6: Validate Email And Name Format In PHP
Next we are validating Email and Name Format using following PHP Script, If email/name is not in proper format. Then it will send the proper response back to the front end with proper http status code.
Step 7: Check Email Existence and Send An Email
Next I will check whether email already exists with us or not. If email exists then I will send response back with proper code and message.
If email not exists, then I will send an email as well as save in the database. Finally send a response with proper code (200) and message.
Step 7: Define Database Connection, Email Count and Save Data Handler Function
Following three functions which will handle the creation of database connection, getting email count and save posted data in MySQL database.
Step 8: Define Email Handler
Define Email handler function, which will send an email to specified email address.
Step 8: Ajax Response Handler
When user clicking ‘Sign Up’ button, I am making ajax request to ajax.php file. Where ajax.php file will process the request and send us back with proper status code and message.
Based on the HTTP_STATUS_CODE, it will call success/error callback. Where success/error callback function will show proper status to the user.
Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts
If you want any of my script need to be customized according to your business requirement,
Please feel free to contact me [at] muni2explore[at]gmail.com
Note: But it will be charged based on your customization requirement