带有JavaScript弹出窗口的电子报注册的表单验证 [英] form validation for newsletter signup with javascript popup

查看:61
本文介绍了带有JavaScript弹出窗口的电子报注册的表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主页上有以下代码供人们注册新闻通讯-电子邮件地址输入到newsletter_emails表中,然后单击执行"按钮,将显示谢谢"弹出窗口.

I have the following code on my homepage for people to sign up for a newsletter - the email address is entered into a newsletter_emails table and on clicking GO button a thank you popup appears.

 Email:  <input type="text" name="email" value="" id="email" />

        <input type="button" name="submit" onclick="submit_me()" value="GO" />

        <script type="text/javascript" charset="utf-8">

            function submit_me() {

                var email_value = $('#email').val();

                $.post("ajax_subscribe.php", { email: email_value }, function(response) {

                    if (response!='') {alert(response)};
                    alert('Thank You !');

                });

            }

将电子邮件插入数据库的页面(ajax_subcribe.php)如下:

The page where the email is inserted into the database (ajax_subcribe.php) is as follows:

    <?php 
    $host = "xxxx";
    $user = "xxxx";
    $password = "xxxx";
    $database = "xxxx";
    $server = mysql_connect($host, $user, $password);
    $connection = mysql_select_db($database, $server);

    function sql_quote($value) {
        $value = str_replace('<?','',$value);
        $value = str_replace('script','',$value);   
        if (get_magic_quotes_gpc()) {
            $value = stripslashes($value);
        }
        if (!is_numeric($value)) {
            $value = "'" . mysql_real_escape_string($value) . "'";
        } else {
            if ((string)$value[0] == '0') {
                $value = "'" . mysql_real_escape_string($value) . "'";
        }}
        return $value;
    }
    $q = "INSERT INTO newsletter_emails (email,category) VALUES (".sql_quote($_POST['email']).",'1')";

    mysql_query($q);

?>

虽然一切正常,但ajax_subscribe页面上没有验证-我在网站的另一部分使用了以下javascript.

While this all works well, there is no validation on the ajax_subscribe page - I have the following javascript on another part of my site.

    <script type="text/javascript">
function validate(form_id,email) {

   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[email].value;
   if(reg.test(address) == false) {

      alert('Invalid Email Address');
      return false;
   }
}
</script>

我想将这种验证合并到此新闻通讯表单中,以便在单击执行"按钮时出现谢谢"弹出窗口或无效电子邮件"弹出窗口.由于某些原因,即使我输入表单id ="form_id"等,也无法使它正常工作.我基本上可以站在一边或其他地方工作,但不能一起工作!

I would like to incorporate such validation into this newsletter form so that on clicking the Go button, either the "thank you" popup or the "invalid email" popup appears. For some reason I cant get it working even if I enter form id="form_id", etc etc. I have basically been able to get on side or the other working but not both together!

任何建议,不胜感激 谢谢 京东

Any advice much appreciated thanks JD

推荐答案

在表单中使用类似的代码来调用JS验证代码

use something like this in your form to call your JS validation code

 <form method="GET" action="javascript:Validate(id)">

为此,您的JS应该始终返回false.使用JS位置成功重定向.

For this to work your JS should always return false. Use JS location to redirect on success.

这篇关于带有JavaScript弹出窗口的电子报注册的表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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