开发者

Strange issue about # in url

http://localhost/t开发者_C百科est/editformquestions.php#?formid=1

And

http://localhost/test/editformquestions.php?formid=1

I failed to retrieve $_GET['formid'] in the first one,why?

The content of test/editformquestions.php is simply:

<?php

echo $_GET['formid'];
?>


Characters after the hash # are to be used by the browser, they are not sent back to the server.


# is a hash character, not a GET variable.

You need to put the ? before any hashes, otherwise the $_GET array will not be populated.


# is used by the browser, and is never sent to the server. Everything after a # (regardless of what it is) is used by the browser to jump to a location on the page.

So:

http://localhost/test/editformquestions.php#?formid=1

Will be split as follows:

  • Server request to http://localhost/test/editformquestions.php
  • Browser then searches in page for:

    <a name="?formid=1">named anchor tag</a>
    

What you should do is:

http://localhost/test/editformquestions.php?formid=1&othervar=2#anchorinpage

Or, if you need the # in a query-string parameter:

http://localhost/test/editformquestions.php?formid=1&othervar=textwith%23init


The # is fragment identifier for a URL: http://en.wikipedia.org/wiki/Fragment_identifier

To use it as part of an array variable you'll need to url encode it:

'#' becomes '%23' when url-encoded

In javascript you can accomplish url encoding with the encodeURI() function


A HTTP URL may contain these parts:

  • protocol: http://
  • domain: localhost
  • path: /test/editformquestions.php
  • query string: ?formid=1 - always starts with a ?
  • fragment: #something - always starts with a # - whatever comes after the # mark is NOT sent to the server.

What you have in your first example (http://localhost/test/editformquestions.php#?formid=1) is a fragment containing this: #?formid=1. It does not matter that there's a ? in the fragment; as soon as it's behind the #, it is not sent from the browser.

So, in essence, you are sending to the server only this: http://localhost/test/editformquestions.php - as you can see, there is no formid in that request.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜