tags:

views:

183

answers:

6

In my HTML code, I have a form tag that contains two input boxes, username and password. The form submits the values to a PHP file which validates the values with the database and redirects to another page if the login is successful.

The problem with this approach is that, if AJAX is not involved, it refreshes the page even if the login is unsuccessful.

I would like to remove the form tag altogether, merge the PHP code into the same file as the HTML. For this I attempted to add an onclick() event to the input boxes. But since form tag was removed, something awkward happened. I picked the values using: document.getElementByID

I want to know whether it is necessary for an input tag to be enclosed within the form tag? If I just want the input box and want to have some operation done using the onclick event, would it work?

+2  A: 

It's not necessary to submit the form if you write your own handlers using onClick(). But you should maintain semantic HTML mark-up.

rubayeet
+2  A: 

If you want to put them in a form, you still can; just don't put a submit button on the form. If you use a <button type="button"> element, the button shouldn't trigger form submission.

John Calsbeek
@John: It's <input type = "button"> right?
RPK
No, HTML 4 has a `<button>` tag that is quite well supported.
John Calsbeek
+11  A: 

My preferred method is to leave the <form> tag but catch the onsubmit event of the form and just return false.

<form name="foo" id="foo" action="" method="post">
  <input type="text" name="bar" id="bar" />
</form>

Here's some rough PrototypeJS code that illustrates the approach.

<script type="text/javascript">
  //Handle the window onload event
  Event.observe(window, 'load', function() {

    //Handle the form submit event
    Event.observe($('foo'), 'submit', function(event) {
      //Stop the event so the form doesn't actually submit
      Event.stop(event);
    });

  });
</script>
Mark Biek
I think this is the best solution to your problem. you can remove the form tag, change the input type on submit to button, and use onClick(), but why would you want to? This way makes more sense markup wise.
GSto
The advantage to this method is that if you hook up the form server-side as well, it works even if JavaScript is disabled.
John Calsbeek
@John, excellent point.
Mark Biek
A: 

It's not necessary, however, you need to make sure the form still is usable. The form tag provides better tabbed key navigation, along with the ability to submit on pressing the enter key. You can emulate these in javascript, but beware of that. If you want, you could try setting the form tag's action url to <form action="javascript:submitLogin();">, which would validate the form. If javascript was off, nothing would happen.

CodeJoust
A: 

Although it is generally recommended, it is not necessary. In fact it validates as html 4.01 strict without is.

GameFreak
+2  A: 

It is not necessary for an input tag to be enclosed in a form tag for the site to work as you want it to.

However, you should create your site in such a way that the majority of users can use it. So for those users who don't have JavaScript enabled or are viewing your site with a browser that doesn't have JavaScript capabilities, you should still develop your site so they will have a good experience.

The AJAX behavior should be added after the site works correctly without JavaScript enabled, following Progressive Enhancement principles. The best way to prevent the form from executing the default behavior is as Mark pointed out -- return false on the form's onsubmit event handler.

You will need the form tag as well if you are trying to create semantic HTML that passes validation.

sheats