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 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题