Tag Archives: form

Changing a form submit url with javascript

Sometimes you might need to change the address a form submits to, it’s quite easy with javascript. One example could be if you – like me – from time to time is hit hard by comment-spammers. Changing the submit URL on the form with javascript, makes it at least somewhat harder for “automated brainless robots” to kill your site in a spam attack.

The recipie below is a very simple example, but so far it seems to work quite well:

<script type="text/javascript">
function doSubmit() {
  var target1 = 'hello';
  var target2 = 'world';
  var target3 = '.cgi';

  var supertarget = target0 + target1 + target2 + target3;
  var theForm=document.getElementById("theForm");
  theForm.action = supertarget;

<form id="myForm" method="post" action="some_fake_address.php">
<input type="text" name="dummy field" value="">
<input type="button" onclick="javascript:doSubmit();">

Whats going on?

The form is a regular form except for to small things:

The action-url is a fake address. On the url it points to, is only an empty file (to prevent 404 errors in my errorlog.
The normal submit button has been changed to a regular button, and the onclick-evnt calls a small piece of javascript, which does the magic trick.

The javascript function can be more or less advanced depending on your needs. In this example it constructs the new target for the form submit by pasting three strings together and replacing the action of the form.