views:

99

answers:

3

Hello all! Quick question

I originally had a submit button.

<input class="submit" type="submit" class="input" value="Add" name="command" />

but now I would like to use a <a href> instead. The issue is, the value="Add" is very important.

I'm currently making the <a href> like this.

<a href="javascript:document.register.submit();">submit</a>

Any suggestions? Issue is, the site is not picking up that this specific <a href> was clicked, and therefore won't run my php code.

+3  A: 

Add a hidden field, and an extra line into the javascript:

<input type="hidden" name="command" />
<a href="javascript: document.register.command.value='Add'; document.register.submit();">
    Submit
</a>

That said, you might be better off using CSS to style the submit button to look how you want it to.

nickf
+1  A: 

First off I highly recommend jquery. It makes a world of difference when working with javascript and simplifies a number of issues especially cross different browsers.

What I suggest is that you create a hidden input to set the value with.

<script type="text/javascript">
function submitFormWithValue(val){
  document.getElementById('command').value = val;
  document.forms["test"].submit();
}
</script>

<form id="test" name="test" action="#" method="post">
<input type="hidden" name="command" value="" />
</form>

<a href="javascript:submitFormWithValue('foo')">Submit</a>

I didn't test my code but it should be close enough to see how it works. Again check out jquery it will make dealing with javascript a lot easier!

bitLost
Everyone had great answers, but in the end I did use jQuery. I've been trying to avoid jQuery thinking it'd be hard to work with. But once you understand how it works, it does make everything much simpler.
Idealflip
+4  A: 

As Pekka suggests: Don't do this. It will make your form unnecessarily be dependent on JavaScript!

The best thing to do is use CSS to style the button to look like a link:

input.submit {
  margin: 0;
  padding: 0;
  border: none;
  color: blue;
  background-color: transparent;
  text-decoration: underline;
}
RoToRa
+1 this. Not only does the link version depend on JavaScript, it also reacts very poorly when interacted with using other link actions, such as middle-click, drag-and-drop or right-click-bookmark. If it's an action, rather than a link to somewhere, it should be a button. Also, `javascript:` links are concentrated evil and should never be used.
bobince
With styling a button to not look like a button there's one slight limitation: IE will always add a 1px extra padding that you can't remove, and will make the content of the button shift by a pixel when clicked upon. This usually doesn't matter, but if you need the size of the button to be pixel-perfect you have to account for the padding with a browser-specific stylesheet rule.
bobince