Thursday, January 17, 2013

Submitting Forms: Don't Forget the NAME Attribute...

Just a quick post on something I stumbled across recently... I was submitting a form with several checkboxes and text fields, but no matter what I did on the receiving page, it wasn't getting any values from either POST or GET data. At first I assumed I was doing something wrong in PHP, since I'm new to the language and have been doing forms for many years. It wasn't until I was thoroughly frustrated and finally searched Google for help that I slapped my forehead and had it solved within seconds.  Hopefully, I can give you the same head-slapping moment with less frustration.

When you're working with elements like DIV, SPAN, TD, IMG or whatever else you need to reference later, common practice is to identify it with an ID such as <div id="myDiv1"> and not use the NAME attribute. This works perfectly fine for the purposes of targeting it with a CSS rule or jQuery selector. The problem is that for the purposes of submitting a form, the elements need a NAME attribute as well or they will be ignored. It'd been a while since I worked with a form, so my brain jumped to the PHP side instead of remembering the basics of form elements.

So... when you're working with <input id="firstName" type="text" > don't forget to include the name, making it <input id="firstName" name="firstName" type="text" > The NAME and ID don't need to match, but they can if it makes it easier for you.  (Quick Reference: W3 Schools)

PS: Have you ever heard the phrase "When you hear hoofbeats, think horses not zebras"? (derived from Occam's Razor) It's true that sometimes there might be a zebra there, but most often it'll be a simple horse.

3 comments:

  1. This blog information was very useful to create the form submission functionality in the websites.This is very essential for beginners of website developers.
    Web Design Company India | Web Designing Company Bangalore

    ReplyDelete
  2. The article is nice and its pleasant to read. I have known very important things over here
    Bangalore Web Designing Company, UI Designing Company Bangalore

    ReplyDelete
  3. A very well-written post. I read and liked the post and have also bookmarked you. All the best for future endeavors. Getting some solution regarding.
    subtitling services india, subtitling company in bangalore

    ReplyDelete