Sorry I didn't look completely at your example.
What you are doing either requires Javascript, or an intermediate page.
Since I dispise Javascript I'll show you what you need to do with an intermediate page.
<!-- Form Page -->
<body>
<form name="form1" method="post" action="formSubmit.php">
<input name="gallery" type="text" id="gallery">
<input name="Submit" type="submit" value="Submit">
</form>
</body>
</html>
<?php
// formSubmit.php
header("Location: [url]http://www.mydomain.com/[/url]" . $_POST("gallery"));
?>
However what you are doing doesn't need any php if you just get the Javascript correct for your OnClick event that you already have.
You need to remember that PHP is a server side language, that means that it is only "running" on the server. When someone is viewing a page that means that PHP has completed running. PHP is only operating on page generation. If you don't understand what I just said you are going to have to look up exactly how the web is structured, and what is ment by a "stateless environment" and do a bit of reading in the php manual.