dream3323 2014-08-25 10:41
浏览 98

保存HTML5画布图像,然后通过PHP发送电子邮件

So I have a form on my website (www.100danish.com) which includes an HTML5 canvas element for the user to draw a picture along with their form.

What I want to have happen is the user clicks submit and this sends the image of the canvas along with the form information (name, e-mail, and message). Right now what I have is the following for my AJAX and PHP. I know that I will need to use JavaScript and PHP but PHP is not my expertise.

Any help would be much appreciated!!

JavaScript

//AJAX request to submit form
$('form').submit(function(evt) {
    evt.preventDefault();
    var canvasData = canvas.toDataURL('image/png');
    var url = $(this).attr("action");
    var formData = $(this).serialize();
    $.post(url, [formData, canvasData] function(response) {
        $('#contact-bottom').html("<p class='ajax-p'>Thanks for reaching out to us, we'll be in touch with you soon<br>Joey &amp; Trev</p>")
    })
});

PHP

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];


    $from = 'From: $name'; 
    $to = 'joey@100danish.com'; 
    $subject = '100 Danish Form Submission';
    $body = "From: $name
E-Mail: $email

Message:
$message";

    mail ($to, $subject, $body, $email);
  ?>
  • 写回答

1条回答 默认 最新

  • doujiyuan0211 2014-08-25 10:59
    关注

    You're asking 2 separate questions: (1) send canvas data to server, (2) send email. Question 2 is everywhere on the internet. Question 1 can be solved as below:

    HTML:

    <canvas id="myCanvas" width="800" height="450"></canvas>
    

    JS:

    //get base64 data
    var canvas = $("#myCanvas").get(0);
    var data   = canvas.toDataURL();
    
    //send to server
    var request = $.ajax({
      type: "POST",
      url:  "canvas-data-receiver.php",
      data: { 
        base64Data:data
      }
    });
    
    request.done(function(response) {
      alert("Data posted to server!");
    });
    
    request.fail(function(){
      alert("Failed to send data to server!");
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器