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;
  theForm.submit();
}
</script>

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

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.

  • Best breast enlargement exercises. User ratings & reviews of top breast enlargement products.

  • Anonymous
  • Anonymous
  • Anonymous
  • The Best shoes manufacturer in Vietnam. We provide: Vietnam shoes, Beach sandals, sport sandals, eva slipper, Vietnam shoes manufacturer, Sandals shoes, Vietnam eva slipper, Sport beach sandals.

  • Biti’s – Nhà sản xuất giầy dép hàng đầu tại Việt Nam. Biti’s cung cấp: Giầy thể thao, giầy sandals, giầy da, giầy tây, dép xốp, giầy dép học sinh, hài, guốc gỗ. Chất lượng tuyệt hảo – Hậu mãi chu đáo.

  • The Best shoes manufacturer in Vietnam. We provide: Vietnam shoes, Beach sandals, sport sandals, eva slipper, Vietnam shoes manufacturer, Sandals shoes, Vietnam eva slipper, Sport beach sandals.

  • Biti's – Nhà sản xuất giầy dép hàng đầu tại Việt Nam. Biti's cung cấp: Giầy thể thao, giầy sandals, giầy da, giầy tây, dép xốp, giầy dép học sinh, hài, guốc gỗ. Chất lượng tuyệt hảo – Hậu mãi chu đáo.

  • The Best shoes manufacturer in Vietnam. We provide: Vietnam shoes, Beach sandals, sport sandals, eva slipper, Vietnam shoes manufacturer, Sandals shoes, Vietnam eva slipper, Sport beach sandals.

  • The Best shoes manufacturer in Vietnam. We provide: Vietnam shoes, Beach sandals, sport sandals, eva slipper, Vietnam shoes manufacturer, Sandals shoes, Vietnam eva slipper, Sport beach sandals.

  • add link

  • add link

  • add link

  • Corscaria

    a use i found for form.action is to force form submits to use https
    example:
    document.form.action = document.form.action.replace(“http”,”https”);

    if your url specifies a custom port (to get past port blocking ISPs), you will need to change the port number to your https port
    example:
    document.form.action = document.form.action.replace(“http”,”https”).replace(“:8080″,”:4330″);

  • Corscaria

    a use i found for form.action is to force form submits to use https
    example:
    document.form.action = document.form.action.replace(“http”,”https”);

    if your url specifies a custom port (to get past port blocking ISPs), you will need to change the port number to your https port
    example:
    document.form.action = document.form.action.replace(“http”,”https”).replace(“:8080″,”:4330″);

  • I wanna find more info about this, anybody could?

  • Aj

    gr8

  • Durgesh

    thanks ,Good Example

  • Smartsidd

    nice one