php - form validation for newsletter signup with javascript popup -


i have following code on homepage people sign newsletter - email address entered newsletter_emails table , on clicking go button thank 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 !');                  });              } 

the page email inserted database (ajax_subcribe.php) 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 newsletter_emails (email,category) values (".sql_quote($_post['email']).",'1')";      mysql_query($q);  ?> 

while works well, there no validation on ajax_subscribe page - have following javascript on part of 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> 

i incorporate such validation newsletter form on clicking go button, either "thank you" popup or "invalid email" popup appears. reason cant working if enter form id="form_id", etc etc. have been able on side or other working not both together!

any advice appreciated jd

use in form call js validation code

 <form method="get" action="javascript:validate(id)"> 

for work js should return false. use js location redirect on success.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -