views:

56

answers:

5

I've got a form that looks like this:

<form method="post">
    {% csrf_token %}
    <table>
        {% for field in form %}
            {% partial "partials/field.html" field=field %}
        {% endfor %}
        <tr>
            <td></td>
            <td>
                <input name="save" type="submit" value="{% if is_new_entry %}Save{% else %}Update{% endif %}" class="submit" />
                {% if not is_new_entry %}
                    <input name="delete" type="submit" value="Delete" class="submit" />
                {% endif %}
                <a style="text-decoration:none" href="{% url dealership-entry %}"><button class="submit">New</button></a>
            </td>
        </tr>
    </table>
</form>

I want that "New" button just to submit a GET request back to that href. This works fine in FF (albeit it puts a stupid underline behind the button which I had to hide), but in IE it actually submits the form!

What's the easiest way to do what I want? I was thinking about closing off the form, then putting a new form with just the one "New" button and put the "href" in the action instead, but I don't think that'd be valid XHTML anymore, because the </form> needs to go after </table>.

I don't want to use JavaScript.

+1  A: 

Is this even valid? to put a button inside a link? A button should post or get depending on what the form does.

Here you have

<form method="post">

so it will post.

Maybe use something like jquery to create a link to post?

should be:

<form action="postform.php" method="post">

I believe you're not suppose to style buttons as this is an OS styling thing.

Matt
I thought the whole point of `<button>` was for stylistic purposes; for use outside a form. Apparently, it can be used for either. I don't see how making the button a link is different than making an image a link. Regardless, I'd prefer not to use JS.
Mark
Why should a `button` *not* be styled? @Mark, so just use a styled link, rather than nesting a `button`. It's much easier, and more reliable. Not to mention valid (x)html.
David Thomas
@David: Because I want it to look exactly like my other 2 buttons?
Mark
@Mark, but it's invalid to have a `button` as a descendant of an `a` element. You can style the buttons *and* a link with exactly the same css if similarity is all you're after.
David Thomas
@David: I prefer the OS defaults... plus, styling buttons isn't nicely supported across browsers.
Mark
@Mark, if you want to keep the OS defaults then you're going to have to live with the OS/browser defaults. Or use js to prevent that particular button from submitting the form.
David Thomas
@David: Eh? Your first sentence doesn't make sense... that's exactly what I want..which is why I said it. And JS isn't necessary, I've already proven that.
Mark
@Mark you're right. I was thinking of an input like mentioned above. Been a while since i touched a form. But i still don't agree with styling a button. As you mentioned and from what i remember, all the browsers don't agree with each other. Thats why i like to use javascript to make a link post.
Matt
A: 

your method is post, would that have anything to do with it? i might be reading u wrong

All browsers seem to submit a form when a button with a submit class with it

Ascherer
All browsers submit a form (assuming no js intervention) when a `button` of `type="submit"` is clicked. The class, for css reasons, shouldn't really have any effect on the action taken by the `onClick` event.
David Thomas
he doesnt have a type=submit on it, and its submitting, sooooo idk
Ascherer
@Ascheer: That's because (most) browsers have a default of type=submit. And yes, I think you're reading me wrong. The other 2 buttons *are* supposed to POST the form, but I was hoping the "new" button would ignore that and work like a link.
Mark
just take it out of the form then?
Ascherer
@Ascheer: How am I supposed to submit my form then?!
Mark
javascript haha, honestly what you are trying to do would really be best suited for an ajax post or something
Ascherer
Now you're just being ridiculous! This is supposed to be a dead-simple form. This isn't even apart of the main site, just some admin junk :P
Mark
Being honest. It would be best suited to use javascript with it
Ascherer
A: 

Always specify the type attribute for the button. The default type for Internet Explorer is "button", while in other browsers (and in the W3C specification) it is "submit".

furtive
Just read that, but it doesn't help. It appears that IE8 is following the spec now, because by default it actually does *submit* the form. Changing it to "button" makes it do *nothing*, even when wrapped in a link, or putting the link inside.
Mark
+2  A: 

A button is not just a 'stylistic' object, but also a functional one.

What you appear to need is an anchor tag (A) styled to appear to be a button. You can do that with CSS alone, and very convincingly with CSS+Images for button states.

The button tag inside the anchor tag is fairly nonstandard (and may be technically invalid), so that's why you aren't getting consistent behavior.

Andrew Barber
Actually, button inside an anchor seems to be valid markup, but it seems logical that it should override the anchor's behaviour. I agree that the problem here is button used instead of CSS.
zvonimir
@zvonimir It is not valid. http://www.w3.org/TR/html5/content-models.html#interactive-content
jleedev
Yup, zvonimir; I just did a check against an XHTML1.0 validator I have, and a BUTTON inside an A did not trigger a warning (whereas nesting an A directly inside an LI, for example, does).
Andrew Barber
Regardless of whether or not it's valid, it doesn't work in IE (as usual). @Andrew: You propose a reasonable solution, but not one I like.
Mark
Fair enough :) I think without JavaScript and without tearing up your form, it's the easiest way. Actually, I think it's even easier than those options myself.. hehe.. I've done links-as-buttons like this myself before.
Andrew Barber
A: 

My present solution is to make the "New" button into an actual <input type=submit> button and handle it with a redirect on the backend, rather than fighting with the HTML and validity.

Mark