ajax jquery Newsletter Subscribe example core php

<!---Html---->
=======================Html=============================================

 <div id='response'></div>
      <div class="form">
      <form class="news-letter" action="javascript:void(0)" method="post" enctype="multipart/form-data" name="subscriber_form" id="subscriber_form">
        <input type="email" placeholder="Enter your Email" name="user_subs_email" id="user_subs_email" class="form-control first">
        <!--<input type="text" class="bttn" value="Subscribe">-->
        <button type="submit" id="user_subcribe_submit" class="btn btn-default" value="Subscribe"  ><i class="fa fa-envelope-o"></i> Subscribe</button>
       </form>


============================script ======================================


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>
<script>
$(document).ready(function(){
    $('#subscriber_form').submit(function(){
    
        // show that something is loading
        $('#response').html("<b>Loading response...</b>");
        
        /*
         * 'post_receiver.php' - where you will pass the form data
         * $(this).serialize() - to easily read form data
         * function(data){... - data contains the response from post_receiver.php
         */
        $.ajax({
            type: 'POST',
            url: 'post_receiver.php',
            data: $(this).serialize()
        })
        .done(function(data){
            
            // show the response
            $('#response').html(data);
           
           
            
        })
        .fail(function() {
        
            // just in case posting your form failed
            alert( "Posting failed." );
            
        });

        // to prevent refreshing the whole page page
        return false;

    });
});
</script>

======================= php page ====================================

<?php
include 'apanel/config.php';  // connection file
//echo "<pre>";
   // print_r($_POST);
     $email =  $_POST['user_subs_email'];
   
     $retrieve_data = "select * from subscribe where email = '".$email."'";   
     $result = mysql_query($retrieve_data) or die(mysql_error());
        
           $row=mysql_fetch_array($result);
           $data=mysql_num_rows($result);
          
        // echo "<pre>";   print_r($row);
          
           if($data>0){
            echo "<p style='color:red';>You are already subscribed</p>"; 
             
           }else {
              
            $sql="insert into subscribe(email)values('$email')";

            //$sql = "insert into subscribe (email)values(".$email.")";
            $sql_result = mysql_query($sql) or die(mysql_error());
             echo "<p style='color:green';>Thank you, You have successfully subscribed</p>";
           }
   
?>

======================= Database ============================

--
-- Table structure for table `subscribe`
--

CREATE TABLE IF NOT EXISTS `subscribe` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(50) NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT '1',
  `date_added` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

Comments

Post a Comment

Popular Posts