Frontend Development 5 min read

Implementing Real-Time Mobile Signature Capture with jSignature and PHP

This tutorial explains how to integrate the jSignature library with jQuery on the front end and PHP on the back end to provide a real‑time mobile signature feature, covering library download, HTML markup, JavaScript initialization, reset, AJAX submission, and server‑side base64 image handling.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Implementing Real-Time Mobile Signature Capture with jSignature and PHP

Online signature capture is often required in approval workflows; this article demonstrates how to achieve real‑time mobile signature using the jSignature library together with jQuery and a PHP backend.

First, download the jSignature library ( https://www.phpclasses.org/file/42277.html ) and the accompanying Chinese documentation ( https://www.php.cn/js-tutorial-489429.html ), as well as jQuery ( https://jquery.com/download/ ), preferably version jquery-3.2.1.js .

Front‑end HTML :

<style>
    .main_sign{padding:10px 10px;color:black;background-color:darkgrey;}
    .main_sign .sign_btn{padding:5px 10px;}
    #signature{border:2px dotted black;}
</style>
<div class="main_sign" id="writers">
    <div id='signature' style='background-color: #d2d2e8;'></div>
    <button type="button" class="sign_btn" id="reset" style="margin: 10px 5px;">重写</button>
    <button type="button" class="sign_btn" id="yes" style="margin: 10px 5px;">确认</button>
    <div id="show_img" style="display: none;"><img src="" id="images"></div>
</div>

1. Instantiate jSignature :

$(document).ready(function(){
    var arguments = {
        width: '100%',
        height: '200px',
    };
    $("#signature").jSignature(arguments);
});

2. Reset signature :

$("#reset").click(function(){
    $("#signature").jSignature("reset"); // clear canvas
    $("#images").attr('src','');
});

3. Submit signature (TP5.1 + AJAX) :

// Convert canvas to image and send to backend
$("#yes").click(function(){
    var $signature = $("#signature");
    var datapair = $signature.jSignature("getData", "image");
    $("#images").attr('src','data:' + datapair[0] + "," + datapair[1]);
    var src_data = $("#images").attr('src'); // base64 data
    $.ajax({
        url:"{:url('getSignInfo')}",
        data:{src_data:src_data},
        type:"post",
        dataType:"json",
        success:function(data){
            window.location.href = data.dump_url;
        },
        error:function(){
            console.log("错误");
        }
    });
});

4. Backend data reception (getSignInfo.php) :

$data = Request::param();
$src = $this->base64ContentToImage($data['src_data'],$path);

The variable $src holds the saved image path; $path is the directory for storing signature images.

5. Convert base64 to image (base64ContentToImage method) :

public function base64ContentToImage($base64_image_content,$path){
    $dir = "./".$path;
    if(!file_exists($dir)){
        mkdir(iconv("GBK", "UTF-8", $dir),0777,true);
    }
    if(preg_match('/^(data:\s*image\/(\w+);base64,)/',$base64_image_content,$result)){
        $type = $result[2];
        $new_file = $path."/".date('Ymd',time())."/";
        if(!file_exists($new_file)){
            mkdir($new_file,0700);
        }
        $new_file = $new_file.time().".{$type}";
        if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
            return '/'.$new_file;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

Following these steps enables a fully functional mobile signature capture component that records the signature as a base64 image, saves it on the server, and provides a downloadable link.

frontendJavaScriptPHPAJAXjSignaturemobile signature
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.