How to Add a Form in a WordPress Post

This is a simple step by step guide to creating a form that will capture data and store it in a mysql database on your server from within a wordpress post

Note: the below example is a live, working form on this page

Step 1:

Create a simple form for a petition, contact request, or registration:


Here is the html code for it:

<tr><th>Name:</th><td><input id="userName" /></td></tr>
<tr><th>Address:</th><td> <input id="userAddress" /></td></tr>
<tr><th>City:</th><td> <input id="userCity" /></td></tr>
<tr><th>State:</th><td> <input id="userState> /></td></tr>
<tr><th>Zip:</th><td> <input id="userZip" /></td></tr>
<tr><th>Email:</th><td> <input id="userEmail" /></td></tr>
<tr><th>Comment:</th><td> <textarea id="userComment" ></textarea></td></tr>
<tr><th><td colspan=2><button type="button" onclick="saveUser();">Save</button></td></tr>

Step 2:

Create a javascript function to process the form and send it to the server. The following is a javascript function added to the page using the CSS & Javascript Toolbox plugin (it uses jquery because wordpress already has that available):

//Process the form and save the data record
function saveUser() {
  //First gather the form parameters and make sure name and email at least are populated
  var name = jQuery("#userName").val();
  var address = jQuery("#userAddress").val();
  var city = jQuery("#userCity").val();
  var state = jQuery("#userState").val();
  var zip = jQuery("#userZip").val();
  var email = jQuery("#userEmail").val();
  var comment = jQuery("#userComment").val();
  if (name.length<1 || email.length<1) {
    alert("Please at least enter your name and email.");
    return false;
  } else {
    //Now send the data to a server side function to really validate it and save it.
      type: "POST",
      url: "/ajax/saveUser.php",
      data: { name:name,address:address,city:city,state:state,zip:zip,email:email,comment:comment }
    }).done(function( results ) {
      if(results.length<1){ // network error
        alert("There was a network error, please try again or contact support and tell them what you are trying to do.");
      } else { // this is a successful interaction
        var resultObj = jQuery.parseJSON(results);
        if (resultObj.errorMsg.length>0) {  
        } else {
    	  //Record save successful
          alert("Thanks for your information, it was saved successfully!");
          //Show the user what they have entered:

Step 3:

Make sure you have a database table ready to store the information. Below is a simple table used to store the info in this example:

  `userName` varchar(80) DEFAULT NULL,
  `address` varchar(100) DEFAULT NULL,
  `city` varchar(80) DEFAULT NULL,
  `state` varchar(40) DEFAULT NULL,
  `zip` varchar(5) DEFAULT NULL,
  `email` varchar(80) DEFAULT NULL,
  `comment` text DEFAULT NULL,
  PRIMARY KEY (`userID`),
  KEY `userIP` (`userIP`)

Step 4:

Create the server side code to accept the form data, really validate it, and put it in the database or send back an error. Here is the contents of a server side code which will do all that for the example data above. Since this form is “in the wild”, I’m capturing the IP address because I will only show results to people that they entered themselves:

 * File: saveUser.php
 * Description: This file takes the passed in user information and validates it before
 * 	saving it to the database and returning content to show on the page. 
 * Inputs (in POST): 
 * 	userName, address, city, state, zip, email, comment
 * Outputs:
 * 	either an error or nothing
include("db.php"); // define your database credentials in another file

#connect to the database
$db = mysqli_connect($servername, $username, $password, $dbname);
mysqli_select_db($db,$dbname) or die('Could not select database');

#Get the passed in parameters into local variables, escaped for database input
$userName = empty($_POST['userName'])?'':addslashes($_POST['userName']);
$address = empty($_POST['address'])?'':addslashes($_POST['address']);
$city = empty($_POST['city'])?'':addslashes($_POST['city']);
$state = empty($_POST['state'])?'':addslashes($_POST['state']);
//only accept 5 numbers for zip
$zip = empty($_POST['zip'])?'':substr(preg_replace('/\D/','',$_POST['zip']),0,5);
$email = empty($_POST['email'])?'':addslashes($_POST['email']);
$comment = empty($_POST['comment'])?'':addslashes($_POST['comment']);

#This is an array used for gathering all the outputs of this file
$jsonObj = array();
$jsonObj['errorMsg'] = "";

#Validate inputs
if (empty($userName) or empty($email)) {
	$jsonObj['errorMsg'] = "Please at least enter your name and email.";
} else if (strpos($email,'@')===false) {
	//there are many more validations that can be made for emails but this is a start
	$jsonObj['errorMsg'] = "Please enter a valid email.";

#Enter the data record
if (empty($jsonObj['errorMsg'])) {	
	$sql = "insert into user_info (userName,address,city,state,zip,email,comment,userIP) 
	if ($debug) echo "about to run $sql ";
	mysqli_query($db, $sql); $err = mysqli_error($db); if (!empty($err)) $jsonObj['errorMsg'] = $err;

#Now get the list of data entered by this IP, without any script tags to prevent XSS:
if (empty($jsonObj['errorMsg'])) {	
	$sql = "select userName,city,state from user_info where userIP='".$userIP."'";
	$rs = mysqli_query($db, $sql); $err = mysqli_error($db); if (!empty($err)) $jsonObj['errorMsg'] = $err;
	while($row = mysqli_fetch_assoc($rs)) {
		$jsonObj['userList'].= "<tr><td>".strip_tags($row['userName'])."</td><td>".strip_tags($row['city']).

#Now send back the data in json format
echo json_encode($jsonObj);

Step 5:

Do something with the results! You could export them, feed them into another system using an API, send them in an email to someone, or just display them like so:

Name City State
You have not entered any user records yet, try it out!

Google Maps API Version 3 Geocoding Example

Today I realized that my location markers were not being displayed in all the google maps implementations I had put into place online, and after digging into it (trying to figure out what a code 610 response meant) I realized that I was using version 2 of the google maps api for retrieving latitude and longitude based on a given address (ie “geocoding”).

My actual maps were being displayed using version 3 of the API but the example I found a couple years ago and followed for geocoding used version 2, which was deprecated and will no longer be supported sometime in 2013 (I’ve heard both March and September, but since my code isn’t working in April I suppose the March date was more accurate).

Since I wasn’t able to find an example for just what I needed (getting the latitude and longitude based on an address, so I could show markers on a google map) I read through the docs and updated my code.  See below for my implementation (I use this as an included file wherever I need to pull latitude/longitude, it could be put into a function if you want). The address, city, state, and zip should be stored in the $address, $city, $state, and $zip params, and I am also saving the latitude and longitude in both a local database and a markers array to be used in a google map later:

 * File: incGetGeoCodes.php
 * Description: this file pulls the lat/long coordinates using Version 3 of the google maps api
 * 	The key parameter caused it to fail so I removed that and it works, but we may be under a 
 * 	lower daily usage limit because of that
// Initialize delay in geocode speed
$delay = 0;

$geoaddress = $address . ", " . $city . ", " . $state . " " . $zip;
$request_url = $base_url . "&amp;address=" . urlencode($geoaddress);

$status = $resultArr['status'];
if (strcmp($status, "OK") == 0) {
	// Successful geocode
	$markers[$shop_id]['lat'] = $resultArr['results'][0]['geometry']['location']['lat'];
	$markers[$shop_id]['long'] = $resultArr['results'][0]['geometry']['location']['lng'];
	$markers[$shop_id]['html'] = $name."<br>".$address."<br>".$city." ".$state." ".$zip."<br>".$phone;
	#Now update the db so we don't have to pull this again
	$query = "update entities ".
		"set shop_latitude=".addslashes($markers[$shop_id]['lat']).", ".
		"shop_longitude=".addslashes($markers[$shop_id]['long'])." ".
		"where EntityID=".intval($EntityID);
	mysql_query($query); $err=mysql_error(); if (!empty($err)) echo "query:$query, error: ".$err."<br>";
} else if (strcmp($status, "620") == 0) {
	// sent geocodes too fast
	$delay += 100000;
} else {
	// failure to geocode
	$error .= urlencode("Address " . $geoaddress . " failed to be geocoded. ");
	$error .= urlencode("Received status " . $status . "%0D%0A");

I was surprised to see no mention of an API key parameter to be used, if someone knows if that is an option (to increase the daily quota of geocoding api calls that can be made) please leave a comment and let me know. I’m just happy to get it working again for now. 🙂

How to integrate existing website into Amazon Simple Email Service

I was preparing to integrate a client’s business to send emails through AWeber last week when I realized that their api does not support sending transactional emails to one or more people on an as needed basis. I confirmed with AWeber’s tech support that their API is read only, it does not allow the sending of emails at all (they have a web interface for sending emails). I asked them what they would use in my situation and they said that the other big newsletter names I had heard of before (MailChimp, Constant Contact, etc.) also only supported newsletter type messages.

What my client needed was a more robust email system because every week they had situations where people were not getting emails (sent from their own server using the php mail command). I recommended AWeber because I knew their deliverability was very high and they would keep up with any changes in email standards. I figured since they had an API that I could specify email addresses and email contents to send using it but after looking for that functionality I came up empty handed.

The Aweber tech I spoke to mentioned the possibility of using SalesForce for this type of account message emailing, but I knew that would be overkill and overpriced for just sending emails. After a quick search I was happy to find out that Amazon provides an email service called “Simple Email Service” (SES) that allows for a certain number of emails for free if you already have an Amazon Web Services (AWS) account. Since my client had signed up for the Amazon S3 service (a storage solution) a few months prior in order to save backups of their weblogs they already had an AWS account.

After reading a few documents about different ways to use the Amazon Simple Email Service I decided that it would be simplest for me to integrate using an authenticated SMTP connection. Since there were only half a dozen files that use the mail command (found by using ‘grep “mail(” *php’ and ‘grep “mail(” *\*php’ and so on in the webroot), I only needed to update those files after getting the Pear modules installed.

I started using just the pear Mail module but then when I tried to send html emails they showed up as source code and not rendered, so I added the Mail_Mime files too. The way I did it was to first try installing it using “pear install Mail” (and pear install Mail_Mime) as root, but after a bunch of trouble with include paths I ended up downloading the tarballs to the server using wget, then extracting them into a subdirectory under the webroot (which I later protected using htaccess to deny all connections directly to the files). Next I tried including the main Mail.php file with error printing on and updated several of the pear files to refer to the right relative path for inclusion. I did the same thing with the Mail/mime.php file, adjusting paths as needed until the errors were all gone.

I had a common included file at the top of each of my php files so inside that common file I included the pear files and defined a few constants for connecting to the smtp server at amazon (pear is the name of the folder in my webroot where I put the pear files):

#Show errors - after I got the paths right I commented this section
ini_set('display_errors', true);
ini_set('html_errors', true);

#Pear email modules
require_once "pear/Mail.php";
require_once "pear/Mail/mime.php";

Then in each file where I wanted to send email, I used this framework to do it:

# Constructing the email
$sender = "Sender Name <>";                              
$recipient = ";                           // The Recipients name and email address
$text = "this will be sent as text;                                  // Text version of the email
$html = "<h1>This will be rendered as html</h1>";  // HTML version of the email
$crlf = "\n";
$headers = array(
        'From'          => $sender,
        'To'          => $recipient,
        'Subject'       => $subject

# Creating the Mime message
$mime = new Mail_mime($crlf);

# Setting the body of the email
if (!empty($text)) {
} else if (!empty($html)){

#Get the header and body into the right format
$body = $mime->get();
$headers = $mime->headers($headers);
$headers['From'] = $sender;  //I heard some people had trouble with this header getting messed up

#Setup the connection parameters to connect to Amazon SES
$smtp_params["host"]     = MAILHOST;
$smtp_params["port"]     = MAILPORT;
$smtp_params["auth"]     = true;
$smtp_params["username"] = MAILUSER;
$smtp_params["password"] = MAILPWD;
# Sending the email using smtp
$mail =&amp;amp; Mail::factory("smtp", $smtp_params);
$result = $mail->send($recipient, $headers, $body);		

#Below is only used for debugging until you get it working
if (PEAR::isError($result)) {
   echo("<p>" . $result->getMessage() . "</p>");
} else {
   echo("<p>Message successfully sent!</p>");


Amazon doesn’t put your emails into a queue if you send them too fast, so in order to stay under their sending limits when sending batches of messages you can use the php usleep command to delay execution. I found that this delay didn’t actually work until I added “set_time_limit(0);” to the top of the file sending the batch of emails, however. Test everything, different server environments will respond differently (just like Browsers are like Churches). I used an echo date(‘h:i:s’) command between delays to see whether the delay worked or not.

How to use Ajax in a WordPress Post

I recently put together an Ajax autocomplete example using PHP and Mysql so I could post it on my blog, but after getting it working outside wordpress on an independent page I had trouble getting it into wordpress.

I had used plugins in the past which allowed for php code to be used with shortcodes, but inline javascript calls are not something I had gotten past the wordpress editor before. The php code used for retrieving data from the database is in a separate file so no shortcode is needed for that portion.

The inclusion of the javascript autosuggest functions is something I first tried putting in a code block in the wordpress post itself as a link to the external file (autosuggest.js), but unfortunately that didn’t work. Then I saw an article where someone suggested putting the javascript in the theme file header.php and surround it with a php if statement checking for the specific post you want it to appear on. I tried that but later removed it because I wanted my ajax to work on the index page, archive pages, and category/tag/author pages – not just the “single post” page.

Then I went back to the plugin I had installed (CSS & JavaScript Toolbox) for css and javascript and tried putting the entire contents of the autosuggest.js file into one javascript block and installing that in the footer. That worked perfectly so I did the same thing for the css file using the same plugin.

The next challenge was to add the inline JavaScript (ajax) function calls from within the html code for the sample input box in the wordpress post. When I tried just putting it all into the html editor for wordpress the javascript was removed. This was not just a snippet of complete javascript or css though, so I couldn’t just use the same plugin to make another code block and put it in the header or footer.

Since PHP is executed as the page loads, I figured I could put my auto-complete input box with ajax call into a php shortcode and put the shortcode where I wanted the input box to be in my html code. That way wordpress would not filter out the javascript when saving the post contents to the database because it would be hidden behind a shortcode which would be replaced during page loading.

This worked beatifully, including the inline javascript by putting it inside a php shortcode. I used the Shortcode Exec PHP plugin for this, which is very easy to work with.

Allow Email as Username within WP-Members Plugin

Today I figured out how to let people use their email to login instead of their username to the WordPress driven site called Jacked Pack. I had already installed the WP-Members plugin in order to add custom registration fields to the WordPress account page (for the “HUGEness profile”), but when I tried installing the WP Email Login plugin it didn’t have any effect. I figured this was because the WP-Members plugin already supercedes the regular login functionality, so I asked Chad Butler (who created the WP-Members plugin) about the issue.

Fortunately Chad had already created a way for people to add plugins to his plugin, so that’s what I ended up doing in this case. I followed his instructions by creating a file called wp-members-pluggable.php in my /wp-content/plugins directory and copying the wpmem-login function into it (I found that function in the wp-members-core.php file in the wp-members plugin folder). Then I added my own simple function (which was taken from the WP Email Login plugin) to get the username from an email address in case someone enters an email in the username field. Then I added just one line to the wpmem-login function which calls my function. Once I got that working, I copied the wpmem_inc_login function (which I found in the wp-members-dialogs.php file in the wp-members plugin folder) into my wp-members-pluggable.php file and changed one line to tell people they can use Username or Email in the first field.

See below for my complete plugin for the WP-Members WordPress Plugin, or download it here:

 * WP-Members Pluggable Functions
 * These functions replace those in the wp-members plugin

if( ! function_exists( 'wpmem_login' ) ):
 * Logs in the user
 * Logs in the the user using wp_signon (since 2.5.2). If login 
 * is successful, it redirects and exits; otherwise "loginfailed"
 * is returned.
 * @since 0.1
 * @uses apply_filters Calls 'wpmem_login_redirect' hook to get $redirect_to
 * @uses wp_signon
 * @uses wp_redirect Redirects to $redirect_to if login is successful
 * @return string Returns "loginfailed" if the login fails
function wpmem_login()
	if( isset( $_POST['redirect_to'] ) ) {
		$redirect_to = $_POST['redirect_to'];
	} else {
		$redirect_to = $_SERVER['PHP_SELF'];
	$redirect_to = apply_filters( 'wpmem_login_redirect', $redirect_to );

	if( isset( $_POST['rememberme'] ) == 'forever' ) {
		$rememberme = true;
	} else {
		$rememberme = false;

	if( $_POST['log'] &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; $_POST['pwd'] ) {
		$user_login = sanitize_user( $_POST['log'] );

		$user_login = wpmem_login_check_for_email($user_login);
		$creds = array();
		$creds['user_login']    = $user_login;
		$creds['user_password'] = $_POST['pwd'];
		$creds['remember']      = $rememberme;
		$user = wp_signon( $creds, false );

		if( ! is_wp_error( $user ) ) {
			if( ! $using_cookie )
				wp_setcookie( $user_login, $user_pass, false, '', '', $rememberme );
			wp_redirect( $redirect_to );
		} else {
			return "loginfailed";
	} else {
		//login failed
		return "loginfailed";

} // end of login function

if ( ! function_exists( 'wpmem_login_check_for_email' ) ):
 * Get Username from Email
 * Takes the username and checks if there is a user with an email that matches.
 * 	If there is, the username associated with the email is returned.
 * 	Otherwise the username is returned as it was passed in.
 * @uses get_user_by
 * @return string Returns username associated with email or the same
 * 	username that was passed in
function wpmem_login_check_for_email($username) {
	if ( !empty( $username ) )
		$user = get_user_by( 'email', $username );
	if ( isset( $user->user_login, $user ) )
		$username = $user->user_login;
	return $username;
} // end of check for email in the username

if ( ! function_exists( 'wpmem_inc_login' ) ):
 * Login Dialog
 * Loads the login form for user login
 * @since 1.8
 * @uses apply_filters Calls wpmem_restricted_msg filters message content
 * @uses wpmem_login_form()
 * @param string $page
 * @return string the generated html for the login form
function wpmem_inc_login( $page="page" )
	global $wpmem_regchk;

	$str = '';
	$arr = get_option( 'wpmembers_dialogs' );

	if( $page == "page" ){
	     if( $wpmem_regchk!="success" ){
			// this shown above blocked content
			$str = '<p>' . stripslashes($arr[0]) . '</p>';
			// filter blocked content message
			$str = apply_filters( 'wpmem_restricted_msg', $str );


    //$arr = array( __( 'Existing users Login', 'wp-members' ), __( 'Username', 'wp-members' ), 'text', 'log', __( 'Password', 'wp-members' ), 'password', 'pwd', 'login', __( 'Login', 'wp-members' ), 'username', 'password' );
	$arr = array( __( 'Existing users Login', 'wp-members' ), __( 'Username or Email', 'wp-members' ), 'text', 'log', __( 'Password', 'wp-members' ), 'password', 'pwd', 'login', __( 'Login', 'wp-members' ), 'username', 'password' );
	$str = $str . wpmem_login_form( $page, $arr );
	return $str;

Update July 5th, 2013

I was asked about the registration function as well (see comments from Brian Weiss below) and although I haven’t made a plugin for the WP-Members registration function I have done auto registrations in a separate file. I tried posting code in a comment and it didn’t turn out well, so here is the relevant code I used (in a separate php file I created in the wordpress directory) to auto create wordpress accounts based on their email and firstname (you could do it without using their name if you want, since the users will never need to know their username). If someone puts this into a plugin for the WP-Members registration function please let me know and I’ll link to it so others who want to do that can see the solution.

#Initial try at an unclaimed username
$username = $firstName.rand(1,1000);

#Now create a WP account

// Initialize connection and select database
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
    or die('Could not connect: ' . mysql_error());
//echo 'Connected successfully';
mysql_select_db(DB_NAME) or die('Could not select database');
//echo "connected to db<br>";

#WP Username has to be unique
while (username_exists( $username )) {
	$username = $firstName.rand(1,1000);
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
//echo "about to create user $username with pwd $random_password and email $email<br>";
$user_id = wp_create_user( $username, $random_password, $email );
if (is_int($user_id)) {
	//echo "new user id is $user_id<br>";
} else {
    if(is_wp_error($user_id)) { echo $user_id->get_error_message(); }
// then send a welcome email and/or redirect wherever you want...

How to Transfer a Folder Full of Files and Sub-Directories to an FTP Server

Today I had to figure out how to transfer 65GB of data (including dozens of files in separate subfolders) from a linux server (where it was stored after being pulled from a FilesAnywhere account using a recursive wget command, “wget -r -l 0”) to another storage account at

I first tried tarring up all the files and folders and transferring that as one file, but I couldn’t get the support people at egnyte to untar it for me. So I began creating a perl script to do this task. After awhile of reading about how to make multidimensional arrays in perl for the file structure (technically this is not possible, but interacting with arrays of references in perl is very similar to multidimensional arrays in php) I decided to look for a simpler solution.

What I found was a CPAN module that allows for recursive gets and puts, (rget and rput, respectively). The details of the module are described at So after a bunch of trial and error I came up with the following script, which you can download here or copy from below.

All you need to do (once you have perl and the Net::FTP::Recursive cpan module installed-see links below for help with that) is put your ftp credentials into the file, put the perl script in the directory where you have files and folders to transfer (I actually put the perl file in the folder above where I wanted to transfer from so I wouldn’t transfer the perl file too), then execute the file by typing ‘perl “”‘ (or in my case I typed perl ../ “”).

# Filename: - this script will recursively transfer files in a directory tree to an ftp server
# Inputs:
#	$host is an ip address of the ftp server to connect to
# Configurable parameters:
#	$user is the ftp username
#	$password is the ftp password
#	$remoteDirectory to transfer files and folders of files to (defaults to top level directory)
# Example usages:
#	perl ""
#	perl ""
use strict;
use Net::FTP::Recursive;
use FileHandle;

my $numParams = scalar(@ARGV);
if ($numParams<1) {
	print "Usage:\n";
	print 'perl "ftphost"'."\n";
	print "ftphost is an ip address or domain name of the ftp server to connect to\n";

my $host=$ARGV[0];
my $user='ftpuser'; #ftp username, use single quotes so special characters are not interpreted
my $password='ftppassword'; #ftp password, use single quotes so special characters are not interpreted
my $remoteBaseDir='/path/to/sub/directory'; #ftp remote base directory where to put all the files under

my $ftp = Net::FTP::Recursive->new("$host", Debug => 1); #use Debug=>0 if you dont want to see what is happening or detailed error messages
if (length($remoteBaseDir)>0) {
print "Done transferring files\n";

In order to install the Net::FTP::Recursive CPAN module you can follow the instructions at Alternatively, if you don’t even have CPAN yet you can start with

Simple Ajax Auto-Complete Example

In a previous post I wrote about how I learned ajax quickly, but recently I learned that the main link I learned from is no longer valid. So I decided to make a simple example I could host myself. Along the way I learned that it is not trivial to include ajax within a wordpress page, but I wrote another post about how I figured that out. 🙂

The input box below is enabled with auto-complete using a simple ajax example. The javascript can be seen here, the css can be seen here, and the php ajax function can be seen here. The input box below calls the getScriptPage javascript function if there are at least 2 characters present. That javascript function calls the php ajax function using a server request. The text sent back from the server by the php file is then handled by the handleHttpResponse javascript function, which just populates a hidden div with choices. The getScriptPage function makes that hidden div (with an id of “box”) visible if the response from the php ajax call is not empty.

The database table being queried has 1000 rows in it, in the following format: name100 (where 100 is the number of the record, 1-1000). So if you start typing “name” in the box, after na you will start seeing 5 suggestions below. If you keep typing the suggestions will be updated as you type.

HTML code for input box

<div id="headersearch">
	<div id="searchbar">
		<div class="ajax-div">
			<div class="input-div">
			<input type="text" autocomplete="off" onKeyUp="if(this.value.length>1) 
				getScriptPage('box','searchTerm',''); else box(0);" 
				name="searchTerm" value="Enter Name Here" onClick="if(this.value.substr(0,6)=='Enter ') this.value=''"
				id="searchTerm" onBlur="if(this.value.length<1) this.value='Enter Name Here'">
			<div id="box"></div>

April 24th 2014 Update:

I was asked to add arrow navigation to an autosuggestion box I created this week using the method described above, and found that there are easier ways to do this. I found that jquery UI has an autocomplete widget which can be used to enable arrow key navigation among many other features. However I had trouble getting the examples at to work, so I did some more searching and found a step by step implementation using a php file for the source element of the autocomplete function which I was able to get working pretty easily, at

Browsers are like Churches

Last night one of my clients asked me why the same code can be interpreted so differently by different browsers. Though I agreed as I pointed out the long standing rift between Internet Explorer and most of the others, I couldn’t really explain why. But after sleeping on it I think I have a decent analogy.

The same text (or scripture) can be interpreted to mean very different things when you go to two different churches, even within the same religion. In the same way, identical html/css text can mean entirely different things when you view it through the window of one browser vs another, even on the same computer.

Yes there are commonalities between churches and across browsers. Many churches even across religions will agree that stealing is not acceptable. Just like many browsers, even across operating systems, will display an error if a requested file is not found.

However, some churches and browsers are more lenient than others in the “grey areas”. For example, certain churches accept alternate lifestyles and others exclude those who participate in them. Internet Explorer (IE) is very strict in it’s adherence to the rules. If an ending div tag is not specified IE shows no mercy, not even a helpful error message. Firefox and Chrome are more focused on the what is “in the heart”, often overlooking minor offenses and sometimes even providing helpful guidance in returning to the right path when mistakes are made.

Some churches provide free services that empower people to change. They run programs for helping people to overcome addictions and live a more fulfilling life. Some browsers like Firefox have free plugins like Firebug and Web Developer which help its users to overcome bad habits and understand how to bring their code to life.

At the end of the day, we are all in this World Wide Web together, no matter which browser we use or which church we go to. Whether online or offline the understanding and acceptance of diversity allows us to thrive while creating and enjoying wonderful experiences. 🙂

How to Easily Prepare Database Import Using Perl

I’m revisiting this topic because in my last post about translating Excel data into a text file for database import I used a php script and yet still had some manual steps (the one that bugged me most was adding custom line termination characters for multiline data). I mentioned the idea that if I wanted to put more time in I could use a Perl script to do the whole process. Well my next set of data I needed to import was almost 700 records spanning almost ten times as many lines in the tab delimited text file I exported from Excel (due to the description fields having newline characters). So here’s my latest solution:

  1. First I added a numeric column in the left-most position (within excel) and filled it with incremental integers.
  2. Then I exported the spreadsheet as tab delimited text
  3. Then I wrote a perl script to loop through the file and add line termination characters before each new record, this time I had to use double equal signs (==) because the data contained single equal signs
  4. I also found a perl module similar to the php htmlentities() function which actually worked for me, as opposed to the php function which I abandoned in favor of hard-coded string replacement commands
  5. Then I ran the perl script on my tab delimited text file to create a tab delimited file with double equal signs as the line terminator and all the funny characters encoded so they would show up correctly in a browser
  6. Finally I imported my file using the load data infile option in the phpmyadmin interface, specifying the delimiter and line termination as \t and == respectively.
  7. Then I looked at a webpage which pulled that data and it looked like it did in the excel file!

Here is the code for the perl script I created:

# Filename: - this script will take a list of data 
#	including multiline data where the first field is a line count.
#	It will add == before each line count to enable mysql importing and 
#	will encode html entities and convert newline characters to <br>
use HTML::Entities;

my $input = $ARGV[0]; #this is the file to parse
my $output = $ARGV[1]; #this is the file to create
my $lineTerminator = "==";
my $count=1;
my $line = '';
open (IN,$input) or die "can't open file $input\n";  
open (OUT, ">$output") or die "Can't open outfile : $output\n";
while (<IN>) {	# each line of the current input file
	$line .= "<br>"; # include this here so the <br> will be encoded too
	$line = encode_entities($_); #This is the function which encodes html symbols
	# Check if this is the beginning of a new record.  If so then add the line terminator
	if ($line =~ /^$count/) {
		$line = $lineTerminator.$line;
	print OUT $line;
close IN;
close OUT;
print "there were $count records processed.\n";

The syntax for running the script (assuming perl is installed on your system – I use activeperl on my PC which is free):

perl inputFileName.txt outputFileName.txt

How to Export One Table Structure in Mysql and Infobright

I have a database with tables in it which have over 300 fields each. I wanted to create a new table like the others but I didn’t have the table structure saved in a file. Due to the number of fields it was impractical to copy the screen after executing a “show create table tablename” command in that database, so I had to find another way.

I found that I could use the mysqldump command to export just the table structure, and this command would probably have worked if my database engine was regular mysql:

# /usr/local/infobright/bin/mysqldump -d -u adminUserName -p'adminPassword' dbName tableName > file.sql
mysqldump: Got error: 1031: Table storage engine for 'BRIGHTHOUSE' doesn't have this option when doing LOCK TABLES

So I looked up how to avoid the lock tables error and found the single transaction flag which made it work in Infobright (which is based on mysql but has some differences):

# /usr/local/infobright/bin/mysqldump -d -u adminUserName -p'adminPassword' --single-transaction dbName tableName > file.sql