Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How To Simulate a Web Form Submit Redirect with POST method
09-27-2009, 09:20 PM
Post: #1
How To Simulate a Web Form Submit Redirect with POST method
After struggling to find an answer to this online with lots of solutions that simply don't work involving curl (because it submits the post data but doesn't redirect), I came up with a simple solution.

Javascript to the rescue!

When submitting a web form with the method of POST for vars it means the variables from the form fields are hidden from view so it becomes more difficult to simulate a form submit in this way. With the GET method it is simple since the form data is in the URL.

The answer is to clone the submit form and in the value fields have the actual data that you want to be posted. Like you are pre-populating the web form.

Next, give the form a name value to reference it in the DOM.

Then, at the end of the page in a script section you have a line of Javascript to submit the form.

You want the Javascript to be after the form since it runs client-side (in the browser) so it is seen as the web page is rendered (displayed) by the browser.

I slipped a bit of PHP into the example to prove that it works, PHP is executed server side so it can respond to POSTED data via the server global variables.

Example code to submit a web form using the post method using Javascript:
Code:
<html>
<head>
</head>
<body>
<?php
if (isset($_POST['forum'])) {
    echo "<h2>Posted data: " . $_POST['forum']  ."</h2>\n";
    exit; //  Avoid looping
}
?>
<form action="" name="myForm" method="post">
<input type="text" name="forum" value="webmasterjuice.com" />
</form>
<script type="text/javascript">
document.myForm.submit();
</script>
</body>
</html>

Save this as jform.php to test on your server. Simply displaying the page should cause it to submit the data by the post method and display this data in an H2 tag above the form.

Internet Marketing Software
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: