Create Signature Pad & Save Using jQuery in CodeIgniter 4

Share this Article
Reading Time: 5 minutes
4,494 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 framework.

Learn More –

Let’s get started.


CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Environment (.env) Setup

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

Either we can do via renaming file as simple as that. Also we can do by terminal command.

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.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# 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

Open project terminal and start development server via command:

$ 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

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5