我刚刚在我的一个网站上实现了这样的东西,这很奇怪。不过,我没有存储电子邮件地址,而是通过电子邮件将它们发送给自己。我将注释掉,以便您可以编写平面文件或数据库存储位。
这是我的实现,使用jQuery。偷走 :)
在 HTML 中:
<script type="text/javascript">
$(document).ready(function(){
$('#btnSubmit').click(function(){
$('#btnSubmit').disabled = true;
var val = $('#emailAddress')[0].value;
$('#emailResponse').html('<img src="ajax-loader.gif" border="0" />').css('backgroundColor', 'ffffd0');
$.getJSON('notify.php',{email: val}, function(data){
if (!data.result) {
$('#emailResponse').html(data.message).css('backgroundColor','ff9999').effect('highlight', {color: '#ffff99'}, 1000);
} else {
$('#emailResponse').html(data.message).css('backgroundColor','99ff99').effect('highlight', {color: '#ffff99'}, 1000);
}
});
$('#btnSubmit').disabled = false;
return false;
});
$('#emailAddress').keyup(function(e) {
if(e.keyCode == 13) {
$('#btnSubmit').click();
}
return false;
});
});
</script>
</head>
<body>
<div style="margin:0 auto;background:#ffffd0;width:500px;padding:10px;text-align:center; font-family:Verdana,Arial,sans-serif; border:1px solid black;">
If you would like to be notified when we launch, please leave your email address here.<br /><br />
<input type="text" id="emailAddress" size="40" style="border:1px solid gray;padding:5px;"/>
<input type="button" id="btnSubmit" value="Submit" style="padding:5px;" />
<div style="padding:10px;margin:10px;" id="emailResponse"></div>
</div>
</body>
在 notify.php 文件中:
<?php
$q = html_entity_decode($_GET["email"]);
$response = array();
if (!filter_var($q, FILTER_VALIDATE_EMAIL))
{
$response['result'] = 0;
$response['message'] = 'Invalid email given - try again';
}
else
{
// write to file or database
$response['result'] = 1;
$response['message'] = "Thanks, your details have been added, we'll notify you when we launch!";
}
}
echo json_encode($response);
?>
编辑:我知道它不漂亮 - 内联样式、<br/>
标签等。