无法获取var以传递Ajax发布脚本

时间:2019-01-05 04:25:17

标签: javascript php jquery ajax html5

我似乎无法将main.php中的“文件名”变量传递给save_sign.php,有人可以帮我吗?

main.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);
?>

1 个答案:

答案 0 :(得分:0)

您需要将文件名var添加到如下所示的ajax数据中

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