Posted: October 5th, 2008 | Author: Bryan | Filed under: Coding, Tutorial | 70 Comments »
Macrobid for sale, Today I am going to speak a little bit about a quick and easy way to make a jQuery AJAX Contact Form that employs a honeypot to foil pesky email bots. You can also check out the demo here to get an idea of how the end product will act. You can use the form anywhere you like, but a courtesy link somewhere is appreciated. It is released under the Creative Commons Attribution License. Let's rundown the goals:
- A safe form with sanitation.
- Load success or error messages dynamically without leaving the page.
- Descriptive error messages detailing why the entered values failed validation.
- A honeypot to beat the bots.
That may sound like quite a tall order, but in fact, it can be put together fairly quickly with the tools that are available online. If you don't feel like learning about how to create a form and just want the form,
macrobid for sale. Go ahead and
download it here, be sure to change the html to
sendmail.php and the
email address in the same file.
Macrobid without prescription, Other wise, read on.
First, let's talk about jQuery and company.
jQuery is a popular javascript framework with powerful tools to help developers write code and develop products faster. There are a ton of popular plugins, and the one I will highlight for use today is the
jQuery Form Plugin.
Macrobid for sale, This plugin is great because by simply adding the the jQuery library and the plugin, you can AJAX'ify pretty much any form without much work. However, this results in a rather crude contact form and we can do much better with a little tweaking.
Getting started with the browser side.
First, download the
jQuery library and form plugin and then let's start creating our HTML form. Keep them all in the same folder for now,
macrobid birth control. We'll want to first call both javascript files, let's do that with:
That's a great start, but we need to make the form before we can customize our javascript to fit the form. Here is a sample form to get us started:
We have several fields in this form, one for a name, email, telephone number and message,
macrobid for sale. The field marked "last" and "Don't fill this in:" is the honey pot. The idea is that this will be hidden with CSS with the a line like this:
li.special {
display:none;
}
This should keep humans from entering any value into the field. If a value is entered (by a bot), then the PHP script will recognize this and won't send out an email.
Now that we have our form it's time to customize that javascript. Go back to the head of your html file:
Macrobid for sale, Now we have lots more code to talk about. The first bit simply says that the target div for updating dynamically with the echoed stuff from php is "#alert".
Generic name for macrobid, The last little bit says that "#contactForm" is the target of the submit. Check it our here:
$(document).ready(function() {
var options = {
target: '#alert'
};
$('#contactForm').ajaxForm(options);
});
Now the next little function simply clears all of the fields on the form, helping reduce an accidental double click of the submit button. It will be called via a PHP echo after the submit button is clicked:
$.fn.clearForm = function() {
return this.each(function() {
var type = this.type, tag = this.tagName.toLowerCase();
if (tag == 'form')
return $(':input',this).clearForm();
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = '';
else if (type == 'checkbox' || type == 'radio')
this.checked = false;
else if (tag == 'select')
this.selectedIndex = -1;
});
};
That should pretty much do it for HTML side, minus the CSS needed to format it. Make sure to download the
demo file here to get all the files and full code,
macrobid for sale. Time to get into the meat of the project and do the PHP.
Getting started with the server side.
There is quite a bit of PHP that will be require to do all this. So let's put it together in steps, let's first start by making a function to sanitize the variables for any dangerous characters,
does macrobid affect birth control. We'll use it later in when we are ready to send the email.
function clean_var($variable) {
$variable = strip_tags(stripslashes(trim(rtrim($variable))));
return $variable;
} Macrobid for sale, Now we'll want to check the "last" variable (the honeypot), if it isn't empty, we'll skip the mail function and echo a message:
if ( empty($_REQUEST['last']) ) {
//rest of code will go here.
} else {
echo "";
echo $honeypot;
}
Assuming the honeypot is empty, we now need to check the variables to see if they are formatted correctly. We'll do that by checking if their empty and if not, their format. If they fail either test, we'll return an appropriate error message and flip a variable telling the server to send the error message instead of sending the email to us. This is how we'll do that for the name:
if ( empty($_REQUEST['name']) ) { //check if its empty
$pass = 1; //flip the variable, send an error message.
$alert .= "
", macrobid for sale. Buy macrobid online, $emptyname . "
"; //this is the customized error message
} elseif ( ereg( "[][{}()*+?.^$|]", $_REQUEST['name'] ) ) { //check if it has any bad characters
$pass = 1; //flip the variable, send an error message.
$alert .= "
" . $alertname . " Macrobid for sale, "; //this is another, different customized error message
}
Now a similar format is followed for the rest of the variables, with different validation checks:
if ( empty($_REQUEST['name']) ) {
$pass = 1;
$alert .= "
" . $emptyname . "
";
} elseif ( ereg( "[][{}()*+?.^$|]", $_REQUEST['name'] ) ) {
$pass = 1;
$alert .= "
" . $alertname . "
";
}
if ( empty($_REQUEST['email']) ) {
$pass = 1;
$alert .= "
", buy macrobid without prescription. $emptyemail, macrobid for sale. "
";
} elseif ( !eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_REQUEST['email']) ) {
$pass = 1;
$alert .= "
" . $alertemail . "
";
}
if ( empty($_REQUEST['tele']) ) {
$pass = 1;
$alert .= "
" . $emptytele . " Macrobid for sale, ";
} elseif ( !ereg( "(?[0-9]{3})?[-. ]?[0-9]{3}[-. ]?[0-9]{4}", $_REQUEST['tele'] ) ) {
$pass = 1;
$alert .= "
" . Buy macrobid, $alerttele . "
";
}
if ( empty($_REQUEST['message']) ) {
$pass = 1;
$alert .= "
" . $emptymessage, macrobid for sale. "
";
} elseif ( ereg( "[][{}()*+?^$|]", $_REQUEST['message'] ) ) {
$pass = 1;
$alert .= "
" . $alertmessage . "
";
}
I think you get the idea. Each error message is encapsulated in a list tag and it appended to the $alert variable.
Macrobid for sale, By the end of the run, $alert should contain a string of all the appropriate (if any) error messages which we can then relay to the user. We do that by checking to see if the $pass variable has flipped:
if ( $pass==1 ) {
echo ""; //js for the error message effect
echo "" . $errormessage . "";
echo "
"; //starting the list
echo $alert; //echo the error messages
echo "
"; //end the list
}
Now, for the final addition,
buy macrobid without prescription, let's send that email. We'll want to use the "mail" function in PHP to do this because it's simple and quick. We'll employ the "clean_var" function to sanitize the user entered data,
macrobid for sale. Here it is:
///this is earlier error code
} elseif (isset($_REQUEST['message'])) {
$message = "From: " . clean_var($_REQUEST['name']) . "n";
$message .= "Email: " . clean_var($_REQUEST['email']) . Macrobid for sale, "n";
$message .= "Telephone: " . clean_var($_REQUEST['tele']) . "n";
$message .= "Message: n" . clean_var($_REQUEST['message']);
$header = 'From:'. Macrobid for sale, clean_var($_REQUEST['email']);
mail($sendto, $subject, $message, $header);
echo "";
echo $thanks;
die();
}
That should just about wrap it up besides for the variables we should set at the beginning of the code. Here they are in all their glory:
// Who you want to recieve the emails from the form, macrobid for sale. (Hint: generally you.)
$sendto = 'youremail@example.com';
// The subject you'll see in your inbox
$subject = 'Contact from contact form';
// Message if there isn't a mail function
$mailerror = "Oops. It seems that the server can't mail this. Try emailing us at " . $sendto . Macrobid for sale, " instead.";
// Message for the user when he/she fills in the form correctly.
$thanks = "Thanks for the email. We'll get back to you as soon as possible!";
// Message for the bot when it fills in in at all.
$honeypot = "You filled in the honeypot, generic name for macrobid. If you're human, try again!";
// Various messages displayed when the fields are empty.
$emptyname = 'Entering your name?';
$emptyemail = 'Entering your email address?';
$emptytele = 'Entering your telephone number?';
$emptymessage = 'Entering a message?';
// Various messages displayed when the fields are incorrectly formatted, macrobid for sale.
$alertname = 'Entering your name using only the standard alphabet?';
$alertemail = 'Entering your email in this format: name@example.com?';
$alerttele = 'Entering your telephone number in this format: 555-555-5555?';
$alertmessage = "Making sure you aren't using any parenthesis or other escaping characters in the message. Most URLS are fine though!";
$alert = ''; // Got to call it first in order to append the error messages
$pass = 0; // Default is pass through, errors will flip
Phew. Now that was a lot of code. If this is too much to deal with right, just
download the zipped file and get all of the code, html and CSS together.
If you catch any error or bloopers, please let me know. I stuck this together really quickly and I am bound to make mistakes. Thanks guys.
Similar posts: Does macrobid affect birth control. Buy macrobid online. Buy macrobid without prescription. Generic name for macrobid. Uses for macrobid. Macrobid without prescription. Buy macrobid. Macrobid birth control. Generic for macrobid. Macrobid 100mg. Macrobid without prescription. Buy macrobid online. Uses for macrobid. Does macrobid affect birth control. Buy macrobid online. Generic for macrobid. Does macrobid affect birth control. Macrobid for sale. Macrobid for sale.
Trackbacks from: Macrobid for sale. Macrobid for sale. Macrobid for sale. Macrobid for sale. Macrobid for sale. Generic for macrobid. Uses for macrobid. Buy macrobid without prescription. Buy macrobid. Buy macrobid.