无法让 var 传递 Ajax 后脚本



我似乎无法将"文件名"变量从main.php传递到save_sign.php,有人可以帮助我吗?

主.php

<? $filename="222222"; ?>    
 <script>
        $(document).ready(function() {
            $('#signArea').signaturePad({drawOnly:true, drawBezierCurves:true, lineTop:90});
        });
        $("#btnSaveSign").click(function(e){
            html2canvas([document.getElementById('sign-pad')], {
                onrendered: function (canvas) {
                    var canvas_img_data = canvas.toDataURL('image/png');
                    var img_data = canvas_img_data.replace(/^data:image/(png|jpg);base64,/, "");
                    var filename = '<? echo $filename; ?>';
                    //ajax call to save image inside folder
                    $.ajax({
                        url: 'save_sign.php',
                        data: { img_data:img_data },
                        type: 'post',
                        dataType: 'json',
                        success: function (response) {
                           window.location.reload();
                        }
                    });
                }
            });
        });
</script>     

save_sign.php

<?php 
$result = array();
$imagedata = base64_decode($_POST['img_data']);
$filename = $_POST['filename'];
//Location to where you want to created sign image
$file_name = ('./doc_signs/'.$filename.'.png');
file_put_contents($file_name,$imagedata);
$result['status'] = 1;
$result['file_name'] = $file_name;
echo json_encode($result);
?>
您需要

将文件名 var 添加到 ajax 数据中,如下所示

url: 'save_sign.php',
data: { img_data:img_data,filename:filename}