Javascript/css/php/mysql 在网站的 div 中收集用户电子邮件地址 [英] Javascript/css/php/mysql to collect user email addresses in a div on a website

查看:24
本文介绍了Javascript/css/php/mysql 在网站的 div 中收集用户电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您希望在您的网站上有一个框,上面写着给我们您的电子邮件地址,我们将向您发送新闻"或类似内容.收集这些电子邮件地址的简单/优雅的方法是什么(假设是标准的 LAMP 堆栈)?特别是,我想推荐有关

  1. 用于处理 UI 的 JavaScript(如果电子邮件地址无效,请投诉,按 Enter 键时表示感谢等).
  2. CSS 使其看起来不错.
  3. PHP 用于实际收集电子邮件地址并存储它们(平面文件或数据库都可以).

或者,如果有一种奇特的 Rails 或 AJAX 方式可以做到这一点,我也很乐意接受.

(我目前所知道的是如何以老式的 CGI 方式执行此操作,使用带有提交按钮的纯 html Web 表单和一个服务器端脚本,该脚本获取表单内容、提取电子邮件地址并吐出out html(可能重定向回原始页面).

如果我天真地认为我可以为此从架子上拿一些东西并且应该从 AJAX 教程之类的东西开始,也请告诉我.我见过 推荐此 JQuery/AJAX 教程.这是启动并运行简单注册表单的最快方法吗?

解决方案

我刚刚在我的一个网站上实施了类似的东西,这很奇怪.不过,我没有存储电子邮件地址,而是通过电子邮件将它们发送给自己.我会注释掉这一点,以便您可以编写平面文件或数据库存储位.

这是我的实现,使用 jQuery.偷走:)

在 HTML 中:

<身体><div style="margin:0 auto;background:#ffffd0;width:500px;padding:10px;text-align:center; font-family:Verdana,Arial,sans-serif; border:1px纯黑色;">如果您想在我们发布时收到通知,请在此处留下您的电子邮件地址.<br/><br/><input type="text" id="emailAddress" size="40" style="border:1px 纯灰色;padding:5px;"/><input type="button" id="btnSubmit" value="Submit" style="padding:5px;"/><div style="padding:10px;margin:10px;"id="emailResponse"></div>

在notify.php文件中:

我知道它并不漂亮 - 内联样式、<br/> 标签等

Suppose you want a box on your website that says "Give us your email address and we'll send you news" or somesuch. What's a simple/elegant way to collect those email addresses (assuming a standard LAMP stack)? In particular, I'd like recommendations on

  1. Javascript to handle the UI (complain if invalid email address, say thanks when they hit enter, etc).
  2. CSS to make it look decent.
  3. PHP to actually collect the email addresses and store them (either flat file or database is fine).

Or if there's a fancy Rails or AJAX way to do this, I'm very open to that, too.

(All I know currently is how to do this the old-school CGI way, with a plain html web form with a submit button and a server-side script that takes the form contents, pulls out the email address, and spits out html (potentially a redirect back to the original page).)

If I'm naive in thinking I can grab something off the shelf for this and should be starting with something like an AJAX tutorial, let me know that as well. I've seen this JQuery/AJAX tutorial recommended. Is that or something like it the quickest way to get a simple sign-up form up and running?

解决方案

I just implemented something like this on one of my websites strangely enough. I didn't store the email addresses though, I emailed them to myself. I'll comment that bit out so you can write your flat-file or database storage bits.

Here's my implementation, using jQuery. Steal away :)

In the 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>

In the notify.php file:

<?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);
?>

Edit: I know it's not pretty - inline styles, <br/> tags, etc.

这篇关于Javascript/css/php/mysql 在网站的 div 中收集用户电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆