Create Signature Pad & Save Using jQuery in CodeIgniter 4

Share this Article
Reading Time: 5 minutes
655 Views

Inside this article we will see a very interesting topic i.e How to create a signature pad & save using jquery in CodeIgniter 4. Signature Pad – HTML5 canvas based smooth signature drawing using variable width spline interpolation.

This tutorial will give you a idea to integrate a signature pad into your application and save signature generated image to server. For signature pad integration we will use jquery plugin and to save signature to server as an image we will use CodeIgniter 4 framwork.

Learn More –

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


Create Images Folder

Go to /public folder available at application root. Create a folder with name images. We will save generated signature images inside it.

Make sure images folder should have sufficient permission to write content into it.


Create Controller

Next, we need to create a controller file.

$ php spark make:controller Signature --suffix

It will create a file SignatureController.php at /app/Controllers folder.

Open SignatureController.php and write this complete code into it.

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class SignatureController extends BaseController
{
	public function index()
	{
		return view('signature-pad');
	}

	public function store()
	{
		if ($this->request->getMethod() == "post") {

			$file_string = $this->request->getVar('signed');

			$image = explode(";base64,", $file_string);

			$image_type = explode("image/", $image[0]);

			$image_type_png = $image_type[1];

			$image_base64 = base64_decode($image[1]);

			$folderPath = ROOTPATH . 'public/images/';

			$file = $folderPath . uniqid() . '.' . $image_type_png;

			$session = session();

			file_put_contents($file, $image_base64);

			$session->setFlashdata("success", "Signature saved successfully");

			return redirect('signature-pad');
		}
	}
}

Create Layout File

Go to /app/Views folder and create a file with name signature-pad.php

Open signature-pad.php and write this complete code into it.

<html>

<head>
    <title>Create Signature Pad Using jQuery in CodeIgniter 4</title>

    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css">

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />

    <link rel="stylesheet" type="text/css" href="http://keith-wood.name/css/jquery.signature.css">

    <style>
        .kbw-signature {
            width: 100%;
            height: 200px;
        }

        #sig canvas {
            width: 100% !important;
            height: auto;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-8 offset-md-3 mt-5">
                <div class="card">
                    <div class="card-header">
                        <h5>Create Signature Pad Using jQuery in CodeIgniter 4</h5>
                    </div>
                    <div class="card-body">

                        <?php if (session()->get("success")) { ?>
                            <div class="alert alert-success">
                                <?= session()->get("success") ?>
                            </div>
                        <?php } ?>

                        <form method="POST" action="<?= base_url('submit-signature') ?>">
                            <div class="col-md-12">
                                <label class="" for="">Draw Signature:</label>
                                <br />
                                <div id="sig"></div>
                                <br><br>
                                <button id="clear" class="btn btn-danger">Clear Signature</button>
                                <button class="btn btn-success">Save</button>
                                <textarea id="signature" name="signed" style="display: none"></textarea>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>

    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

    <script type="text/javascript" src="http://keith-wood.name/js/jquery.signature.js"></script>

    <script type="text/javascript">
        var sig = $('#sig').signature({
            syncField: '#signature',
            syncFormat: 'PNG'
        });
        $('#clear').click(function(e) {
            e.preventDefault();
            sig.signature('clear');
            $("#signature").val('');
        });
    </script>
</body>

</html>

Create Route

Open Routes.php from /app/Config folder and add these route into it.

//...

$routes->get('signature-pad', 'SignatureController::index');
$routes->post('submit-signature', 'SignatureController::store');

//...

Application Testing

Start development server:

$ php spark serve

URL: http://localhost:8080/signature-pad

When we hit Save button, it will save the signature image into images folder.

We hope this article helped you to learn Create Signature Pad & Save Using jQuery in CodeIgniter 4 in a very detailed way.

Buy Me a Coffee

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here