Jin Hao
Jin Hao

Reputation: 1

How to use GET method to send string which includes'#"?

I want to use the GET method to send a string to the receive page, but if the string includes '#', the receiver page can only get the sub string before the '#'. As the following example:

<a href="test.php?q=string1#string2">test</a>

When I click the 'test' link to open the 'test.php' page, which has the following code:

<?php
if(isset($_GET["q"])) {
echo $_GET["q"];
} 
?>

It only display 'string1' on the page, '#string2' is missing.

So I want to know what happened to the string, and how to fix this problem.

Thank you for any help!

=======Update===========

With the help of @Eric Shaw and @JP Dupéré, I know how to fix this problem.

The simplest way is encoding the string before using the get method.

To encode the query string, you can:

  1. use urlencode() in PHP, and urldecode() can decode the string.
  2. use encodeURIComponent() in JavaScript, and decodeURIComponent() can decode the string.

Upvotes: 0

Views: 2019

Answers (2)

Eric Shaw
Eric Shaw

Reputation: 141

The #foo is used to jump to an <a name="foo"/> tag on the page, rather than viewing the top of the page when the browser loads it.

The stuff after the # is processed by the browser and NOT sent to the server.

You can escape the # and the escaped version will be sent to the server, i.e.

<a href="test.php?q=string1%23string2">test</a>

will do what you want I think

This escaping is also a common technique to get the # passed along in the URL for redirectors.

Upvotes: 1

JP Dup&#233;r&#233;
JP Dup&#233;r&#233;

Reputation: 164

Try

urlencode("string1#string2")

before calling GET.

Upvotes: 2

Related Questions