检查并向db插入电子邮件,如果电子邮件已经存在警告用户,则调用ajax

时间:2016-11-25 10:20:48

标签: php ajax database email insert

您好我有以下代码。 我试图做的事情是当用户输入电子邮件时,它应该检查数据库是否已经存在。如果电子邮件不存在,则将其存储在数据库中并将其发送到ajax函数

    <!-- Begin  Signup Form -->
<div id="mc_embed_signup">

<form>

  <input type="text" id="email" name="email" class="required email" id="email" placeholder="Email Address" style="font-size:15pt;height:38px;width:350px;font-family:Century Gothic;margin-right:5px;" >

  <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_438e8df85b7f9df7fce94287a_013c74a5bc" tabindex="-1" value=""></div>
  <button value="Submit" name="subscribe" id="mc-embedded-subscribe" class="button" style="background-color: rgba(14,46,71,1);border: 1px solid #0E2E47;width:150px;height:37px;font-family:Century Gothic;font-size:18px;color:white;">Subscribe</button>
  <div id="mce-responses" class="clear">
    <div class="response" id="error" style="display:none"></div>
    <div class="response" id="success" style="display:none"></div>
  </div>
</form>

</div>
<!--End mc_embed_signup-->
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
function call_mailm(){

  $.ajax({url: "https://google.co.in/account/mailserverprimary/"+$("#email").val(), 
    success: function(result){
      $('#mc_embed_signup').html('<div align="center"  style="color:#0E2E47;font-size:18px;font-family:Century Gothic;">Thank you for subscribing.</div>');

    },
    error: function (error) {
      alert("Please Enter Valid Email Address.");
    }
  });
  return false;

}
</script>

<script>
    $(document).ready(function(){

    var re = /(\w+)\@(\w+)\.[a-zA-Z]/g;
    var $email = $("#email");
        $('#mc_embed_signup form').submit(function(){

            var emailValue = $email.val();
            var testEmail = re.test(emailValue);

            if(!testEmail){
                alert("Please enter valid email address.");
                $email.focus();
            } else {
                call_mailm();
            }

            return false;
        });
    });
</script> 

这是我的php

<?php           
         $host = 'localhost';
         $user = 'root';
         $pass = '123';

         mysql_connect($host, $user, $pass);

         mysql_select_db('dummy_test');


         if(isset($_POST['email']))
         {
           $emailId=$_POST['email'];

           $checkdata=" SELECT loginid FROM user WHERE loginid='$emailId' ";

           $query=mysql_query($checkdata);

           if(mysql_num_rows($query)>0)
           {
             echo "Email Already Exist";
           }
           else
           {
             if( isset( $_POST['submit_form'] ) )
{

$email = $_POST['email'];


$host = 'localhost';
$user = 'root';
$pass = '123';

mysql_connect($host, $user, $pass);

mysql_select_db('dummy_test');

$insertdata=" INSERT INTO user VALUES( '$email' ) ";
mysql_query($insertdata);
           }

?>  

不幸的是,我不知道如何将php与form和ajax集成 任何建议都会很感激......谢谢

0 个答案:

没有答案