我有一个带有提交按钮的HTML表单:
<button type="submit" class="subscribe-submit" onclick="return foo()">Subscribe</button>
我希望将表格上的详细信息发送到我的个人电子邮件地址。
我搜索了Stack Overflow,Google,Bing和Yahoo,但我找不到答案
答案 0 :(得分:1)
使用纯HTML无法做到这一点。但是,使用一点点PHP,您就可以准备好表格了。
以下是一个名为form_handler.php的单独文件中的表单处理程序PHP代码的示例:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['comment'];
$mail_to = 'yourPersonalEmail@email.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the feedback.');
window.location = 'form.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Something went wrong. Please try again or contact at myPersonalEmail@email.com');
window.location = 'form.html';
</script>
<?php
}
?>
这是名为form.html的文件中的HTML表单:
<html>
<head>
</head>
<body>
<form name="form" action="form_handler.php" method="POST">
<label class="name">Name</label>
<br>
<input name="name" type="text" required="required">
<br>
<label class="email">E-Mail</label>
<br>
<input name="email" type="email" required="required">
<br>
<label class="comment">Comment</label>
<br>
<textarea name="comment"></textarea>
<br>
<input type="submit" name="submit" value="Subscribe" align="center">
</form>
</body>
</html>
我建议您稍微了解一下PHP和表单处理。您可能需要更改字段,但我认为您会明白这一点。