2

I have looked at different threads but couldn't find a proper solution.
I want to make a fake html file which fills a form automatically and submits when it is loaded by the browser.

The two input fields maks and reciever are filled by the values I have given, but I need to press submit.
And if possible it could be nice if the values of the input fields are invisible. Thank you.

<html xmlns="http://www.w3.org/1999/xhtml">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <head>
    <script type="text/javascript">
      function postReq() {
        var frm = document.getElementById('post_req');
        if (frm) {
          frm.submit();
        }
      }
    </script>

  <body onload="postReq()">
    <form method=POST name=exampleform id='post_req'
       action="/ex/makfoer.php">
      <input name=maks type=hidden value="2" /> 
      <input name=reciever type=hidden value="otto" />
      <input type=submit />
    </form>
  </body>

</html>
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
alperc
  • 373
  • 2
  • 18

3 Answers3

2

In your case you could just use:

document.exampleform.submit();

Explanation:

exampleform is the name of your form, so you can get the form with:

document.exampleform

Now you only activate the .submit() method of the form. You don't need a submit button to submit. So everything is hidden.

Kai
  • 1,156
  • 8
  • 18
0

1. Close your <head> tag.

2. Use quotes for the values of attributes name, type, etc.

3. No need for a submit button, especially since you don't want your form to be visible

<html xmlns="http://www.w3.org/1999/xhtml">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <head>
    <script type="text/javascript">
      function postReq() {
        var frm = document.getElementById('post_req');
        if (frm) {
          frm.submit();
        }
      }
    </script>
  </head>

  <body onload="postReq()">
    <form method="POST" name="exampleform" id='post_req'
       action="/ex/makfoer.php">
      <input name="maks" type="hidden" value="2" /> 
      <input name="reciever" type="hidden" value="otto" />
    </form>
  </body>

</html>
abl
  • 5,970
  • 4
  • 25
  • 44
  • I want to see the submit button even though it is not making any sense, since the intention of this is a fake html of the original one only to make the user confusing and i want the inputs "maks"'s and "reciever"'s values to be hidden not the button itself. – alperc Apr 27 '14 at 13:07
-1

If you want to do that without jQuery, use window.onload event handler:

<head>
<script type="text/javascript">
  function postReq() {
    var frm = document.getElementById('post_req');
    if (frm) {
      frm.submit();
    }
  }
  window.onload = postReq;
</script>
</head>

window.onload is fired when the whole page is loaded (including external scripts, images, etc.), as opposed to document.onload which is fired once the DOM of the page is parsed and loaded.

Djizeus
  • 4,161
  • 1
  • 24
  • 42