Thursday, January 29, 2009

Convert an XTHML form to a JSON object, then use jQuery's AJAX methods to send the data to the backend

I had a project at work that almost drove me insane tonight. When it comes to Javascript, forget what you know about other languages, and get in the mode of: "all objects are hashes".

Case Scenario:


You have a form with lots of dynamically created input fields. Instead of a normal submit button post, you want to send the data to the backend using AJAX.

Problem:


After hours of Googling, you just can't find information on dynamically populating a JSON object. You also want a clean and simple way to grab all that form data, put it in a JSON object, and then submit it with jQuery's wonderfully simple AJAX methods.

Example Solution:


XHTML from a dynamically generated form
I left some code out for simplicity, but just imagine it's there. The inputs below are added dynamically, and can be infinite.

<form id="invite_users">
<fieldset>
<input name="users[0][name]" type="text">
<input name="users[0][email]" type="text">
</fieldset>
<fieldset>
<input name="users[1][name]" type="text">
<input name="users[1][email]" type="text">
</fieldset>
<fieldset>
<input name="users[1][name]" type="text">
<input name="users[1][email]" type="text">
</fieldset>


<!-- ...etc...etc.. -->

<a href="javascript:void(null)" id="#submit_form">Submit</a>
</form>

jQuery Goodness

Here is the sweet part. In this example, we just iterate through all the input fields dynamically, and place them in a JSON object -- which is what jQuery's ajax method uses to send the data back to the server.


$(document).ready(
function() {
$("#submit_form").click(function() {
var data = {}

$("#invite_users > fielset > input").each(function() {
data[$(this).attr("name")] = $(this).val();
});

$.post("post_form.php", data, function(returnData) {
alert("Thank You!");
});
});
}
);


Wallah! There you have it. I found this to be very clean for my purposes, and am contemplating writing a jQuery plugin for it - though, I'm not really sure I have the time.

Hope this helps others out there! Leave a comment on your thoughts!

Monday, December 8, 2008

Latest Happenings!

So I haven't updated this blog in at least 6 months, and it's been a very busy time indeed.

So first things first, I got married May 23rd to the beautiful Amber Ellifritz, now known as the beautiful Mrs. Amber Page ( us on the right ). It's been a wild and wonderful ride. We are now expecting! She's 4 1/2 months at this point. We find out the gender in 2 days, then I plan on throwing a Baby Blue Victory party between now and Christmas ;-)

It's clear I want a boy.

Shortly after we married, we moved to Charles Town, WV. From there, I picked up remote work and since have churned out FirstString.com - a social network for high school athletes. This has been an exciting project, we started it in Typhoon PHP, the www.shockwavelabs.com Framework, and a good 80% of it is still written in that framework, we are now writing new components in Ruby on Rails, which I've grown to adore. It's not perfect, but it is feature rich and gets the job done.

Speaking of Ruby, it presented a learning curve. Below is a picture of depicting said learning curve:



As you can see. It was easy. I recommend everybody give it a shot.

[ ... ok my break is up, I need to go back to some projects, but I'll finish writing on this post at a later time ]

Saturday, May 3, 2008

Sprint Rumor LG, Drunkguy.com's Wedding, and a new job!

It's been a hectic few weeks, and I've hardly had extra time to keep up with my blog. While I'm supposed to be taking a Saturday away from the computer, I couldn't help myself this evening.

This afternoon I decided it was time to upgrade my phone. I've had the same phone for almost 5 years now. Needless to say, the upgrade was *free* -- it would have been completely free, but I tossed in the 2 year warranty, which scored me a nifty belt strap-able case and blue tooth ear piece to boot.

I went with Sprint's Rumor by LG. I didn't go all out like you'd expect a technology buff to do, but I'm satisfied with it. It's a sleek little phone with a 1.3 mega pixel camera/video recorder, a slide out keyboard, a nice interface and some pretty lights.

I also went to Matt O'Donnell's wedding, which was absolutely beautiful! I picked up my wonderful girlfriend, Amber, in PA, drove god knows how many hours to Virginia Beach, then spent 3 amazing days at a beach house drinking it up with the guests and man/woman of honor. The only downside was the driving... on the way home we got a bit lost in D.C., which was a nightmare to get out of -- though we saw an amazing hotel that we're definitely going to make a trip to sometime.

Final report is I picked up a second part time job. It's another startup! You know me! I feel they have a GREAT idea and the fact that they have some venture capital to back it really helps. So, now I'm officially a lead architect for 3 web based startups, hence the long delays in between blog posts... What do you think? 6 figure income, 1 year? 2? Wish me luck!

More to come, stay tuned kiddos.

Tuesday, April 8, 2008

Social Networks are cool, social networks are fun, they make their food, from ... well, everyone.

So, you'd think the hype around social networks would be long gone by now? NOT! I actually think we're just getting to the good part. Despite however our economy is doing, there is still lots of money to be made in social networking if you know where to look.

All the easy stuff is out of the way by now, and even much of the obvious stuff at the level after that is done or underway. So where do you look? I'll tell you: "integration with all the niches".

We're at a point where networks are driving traffic from one to another. It's a lot of the same users, trying on different shoes. You can be king of your throne one day, and find out 20,000 users just leaped to Facebook the next.

Don't quote me to anybody important too fast, but just food for thought: I think the next successful social network move is one that keeps you linked in no matter where you are. Wether that be more with mobile devices bringing the off-line world online, technologies such as Open Social or OpenID linking gaps from site to site, platforms for extending social sites to niches -- possibly allowing niches to take advantage of the rich user base of larger sites?

I know there is a lot of debate on this, mostly because everybody is trying to come up with the next scheme! So what's it gonna be......

Btw, I've been getting an eery feeling that my laptop is tapped? How common is that? I won't explain what happened in this post, maybe the next.

Saturday, April 5, 2008

Generate any HTML tag with these two functions.

I was working on a form generation API on Typhoon PHP Framework today and came up with two wonderful functions for generating any HTML tag using PHP.

Here are the functions, enjoy!:


public static function tag($element,$attributes=array())
{
$buffer = '<' . $element;
foreach($attributes as $key => $val) {
$buffer .= ' ' . $key . '="' . $val . '"';
}
$buffer .= '/>';

return $buffer;
}

public static function blocktag($element,$attributes=array())
{
$content = $attributes['content'];
unset($attributes['content']);

$buffer = '<' . $element;
foreach($attributes as $key => $val) {
$buffer .= ' ' . $key . '="' . $val . '"';
}
$buffer .= '>';

$buffer .= $content;
$buffer .= '</' . $element . '>';

return $buffer;
}


Currently I use these in conjunction with Smarty helpers to create consistent markup across a project I'm working on. Any thoughts are appreciated!

Wednesday, April 2, 2008

Google's April Fools, Starcraft 2, The New Relics

Google, April Fools!


How many people got caught by Google's April fools joke this year?

"Virgle: Earth has issues, and it's time humanity got started on a Plan B. So, starting in 2014, Virgin founder Richard Branson and Google co-founders Larry Page and Sergey Brin will be leading hundreds of users on one of the grandest adventures in human history: Project Virgle, the first permanent human colony on Mars."


I managed to get Mary, COO at Moguldom, to fall for it. Hehehe! Everybody else was on to my scheme though, oh well! I love Google.

Starcraft 2


I was practically born on the original Starcraft and Brood Wars expansion. I spent much of my teenage life perfecting the Zerg rush, and eventually mastered the Terran defense with a wonderful firebat-drop counter attack. So I was quite excited yesterday when Dan sent me a link to the new units in Starcraft 2. I like what I see. At the very bottom of the Tauren Marine page there seems to be a spoiler hint, though I can't be certain. Will mercenaries be apart of the new game?

So many questions! Though, I'm glad it's still at least a year away from release, as I have a lot of business plans to knock out beforehand! It'd be a shame to make a mistake and drown all the potential in a video game. Though, it's not just any video game, Starcraft 2 looks to be the nectar of the gods IMHO.

The New Relics - one rock'n band!


Aside of Google's games and video games, everybody go check out The New Relics. They are a Morgantown, WV based band, and they pretty much rock. I saw them a few times when I lived out in Morgantown, they are touring up and down the east coast these days. I'm on a mission to hook them up with a Venue in Shepherdstown so they have an excuse to come play for me. I'll be posting MySpace bulletins, and what not, if it pans out, so you locals better show up!

Monday, March 31, 2008

Getting a move on things, ad sense is treating me well again today.

In effort to keep up with posting on this daily, I have a few minutes and thought I'd jot down the happenings.

My primary income at the moment is my part time job at Moguldom Media. I'm a software developer there, working on a celebrity gossip social site. It's a good project, in my opinion, and I enjoy the professionalism of the team, as well as the work itself. I'm quite happy with it so far.

It would appear an alternate income is showing itself too! Which highly excites me, but I'm trying to refrain from being overly excited. Roughly an hour ago I checked in to see how my ad sense earnings were doing for the day on my other sites. I was surprised that it's up to 26$ today. ( Yesterday capped off around 36$ ).

I'm a bit baffled as to this pulling in basically minimum wage for where I live almost immediately. Though I can't say I'm unhappy that it's working. Still, only day two, will this be consistent? Or did I hit a bit of luck and it's going to all fade away in the morning?

Only time can tell.