Results 1 to 5 of 5

Thread: <button> submitting form

  1. #1
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default <button> submitting form

    I wanted to try and port over all of my input buttons to actual buttons, however I've found that upon being clicked they will submit the form. I was under the impression that this would only happen if type="submit" was set.

    example:
    HTML Code:
    <button onclick="popUpCalendar(document.getElementById('date').value)">
        <img src="/images/icons/calendar.jpg" alt="Choose Date">
    </button>
    For novelty sake, I also tried replacing the <img> with just the alternate text, as well as explicitly assigning type="button", but same action occurs.

    Upon initialization, the button is supposed to create a modal window and display a calendar with various options, however none of them involve submitting the form.

  2. #2
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    That is the default action (when inside a form) if you don't specify what type of button it is. The browser will interpret it as <button type="submit"> and it will work like a submit button.
    Specify that it is just a push button with no default action by giving it the following type:
    HTML Code:
    <button type="button"></button>
    That will prevent it from acting as a submit button.

    Edit: I just noticed that you said you already tried this. I tried it myself and it worked fine so I'm guessing you either made some small mistake or it doesn't work with your doctype. I suggest you try it again with a HTML4 strict doctype. If it still submits, please post a link to the page.

    Good luck!
    Last edited by Snookerman; 04-23-2009 at 07:18 AM.

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    The page in question is HTML 4.01 Strict, but it's a null issue now.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I generally prefer using the input, as I'm just more used to it. Any button or input (regardless of how it is coded - as a button tag or as an input tag), if it is the only button or input, it will often submit the form.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    May 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Something I would do to prevend the submission is in the <form> tag, to have the onSubmit="return some_function();". That function will evaluate the situation and return appropriate action. Like if you dont want the form to be submitted, then just return false, it will not be submitted. Otherwise, return true, and the form is submitted. Cheers

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •