Skip to Content
how to create a popup box using jquery in php
javascriptphp code / September 9, 2021

The web application’s Contact Form is a must-have feature. It assists the site administrator in receiving user feedback, recommendations, and complaints. In most cases, the contact form element is inserted into a web page. However, depending on the website’s UI, this positioning can be altered. The modal window is a great way to include a contact form. Using a modal window, you can quickly add the contact form feature into any web page.

The modal window overlays the existing web page with a popup element that does not interact with the page elements. Any link or button on the web page can be used to trigger the popup contact form. In this article, we’ll teach you how to use jQuery to construct a contact form in a popup dialogue using Ajax and PHP to send an email with form data.

In this example script, we’ll create a contact form in a popup window and use jQuery Ajax to submit the form input without refreshing the page. When a form is submitted, PHP sends the form data to the site administrator through email.a

Popup Contact Form

Trigger Button

The following button (#mbtn) triggers the modal window.

<button id="mbtn" class="btn btn-primary turned-button">Contact Us</button>

Modal Dialog:

With the contact form, the following HTML creates a modal dialogue.

<div id="modalDialog" class="modal">
<div class="modal-content animate-top">
<div class="modal-header">
<h5 class="modal-title">Contact Us</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
<form method="post" id="contactFrm">
<div class="modal-body">
<!-- Form submission status -->
<div class="response"></div>

<!-- Contact form -->
<div class="form-group">
<input type="text" name="name" id="name" class="form-control" placeholder="Enter your name" required="">
<div class="form-group">
<input type="email" name="email" id="email" class="form-control" placeholder="Enter your email" required="">
<div class="form-group">
<textarea name="message" id="message" class="form-control" placeholder="Your message here" rows="6"></textarea>
<div class="modal-footer">
<!-- Submit button -->
<button type="submit" class="btn btn-primary">Submit</button>

Open/Hide Modal Popup:
Include the jQuery library.

<script src=""></script>

Using jQuery, the following code opens or closes the popup window.

The modal is opened by pressing the trigger button (#mbtn).
The modal is hidden when the close button (.close) is pressed.

// Get the modal
var modal = $('#modalDialog');
// Get the button that opens the modal
var btn = $("#mbtn");
// Get the <span> element that closes the modal
var span = $(".close");
// When the user clicks the button, open the modal 
btn.on('click', function() {;
// When the user clicks on <span> (x), close the modal
span.on('click', function() {
// When the user clicks anywhere outside of the modal, close it
$('body').bind('click', function(e){

Contact Form Submission

The form data is submitted to the server-side script via jQuery and Ajax when the submit button on the dialogue window is clicked.

The Ajax request is started via the $.ajax() method.
The complete contact form data is passed to the ajax submit.php server-side script.
The form submission status is presented on the popup based on the response.

$('.modal-body').css('opacity', '0.5');
$('.btn').prop('disabled', true);
$form = $(this);
type: "POST",
url: 'ajax_submit.php',
data: 'contact_submit=1&'+$form.serialize(),
dataType: 'json',
success: function(response){
if(response.status == 1){
$('.response').html('<div class="alert alert-success">'+response.message+'</div>');
$('.response').html('<div class="alert alert-danger">'+response.message+'</div>');
$('.modal-body').css('opacity', '');
$('.btn').prop('disabled', false);

Send Email using PHP

The Ajax request calls this ajax submit.php code to send an email containing contact form data using PHP.

The PHP $_POST method is used to retrieve data from a form.
Validate the user’s input to ensure that the value given is both legitimate and not empty.
Using the PHP mail() function, send HTML email with form inputs.
Using the json encode() function, return the response in JSON format.

// Recipient email 
$toEmail = ''; 
$status = 0; 
$statusMsg = 'Something went wrong! Please try again after some time.'; 
// Get the submitted form data 
$email = $_POST['email']; 
$name = $_POST['name']; 
$message = $_POST['message']; 
// Check whether submitted data is not empty 
if(!empty($email) && !empty($name) && !empty($message)){ 
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){ 
$statusMsg = 'Please enter a valid email.'; 
$emailSubject = 'Contact Request Submitted by '.$name; 
$htmlContent = '<h2>Contact Request Submitted</h2> 
// Set content-type header for sending HTML email 
$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
// Additional headers 
$headers .= 'From: '.$name.'<'.$email.'>'. "\r\n"; 
// Send email 
$sendEmail = mail($toEmail, $emailSubject, $htmlContent, $headers); 
$status = 1; 
$statusMsg = 'Thanks! Your contact request has been submitted successfully.'; 
$statusMsg = 'Failed! Your contact request submission failed, please try again.'; 
$statusMsg = 'Please fill in all the mandatory fields.'; 
$response = array( 
'status' => $status, 
'message' => $statusMsg 
echo json_encode($response); 

Note :

This sample code shows how to use jQuery to create a contact form popup. This script can be used for any type of popup form, not just the contact us form (Login, Registration, Feedback, etc). When the user submits the form, the information entered will be emailed to the site administrator. This jQuery popup contact form’s functionality can be simply customised to suit your needs.

Bobbye Kohl says:


We have hacked y0ur website and extracted your databases.

H0w did this happen?

0ur team has f0und a vulnerability within your site that we were able t0 exploit. After finding the vulnerability we were able to get y0ur database credentials and extract your entire database and move the inf0rmation to an offshore server.

What does this mean?

We will systematically g0 thr0ugh a series of steps 0f totally damaging your reputati0n. First your database will be leaked or s0ld t0 the highest bidder which they will use with whatever their intenti0ns are. Next if there are e-mails f0und they will be e-mailed that their inf0rmati0n has been sold 0r leaked and your site was at fault thusly damaging y0ur reputation and having angry cust0mers/associates with whatever angry cust0mers/ass0ciates do. Lastly any links that you have indexed in the search engines will be de-indexed based 0ff of blackhat techniques that we used in the past to de-index our targets.

How do i stop this?

We are willing t0 refrain from destr0ying y0ur site’s reputati0n for a small fee. The current fee is $3000 in bitcoins (.16 BTC).

Please send the bitcoin to the following Bitcoin address (Make sure to copy and paste):


0nce y0u have paid we will automatically get informed that it was y0ur payment. Please n0te that you have t0 make payment within 5 days after receiving this e-mail 0r the database leak, e-mails dispatched, and de-index 0f your site WiLL start!

H0w d0 i get Bitcoins?

Y0u can easily buy bitcoins via several websites or even offline fr0m a Bitcoin-ATM.

What if i d0n’t pay?

if y0u decide n0t t0 pay, we will start the attack at the indicated date and uph0ld it until you do, there’s no c0unter measure t0 this, y0u will 0nly end up wasting more m0ney trying t0 find a s0luti0n. We will c0mpletely destroy your reputation amongst g00gle and your cust0mers.

This is n0t a h0ax, do n0t reply to this email, d0n’t try t0 reason 0r neg0tiate, we will not read any replies. 0nce you have paid we will stop what we were doing and y0u will never hear from us again!

Please n0te that Bitcoin is an0nymous and n0 0ne will find 0ut that y0u have complied.

Leave a Reply

Your email address will not be published.

PHPCODE © 2023