Javascript: POST Variables to Forms

It’s possible, and in fact very easy, to send POST variables with Javascript. I’m posting it here because I had some trouble coming up with a solution.

The only thing you have to do is to put an HTML form on a site, and use Javascript to submit it. If you don’t want the form to be visible, you can have the fields hidden. Here is a sample HTML code:

Use this JS function to submit the form:

function jsPost() {
  document.theForm.submit();
}

It can be called in any way, for instance in a hyperlink:


Submit

Or as an event, like on body load:



add ringtone cellular link2am mp3 breathefamily ringtones addamsmp3 6cycle upside mind downable mp3 dvd 504credit 1stadvantage union6buy levitra genericbody mp3 gloves 75547 Mapbig movie titsmovies porn childrensex movies pantyhose freemovie trailers gaymassive cock moviesperfect movies fuckmovie the rent musicalmovies summer cummings Map

Maybe Related?

11 Comments »

  1. Is this possible to do but have jsPost use a variable to change the post data?

    Comment by Anon — October 11, 2006 @ 3:54 pm

  2. Anon: Certainly. You can use the following code (untested):


    document.theForm.someField.value = "The new value"

    Comment by Tim — October 11, 2006 @ 4:40 pm

  3. When use document.theForm.someField.value = “The new value” I lose the ability to get my post variables. Is there a way to get around this.

    Comment by Jeffery Shipman — December 18, 2006 @ 7:30 pm

  4. Thanks,

    I needed a way to pass both user input and which item a user clicked in a list. Without JavaScript I could only conveniently either post the user input or retrieve the selected list item with a GET parameter. This solution allows to easily combine POST and GET parameters. Thanks again for the solution.

    Comment by Toon Macharis — April 3, 2007 @ 12:16 am

  5. Can we post not form but from javascript itself ?

    Comment by Turikan — May 7, 2007 @ 9:56 am

  6. Toon Macharis: You are welcome.

    Tunkan, I do not know how to do that. However, this solution has the exact same effect, so I do not understand why you would want to do it, either.

    Comment by Tim — May 8, 2007 @ 5:49 am

  7. fddfghdf

    Comment by rewr — May 16, 2007 @ 3:29 am

  8. Thanks much! Can’t say how much I appreciate this.

    I don’t even bother to create a function and now use this on buttons instead of using submit buttons for passing a single command value to pages. Using standard submit buttons required the value sent be the text on the button face which has always annoyed me.

    TEST BUTTON

    Comment by Norm B — May 30, 2007 @ 8:54 pm

  9. Norm B, I’m glad it helped!

    Comment by Tim — May 31, 2007 @ 8:50 pm

  10. Well actually you can use AJAX for POSTing stuff here and there :) Very useful approach.

    Comment by alpha — August 19, 2007 @ 4:22 pm

  11. [...] Javascript: POST Variables to Forms — Timblog [...]

    Pingback by <blog> interfce.com </blog> » Blog Archive » bookmark dump — November 20, 2007 @ 8:43 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

FireStats iconAnvänder FireStats