简体   繁体   English

如何使用 PHP 捕获 Szimek/Signature_Pad(将 Javascript 捕获到 PHP 变量中)?

[英]How to Capture Szimek/Signature_Pad with PHP (Capture Javascript into PHP Variable)?

I was browsing StackOverflow and came across Szimek/Signature_Pad to capture electronic / digital signatures using Javascript.我在浏览 StackOverflow 时遇到了 Szimek/Signature_Pad 使用 Javascript 捕获电子/数字签名。

I researched and I'm still dumbfounded on how to capture the DATA URI into a variable.我进行了研究,但仍然对如何将 DATA URI 捕获到变量中感到目瞪口呆。

http://szimek.github.io/signature_pad/

I want to capture it like this $inputESignature = signaturePad.toDataURL() , where signaturePad.toDataURL() is Javascript.我想像这样捕获它$inputESignature = signaturePad.toDataURL() ,其中signaturePad.toDataURL()是 Javascript。

If clarification is needed, please let me know.如果需要澄清,请告诉我。 Sorry if my request is a bit vague.对不起,如果我的要求有点模糊。

For other users who need a push in the right direction and found $_POST or $_REQUEST to be extremely vague...对于需要朝正确方向推动并发现 $_POST 或 $_REQUEST 非常模糊的其他用户......

Here is what I did.这是我所做的。

First I added a hidden input, because I integrated this into a form:首先我添加了一个隐藏的输入,因为我把它集成到了一个表单中:

<input type="hidden" name="input_name" value="" />

Then you can store the data uri image into that input's value and it will get stored with the form.然后您可以将数据 uri 图像存储到该输入的值中,它将与表单一起存储。

I added this to the JS to add the data uri to the form:我将此添加到 JS 以将数据 uri 添加到表单中:

var input = wrapper.querySelector("input");

canvas.addEventListener("mouseup", function(event) {
    if ( signaturePad.isEmpty() ) {
        input.value = '';
    } else {
        input.value = signaturePad.toDataURL();
    }
});

And then update the clear button with input.value = '';然后用input.value = '';更新清除按钮to wipe the field:擦场:

clearButton.addEventListener("click", function (event) {
    signaturePad.clear();
    input.value = '';
});

This is based off ofSzimek's demo code .这是基于Szimek 的演示代码

Another option for posting the data to your php would be to use an ajax request which is pretty easy with jQuery's $.ajax or $.post.将数据发布到 php 的另一种选择是使用 ajax 请求,这对于 jQuery 的 $.ajax 或 $.post 来说非常简单。

This script is surprisingly easy to use...这个脚本非常容易使用......

I also used Szimek's demo code as the foundation of my solution.我还使用 Szimek 的演示代码作为我的解决方案的基础。 I had some trouble with Jake's approach when using an iPad, so I intervened even less with the demo code.我在使用 iPad 时对 Jake 的方法有一些麻烦,所以我对演示代码的干预更少。

I also added an HTML form, but my form has no buttons, it simply has the invisible input element:我还添加了一个 HTML 表单,但我的表单没有按钮,它只有一个不可见的输入元素:

<form id="sigform" action="myscript.php" method="post">
    <input id="siginput" type="hidden" name="sig" value="" />
</form>

I then added the following var definitions and this change to the saveButton listener in the demo app.js code:然后,我在演示 app.js 代码中向 saveButton 侦听器添加了以下 var 定义和此更改:

var form = document.getElementById("sigform"),
    input = document.getElementById("siginput")

saveButton.addEventListener("click", function (event) {
    if (signaturePad.isEmpty()) {
        alert("Please provide signature first.");
    } else {
        input.value = signaturePad.toDataURL();
        form.submit();
    }
}

And finally, to handle the results in PHP I used this:最后,为了在 PHP 中处理结果,我使用了这个:

if ( $_POST['sig'] ) {
    file_put_contents("myfile.png", file_get_contents($_POST['sig']));
}

This did the trick.这成功了。

To properly answer your question.正确回答你的问题。

You may retreive a post variable in PHP with $_POST or $_REQUEST http://www.php.net/manual/en/reserved.variables.request.php您可以使用$_POST$_REQUEST在 PHP 中检索 post 变量http://www.php.net/manual/en/reserved.variables.request.php

I'm using this script to echo the result in php form.我正在使用此脚本以 php 形式回显结果。

saveButton.addEventListener("click", function (event) {
    if (signaturePad.isEmpty()) {
        alert("Please provide signature first.");
    } else {
      var image = signaturePad.toDataURL(); // data:image/png....
     document.getElementById('mydata').value = image;
     document.getElementById('results').innerHTML = '<img src="'+image+'"/>';
    }
});

html html

   <div id="results"><b>Preview:</b></div>
   <input id="mydata" type="hidden" name="mydata" value=""/>

use the "mydate" to store it in folder or in mysql.使用“mydate”将其存储在文件夹或 mysql 中。

I'd like to add that you must also add the following code to the top of the app.js file for EFC's solution to work.我想补充一点,您还必须将以下代码添加到 app.js 文件的顶部,以便 EFC 的解决方案工作。 Also, must create a new button with the same data-action.此外,必须创建一个具有相同数据操作的新按钮。

var saveButton = wrapper.querySelector("[data-action=save]");



<button type="button" class="button save" data-action="save">Save</button>

I struggled with this since I'm not strong in JavaScripting.我为此苦苦挣扎,因为我不擅长 JavaScript。 I finally figured it out after several hours of working with this code.在使用此代码几个小时后,我终于弄清楚了。 I got hung up on the html form with the hidden field not having a submit button.我挂在了 html 表单上,隐藏字段没有提交按钮。 Then I realized the javascript was submitting the form with the save button.然后我意识到javascript正在使用保存按钮提交表单。 Hope this helps someone else.希望这对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM