我目前已经设置了一个小问卷,最后会弹出一个框,上面写着“你完成了”。
我如何让这个包含一个额外的框,以便用户可以查看他们的答案,输入他们的电子邮件地址并单击提交,以便创建一封电子邮件并将他们的答案和联系方式发送给我上?
我会从我网站的其他地方复制一个简单的 html 表单,但我认为我无法将 html 输入到外部 .js 脚本中,所以我请专家寻求帮助。
以下代码应该没有任何内容,但这可能会帮助您帮助我。
问卷调查.js:
function QuestionnaireViewModel() {
var self = this;
var currentQuestionIndex = 0;
var questions = [
{
caption: 'Q1?',
answers: [
{ caption: 'Q1A1' },
{ caption: 'Q1A2' }
]
},
{
caption: 'Q2',
answers: [
{ caption: 'Q2A1' },
{ caption: 'Q2A2' }
]
},
{
caption: 'Q3',
answers: [
{ caption: 'Q3A1' },
{ caption: 'Q3A2' }
]
},
{
caption: 'Q4',
answers: [
{ caption: 'Q4A1' },
{ caption: 'Q4A2' }
]
}
];
self.currentQuestion = new ko.observable(questions[0]);
self.progress = new ko.observableArray();
self.selectQuestion = function (answer) {
self.progress.push({
question: questions[currentQuestionIndex].caption,
answer: answer.caption
});
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
self.currentQuestion(questions[currentQuestionIndex]);
} else {
alert('Your done');
}
};
};
$(document).ready(function () {
ko.applyBindings(new QuestionnaireViewModel());
});
表单 handler.php:
<?php
$errors = '';
$myemail = 'name@domain.com';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. "
. " Here are the details:\n Name: $name \n "
. "Email: $email_address \n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: thankyou.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>