Skip to content

Instantly share code, notes, and snippets.

@stinoga
Created April 22, 2012 03:35
Show Gist options
  • Save stinoga/2447069 to your computer and use it in GitHub Desktop.
Save stinoga/2447069 to your computer and use it in GitHub Desktop.
Jquery Ajax Email Signup Form in Wordpress
// This function will submit the mailing list form through Ajax
// No page load necessary!
$('#addressForm').submit(function() {
$.ajax({
type: 'GET',
// This page will need setup in your wordpress admin, and you'll need to assign the ajaxServer template below. This way you have access to all the wordpress globals
url: '<?php bloginfo('url'); ?>/ajax-server-do-not-delete/',
data: $('#addressForm').serialize(),
\ success: function(response) {
$('#response').text(response);
}
});
return false;
});
<?php
/**
* Template Name: ajaxServer Page
*/
?>
<?php
// run the function to submit the form data to the database
echo(storeAddress());
?>
<h4>Subscribe</h4>
<p>Sign up for our email list to stay in the loop on the latest happenings.</p>
<p>I use the site as...</p>
<form id="addressForm" action="index.php" method="get">
<input type="radio" name="usertype" id="radFree" value="Freelancer" checked="checked"/>
<label for="radFree">Freelancer</label>
<input type="radio" name="usertype" id="radEmp" value="Employer" />
<label for="radEmp">Employer</label>
<input type="email" name="address" id="address" placeholder="email" />
<input type="submit" value="Go" class="buttonGreen" id="emailSubmit" />
<p id="response"><?php echo(storeAddress()); ?></p>
</form>
<?php
// Function to store addresses in the database to be used later for the mailing list
// This should be added to functions.php in your wordpress theme folder
function storeAddress() {
$message = "&nbsp;";
// Check for an email address in the query string
if( !isset($_GET['address']) ){
// No email address provided
}
else {
// Get email address from the query string
$address = $_GET['address'];
// Get radio selector from query string
$userType = $_GET['usertype'];
// Validate Address
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $address)) {
$message = "<strong>Error</strong>: An invalid email address was provided.";
}
else {
// Connect to database
$con = mysql_connect(DB_HOST ,DB_USER, DB_PASSWORD);
mysql_select_db(DB_NAME, $con);
// Insert email address and user type into mailinglist table
// Here is the sql to create the mailing list:
// CREATE TABLE `mailinglist` ( `id` INT NOT NULL AUTO_INCREMENT , `email` TEXT NOT NULL, `user` TEXT NOT NULL , PRIMARY KEY ( `id` ) )
$result = mysql_query("INSERT INTO mailinglist (email, user) VALUES('" . $address . "', '" . $userType . "')");
if(mysql_error()){
$message = "<strong>Error</strong>: There was an error storing your email address.";
}
else {
$message = "Thanks for signing up!";
}
}
}
return $message;
}
?>
CREATE TABLE `mailinglist` ( `id` INT NOT NULL AUTO_INCREMENT , `email` TEXT NOT NULL, `user` TEXT NOT NULL , PRIMARY KEY ( `id` ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment