0

I'm implementing a voting system like the one used by stackoverflow. It's working with ajax sending POST request to an url. I'd like a way to fail gracefully when javascript/ajax isn't supported or enabled, a GET like /voteup/id isn't even considered because i'm altering the database. What's the best solution? I'm either considering a form or simply removing the feature if js isn't enabled.

There are at least three related entries on SO but i can't insert more than one hyperlink

POST with links without JavaScript

Community
  • 1
  • 1
Spear
  • 153
  • 7
  • Bypassing the limit http://stackoverflow.com/questions/1528583/django-vote-up-down-method http://stackoverflow.com/questions/719194/how-can-you-make-a-vote-up-down-button-like-in-stackoverflow – Spear Jan 15 '10 at 19:51

3 Answers3

3

Make the basic voting actions mini-forms, then use javascript to disable their posting action.

<form method=post action="hit-url">
   <input type=hidden name=vote value="1" />
   <input type=submit value="Vote Up" onSubmit="doVote(1);return false;" />
</form>
<form method=post action="hit-url">
   <input type=hidden name=vote value="-1" />
   <input type=submit value="Vote Down" onSubmit="doVote(-1);return false;" />
</form>

To replace these with links for javascript-enabled users:

<div id="voteupbutton">
   <form method=post action="hit-url">
      <input type=hidden name=vote value="1" />
      <input type=submit value="Vote Up" onSubmit="doVote(1);return false;" />
   </form>
</div>
<script>
   document.getElementById("voteupbutton").innerHTML="<a href='script:return false' onClick='doVote(1);return false;'>Vote up</a>";
</script>

I haven't tested the above. If you're using jQuery or some other framework, there will be more elegant ways of doing all of this.

Phil H
  • 19,928
  • 7
  • 68
  • 105
  • Is there a way to submit a form with a link? (Without images or javascript) – Spear Jan 15 '10 at 19:49
  • @Spear: what's wrong with using a GET request? you can re-direct to the proper URL from the server-side with a 30x HTTP response – Christoph Jan 15 '10 at 19:52
  • @Spear: Not without javascript. However, you can use javascript to modify the page dynamically and replace the button with a link. I'll modify the answer to show that. – Phil H Jan 15 '10 at 19:52
  • @Phil H: Yes, i'm using jquery, that will do the job, thank you :) – Spear Jan 15 '10 at 20:03
  • 1
    Probably the noscript tag could be helpful. – Spear Jan 15 '10 at 20:07
1

The straightforward option is just a regular form POST, even if it is to the URL /voteup/id, and I'm not sure why you can't do that (or even the GET you mentioned).

Put onsubmit="return false" into the tag to prevent POSTing by users who do have JS enabled.

Karl B
  • 1,597
  • 8
  • 9
0

While you can't use links to submit forms, you can certainly use links to trigger database actions if you want to, via the querystring. In no particular scripting language:

<===
if (querystring("v")) then {
 v.value.writeToDatabase
}
===>

<a href="vote.xxx?v=a">Vote A</a>, <a href="vote.xxx?v=b">Vote B</a>
graphicdivine
  • 10,937
  • 7
  • 33
  • 59