SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Preventing Duplicate Entries - jQuery-Ajax Not returning error (PHP MySQL)

    Hi All,

    I'm trying to prevent duplicate entires in my DB for a newsletter submissions form. I'm successfully preventing the duplicate entries, but I haven't been able to revise my jQuery to print the error. I've included the PHP just incase I'm missing something there... Any help would be appreciated!

    jQuery:
    Code:
    $('#newsletterForm').validate({
        submitHandler: function(form) {
            jQuery(form).ajaxSubmit(function() {
                $('#newsletterForm').html("<div id='success-message'></div>");
                $('#success-message').html("<p><strong>Thank you.</strong> <br/>You've been added to our list and will hear from us soon!</p>");
            });
        }
    });
    PHP:
    Code:
    if(isset($_POST['submit'])) {  
    
    if(trim($_POST['email']) == '')  {  
        $hasError = true;  
    } else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {  
        $hasError = true;  
    } else {  
        $email = trim($_POST['email']);  
    }  
    
    if(!isset($hasError)) {  
        $con = mysql_connect("xxx.xxx.xxx.xxx","user","pass");
            if (!$con)
                {
                    die('Could not connect: ' . mysql_error());
                }
    
        mysql_select_db("myDB", $con);
    
        $query = "SELECT COUNT(id) AS mycount FROM newsletter WHERE email = '$email'";
        $result = mysql_query($query);
        $row = mysql_fetch_array($result);
        if($row['mycount'] == 0) {
            $query = "INSERT INTO newsletter (email,dt) VALUES ('$email',NOW())";
            mysql_query($query) or die ("Error writing to database");
        } else {
             echo "I'm sorry our database already contains that email address. Please use a new email address to continue.";
        }
    
        if (!mysql_query($sql,$con))
             {
                die('Error: ' . mysql_error());
             }
        mysql_close($con);  
    }  
    }

  2. #2
    SitePoint Zealot
    Join Date
    Sep 2006
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, full jquery:

    Code:
    $('#newsletterForm').validate({
    	submitHandler: function(form) {
    		jQuery(form).ajaxSubmit({
    				success: function() {
    					$('#newsletterForm').html("<div id='success-message'></div>");
    					$('#success-message').html("<p><strong>Thank you.</strong> <br/>You've been added to our list and will hear from us soon!</p>");
    				},
    				error: function() {
    					$('#newsletterForm').html("<div id='success-message'></div>");
    					$('#success-message').html("<p><strong>That email already exists.</strong> <br/>Please enter another email address.</p>");
    				}
    			});
    		}
    	});


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •