0

I have the following code to link a button to a specific URL.

stage_link = " 'http://limitless-scrubland-2940.herokuapp.com/game/game.html?stageid=5' "
<form action=stage_link method='get'> 
    <button>Play stage!</button>
</form>

The button renders fine, but it links to: "http://limitless-scrubland-2940.herokuapp.com/game/game.html"

Why is it that the query-string portion (?stageid=5) gets truncated off? Is it possible to prevent this from happening?

2 Answers2

1

Simple. <form action cannot be a $_GET query. Use a hidden field and test against it, if you must submit a form:

<form method='get' action='http://limitless-scrubland-2940.herokuapp.com/game/game.html' method='get'>
  <input type='hidden' name='stageid' value='5' />
  <input type='submit' value='Play stage!' name='playstage' id='playstage' />
</form>

Now test for $_GET['stageid'] on your PHP page.

For your purpose, however you may just need:

<a id='playstage' href='http://limitless-scrubland-2940.herokuapp.com/game/game.html?stageid=5'>
  Play stage!
</a>
StackSlave
  • 10,613
  • 2
  • 18
  • 35
0
<form action="" method='get'> 
    <button>Play stage!</button>
</form>
<script>
document.forms[0].action= "http://limitless-scrubland-2940.herokuapp.com/game/game.html"
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'stageid';
input.value = '5';
document.forms[0].appendChild(input);
</script>

See submitting a GET form with query string params and hidden params disappear on why.

Community
  • 1
  • 1
Brett Zamir
  • 14,034
  • 6
  • 54
  • 77