tags:

views:

67

answers:

5
http://localhost/test/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'];
?>
+3  A: 

# is a hash character, not a GET variable.

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

Jacob Relkin
No,you didn't get it.
It also sends request to editformquestions.php
Ok, tell me what i didn't get.
Jacob Relkin
What also sends requests to editformquestions.php?
Jacob Relkin
Show me your code.
Jacob Relkin
The 1st url containing "#" also sends request to test/editformquestions.php,but can't retrieve the $_GET parameters in that file.
Exactly. it goes to editformquestions.php, of course. But it DOESN'T PASS IN THE GET VARIABLE BECAUSE OF THE `#` CHAR IN FRONT OF IT.( SEE OTHER MORE IN-DEPTH ANSWERS ABOVE ).
Jacob Relkin
In fact I'm asking:why $_GET array is not populated ?
You're kidding?
Kyle Rozendo
If you wrote `?form=1#` that would work.Because the querystring parser ignores it because it sees something other than the `?` char, so it stops execution.
Jacob Relkin
@Kyle, Tell me about it ;)
Jacob Relkin
+6  A: 

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

Jerome
A: 

The # is fragment identifier for a URL: http://en.wikipedia.org/wiki/Fragment%5Fidentifier

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

pygorex1
It's ahead of "?",not behind!
A: 

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.

Piskvor
+2  A: 

# 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&amp;othervar=2#anchorinpage

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

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