12

So I have a HTML form:

<html>
  <body>
  <script>history.pushState('', '', '/')</script>
    <form action="http://myserver.com" method="POST">
      <input type="hidden" name="Id" value="83" />
      <input type="hidden" name="url" value="http://example.com/" />
      <input type="submit" value="Submit request" />
    </form>
    <script>
      document.forms[0].submit();
    </script>
  </body>
</html>

As you can see this is submitting the action for <input type="hidden" name="Id" value="83" /> meaning it's submitted for the attribute associated with ID number 83, I'm wanting the action to be submitted for multiple ID values, i.e. 1 - 100. Is this possible? If so how can it be done?

Sarah M.
  • 153
  • 1
  • 1
  • 10

3 Answers3

13

I assume you want to do something like this

<html>
  <body>
  <script>history.pushState('', '', '/')</script>
    <form action="http://myserver.com" method="POST">
      <input type="hidden" name="Id[]" value="83" />
      <input type="hidden" name="Id[]" value="85" />
      <!-- you can add as many as input here for id if you want -->
      <input type="hidden" name="url" value="http://example.com/" />
      <input type="submit" value="Submit request" />
    </form>
    <script>
      document.forms[0].submit();
    </script>
  </body>
</html>

After this form is posted, on the server side you can get $_POST['id'] as an array and playing around with it.

Long Kim
  • 490
  • 3
  • 9
  • 6
    This is assuming PHP is being used serverside. The array notation may not work in other serverside languages. – Jon P Apr 07 '17 at 00:47
2

Add [] to input name:

<input type="hidden" name="ID[1]" value="83" />
<input type="hidden" name="ID[100]" value="100" />

then the in php

  print_r($_POST['ID']); //print out the data array

Or use just one input with comma separated values?

  <input type="hidden" name=Id value="1, 2, 3,.., 100" /> 

PHP:

$ids = explode(" ", $_POST['ID']);
Leszek P
  • 1,649
  • 15
  • 22
  • 4
    This is assuming PHP is being used serverside. The array notation may not work in other serverside languages. – Jon P Apr 07 '17 at 00:47
  • 1
    Of course, but the same idea can be applied to practically every server side language – Dan Lynch Nov 18 '19 at 18:32
1

By doing document.forms[0].submit(); you are submitting all the input values in that form and values will be saved as Id=83&url=http://example.com/

If you want to submit several forms then you could use a for loop

x = document.forms.length //your desired number or number of forms
for(i = 0; i<x; i++){
    document.forms[i].submit();
}
Carlos F
  • 4,131
  • 3
  • 11
  • 19