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>
3
  • 1
    You should close your <head> tag Commented Apr 27, 2014 at 12:30
  • The code you have is fine. What exactly is not working? Commented Apr 27, 2014 at 12:56
  • I have to press "submit" in order to make the event. But what i want is doing it automatically without pressing. Commented Apr 27, 2014 at 12:59

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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>

1 Comment

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.
-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.

5 Comments

it doesn't work and i want the input to be hidden not visible
Sorry I misread and saw 'visible' instead of invisible. Also as pointed out by abl you should close the head tag. Edited my answer.
yes i have but no difference :(
When I copy the code on on a file on my computer, it works. You have javascript activated, right? Do you get any error in the browser or in the console?
yes javascript is activated and i get no error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.