user1257724
user1257724

Reputation:

URL's query-string portion is getting cut of when used as a link in a button

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?

Upvotes: 0

Views: 1066

Answers (2)

StackSlave
StackSlave

Reputation: 10627

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>

Upvotes: 1

Brett Zamir
Brett Zamir

Reputation: 14355

<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.

Upvotes: 0

Related Questions