0

I know that there is no way to use a query string to pass information into a form that uses the post method, but are there any other options? I am trying to use a link (or button) on one website to link to a page on another. In the process, the URL/button needs to send information to the target. The target website uses post, so I can't just use a query string to input my data. Is there some sort of button attribute that will let me send data to the fields in the target website? Thank you in advance.

EDIT: I should have also mentioned that I cannot use the form tag. For some reason, the environment I am using does not allow for some tags to be used.

Kevin Rogers
  • 85
  • 1
  • 2
  • 8

2 Answers2

2

With a button:

<form action="http://example.com/target.php" method="post">
  <input type="hidden" name="foo" value="bar" />
  <input type="submit" />
</form>

With a link:

<form id="my_form" action="http://example.com/target.php" method="post">
  <input type="hidden" name="foo" value="bar" />
</form>

<a href="#" onclick="document.getElementById('my_form').submit(); return false;">Submit</a>
Sammitch
  • 30,782
  • 7
  • 50
  • 77
  • This would work, but I have edited my original post to explain why I cannot use this method. – Kevin Rogers May 16 '14 at 20:02
  • @KevinRogers then you have to figure something out in 100% javascript, but it will still rely on Javascript constructing a form element in the page's DOM structure. Disallowing the form tag is... well, the most diplomatic term I can think of is: moronic. – Sammitch May 16 '14 at 20:07
  • I agree, but for some reason that's how it was set up. – Kevin Rogers May 16 '14 at 20:19
2

This should solve your problem (tested it locally and confirmed form data was present) ... got starting point here https://stackoverflow.com/a/13678453/1946558

<button class="btn" id="go">Go </button>

<script type="text/javascript">
    $(document).ready(function () {
        $('#go').click(function () {
            var form = document.createElement("form");
            var input = document.createElement("input");
            // where you want to post the data 
            form.action = "test";
            form.method = "post";

            // add your fields here 
            input.name = "id";
            input.value = 'some data';

            // then append 
            form.appendChild(input);

            document.body.appendChild(form);
            form.submit();
        });


    });


</script>
Community
  • 1
  • 1
ajzeffer
  • 840
  • 1
  • 7
  • 13
  • my assumption was that you could not enter a form tag into the html before hand ... this allows you add the form tag on the fly – ajzeffer May 16 '14 at 20:23
  • @kevin did this resolve your issue ? If so can you please mark as answered. – ajzeffer May 20 '14 at 22:41