用户在测验开始使用javascript之前输入名称

用户在测验开始使用javascript之前输入名称,javascript,html,css,Javascript,Html,Css,我已经创建了一个测验,但想知道当用户输入电子邮件时如何开始 基本上,我希望用户在开始我的测试之前输入他们的名字,然后一旦他们完成测试,结果就会通过电子邮件发送给我,使用Javascript 这可能吗?据我所知,你不能用纯JavaScript发送电子邮件,你需要一个服务、后端或php文件 例如,此php文件: <?php header('Content-type: application/json'); $errors = ''; if(empty($errors)) { $f

我已经创建了一个测验,但想知道当用户输入电子邮件时如何开始

基本上,我希望用户在开始我的测试之前输入他们的名字,然后一旦他们完成测试,结果就会通过电子邮件发送给我,使用Javascript


这可能吗?

据我所知,你不能用纯JavaScript发送电子邮件,你需要一个服务、后端或php文件

例如,此php文件:

<?php

header('Content-type: application/json');

$errors = '';

if(empty($errors))
{
    $from_name = $_POST['name'];
    $from_email = $_POST['email'];
    $message = $_POST['message'];

    $to_email = 'your@email.com';
    $to_email_cc = $from_email;

    $contact = "<p><strong>Name:</strong> $from_name</p>
            <p><strong>Email:</strong> $from_email</p>";
    $content = "<p>$message</p>";

    $email_subject = "Neue Nachricht von $from_name erhalten";
    $email_subject_cc = "Deine Nachricht wurde gesendet";

    $email_body = '<html><body>';
    $email_body .= "$contact $content";
    $email_body .= '</body></html>';

    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $headers .= "From: $my_email\n";
    $headers .= "Reply-To: $from_email";

    mail($to_email,$email_subject,$email_body,$headers);
    mail($to_email_cc,$email_subject_cc,$email_body,$headers);

    $response_array['status'] = 'success';
    echo json_encode($response_array);
} else {
    $response_array['status'] = 'error';
    echo json_encode($response_array);
}
?>

然后你可以用angular、jquery或其他工具发表文章。。 大概是这样的:

postEmail(newMail: Email): Observable<string>{
    let body = `name=${newMail.name}&email=${newMail.email}&message=${newMail.message}`;
    let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
    let options = new RequestOptions({ headers: headers });

    return this._http.post(this._contactUrl, body, options)
                    //.map(res =>  <string> res.json())
                    .catch(this.handleError)
  }
postmail(newMail:Email):可见{
让body=`name=${newMail.name}和email=${newMail.email}和message=${newMail.message}`;
let headers=新的头({'Content Type':'application/x-www-form-urlencoded'});
let options=newrequestoptions({headers:headers});
返回此.\u http.post(此.\u联系人URL、正文、选项)
//.map(res=>res.json())
.接住(这个.把手错误)
}

这是angular2中的一些旧代码,我写过一次,没有经过测试,但它应该能让您了解如何执行此操作。

是的,这是可能的,您能展示您迄今为止尝试过的内容吗?