How to generate QR code using PHP and Ajax

  •  
  •  
  •  
  •  
  •  
  •  

QR code is a two-dimensional barcode, also known as the Quick Response code. It can contain URL, simple text, contact number, email address and so on. In this tutorial, I will guide you through a step by step way about how to generate QR code using PHP and Ajax. The following example of QR code generator is very easy to implement and understand. In order to generate the QR code, you need a PHP library – PHP QR Code, download the latest version. Also, it is already included in the complete source code. Also, you may like to read the article on how to generate the barcode using PHP.

How to generate QR code using PHP and Ajax

File Structure

Before proceeding, please have look at the file structure —

  • index.php
  • generate-qr-code.php
  • bootstrap (for bootstrap CSS and JS files)
  • jquery.min.js
  • qrcodes (location of generated QR code images)
  • library (PHP QR Code library)

HTML – QR Code Generator Form (index.php)

<div class="container">
    <div class="row">
      <div class="col-md-6 col-6">
            <form class="form-horizontal" method="POST" id="form" onsubmit="return false">
                <div class="form-group">
                	<label class="control-label">Data : </label>
                	<input class="form-control col-xs-12" name="dataContent" id="dataContent" type="text" value="" required="required">
                </div>

                <div class="form-group">
                	<label class="control-label">ECC : </label>
                	<select class="form-control col-xs-12" name="ecc" id="ecc">
                		<option value="H">H - best</option>
                		<option value="M">M</option>
                		<option value="Q">Q</option>
                		<option value="L">L - smallest</option>       		            
                	</select>
                </div>

                <div class="form-group">
                	<label class="control-label">Size : </label>
                	<input type="number" min="1" max="10" step="1" class="form-control col-xs-12" name="size" id="size" value="5">
                </div>

                <div class="form-group">
                	<label class="control-label"></label>
                	<input type="submit" name="submit" id="submit" class="btn btn-success" value="Generate QR">
                </div>
            </form>
      	</div>

      	<div class="col-md-6 col-6">
      		<div class="qrdiv loading"></div>
      	</div>
    </div><!-- .row -->
</div>

The above HTML code generates a Bootstrap form where the user will input data and then it generates the  QR code based on that inputs.

Data : It can be URL, simple text, contact number, email address and so on.

ECC : Error Correction Capability. This compensates for dirt, damage or fuzziness of the barcode. A high ECC level adds more redundancy at the cost of using more space.

Size : This is the number of pixels that make a block of the matrix barcode.

Ajax – Call For QR Code Generator page (index.php)

<script type="text/javascript">
    $(document).ready(function() {
    	$("#form").submit(function(){
            $.ajax({
      		url:'generate-qr-code.php',
      		type:'POST',
      		data: {dataContent:$("#dataContent").val(), ecc:$("#ecc").val(), size:$("#size").val()},
      		beforeSend: function() {
      		    $(".qrdiv").addClass('loading');  
      		},
      		success: function(resp) {
      		    $(".qrdiv").html(resp);  
      		},
      		complete: function() {
      		    $(".qrdiv").removeClass('loading');  
      		},
           });
    	});
    });
</script>

The above jQuery Ajax code sends the user’s value to the generate-qr-code.php page and it returns the dynamically generated QR code as a .png image to the index page.

PHP – Dynamically Generate QR Code (generate-qr-code.php)

<?php 

    if(isset($_POST) && !empty($_POST)) {

        include('library/phpqrcode/qrlib.php'); 
         
        $image_location = "qrcodes/";

        $image_name = date('d-m-Y-h-i-s').'.png';

        $dataContent = $_POST['dataContent'];
        $ecc = $_POST['ecc'];
        $size = $_POST['size'];

        // generating the QR code
        QRcode::png($dataContent, $image_location.$image_name, $ecc, $size); 
        
        // displaying the QR code on the web page
        echo '<img class="img-thumbnail" src="'.$image_location.$image_name.'" />';
    
    } else {
        header('location:./');
    }
?>

The above PHP code dynamically generates the QR code based on the user inputs. First of all, you have to include the qrlib.php file in your code. Each time when a new image will create it takes the PHP date and time as the new image name. QRcode() function creates the .png image of QR code. It accepts four parameters. The first one is the data, on which you want to create the QR code. The second parameter is image location with image name. The third and fourth parameters are ECC and size of the QR code respectively.

Demo & Complete Source Code of PHP QR Code Generator

Try the online demo on – how to generate QR Code using PHP and Ajax by click on the below demo link. Download the complete source code from the below Download link. Please, like and share this post with others.

About Mitrajit

4 comments on “How to generate QR code using PHP and Ajax

  1. [Sorry I have to contact you with this method. Your form is not working the Captcha Key is expired or something.]

    Dear Sir, Madam,

    You are providing really awesome scripts. I am currently copying lot’s of your work. Of course this is not completely nice for you. You did all the work and I am just copying.

    I do not know how I am able to do something back for you. Are you maybe able to help me a little bit so we are both happy with the situation ?

    Thank you in advance,

    Tom Dings

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove you are a human *