Category Archives: JavaScript

Submit HTML form using GET or POST without the ‘Submit’ button

Whenever you submit a form using the submit button, the value of the submit button is also sent.

For example, this form:

<form id="myForm" method="get" action="http://www.your-site-name.com/page.php">
 <input type="text" name="value1" value="foo">
 <input type="text" name="value2" value="bar">
 <input type="submit" name="submit" value="Submit">
</submit>

Will open a page with the following URL:

http://www.your-site-name.com/page.php?value1=foo&value2=bar&submit=Submit

The best way to prevent the submit variable from showing is to not transmit the form using the submit button, but instead to use a bit of JavaScript on a normal button:

<form id="myForm" method="get" action="http://www.your-site-name.com/page.php">
 <input type="text" name="value1" value="foo">
 <input type="text" name="value2" value="bar">
 <input type="button" name="submit" value="Submit" onClick="document.getElementById('myForm').submit();">
</submit>

Which will yield a submitted form with the following URL:

http://www.your-site-name.com/page.php?value1=foo&value2=bar

Voila.

How to open a new browser window or tab using JavaScript

A client-side language like JavaScript can be used to open a new browser window or tab. Unfortunately, as it is client-side then there is no way to specify whether it is a new window or tab that is opened… that is down to how the particular browser is configured.

<script type="text/javascript">
 window.open('http://link-to-site/page.php?$value1=" . $value1 . "&value2=" . $value2 . "')
</script>

Open new window or tab and submit form using POST and PHP

How often have you pondered the best way of generating a dynamic page in PHP and then need to submit it to a website in a new window as if it has come from a form? Not often? Me neither… but having struggled to find an elegant solution I thought I would share my findings.

(See also the solution for using GET)

There are a few different ways of submitting a form using the POST headers from a dynamically generated PHP page. The first uses cURL, however I find this code to be cumbersome. The simplest way I have found is to generate a form and submit it using the POST method. It would be up to you whether you would prefer to open a new window (see how to open new window in javascript here) or place it in the code of the page you are loading and set ‘targert = “_blank” ‘ as used in the example below:

<?php
 $value1 = "foo";
 $value2 = "bar";
?>

<form id="myForm" method="get" action="http://link-to-site/page.php" target="_blank">
 <input type="hidden" name="value1" value="<?php echo $value1;?>" />
 <input type="hidden" name="value2" value="<?php echo $value2;?>" />

 <script type="text/javascript">
  document.getElementById('myForm').submit();
 </script>
</form>

Open new window or tab and submit form using GET and PHP

How often have you pondered the best way of generating a dynamic page in PHP and then need to submit it to a website in a new window as if it has come from a form? Not often? Me neither… but having struggled to find an elegant solution I thought I would share my findings.

(See also the solution for using POST)

Submitting a form using the GET method simply places all of the values in the URL. Because PHP is a server-side language, there is no way to force a new window to open in a browser without using a client-side language (e.g. JavaScript). This javascript can be dynamically generated via PHP though to solve our problem.

<?php
$value1 = "foo";
$value2 = "bar";

echo "<script type="text/javascript">
	window.open('http://link-to-site/page.php?$value1=" . $value1 . "&value2=" . $value2 . "')
</script>";"