0

For example: I declared url in javascript:

<script>
  window.location.href = "signup.php#year=" + myyear; 
</script>

And in php, I am trying to get #year:

<?php
  if(isset($_GET['year'])){
    $year = $_GET['year'];
 }
?>

thank you in advance!

  • 2
    The hash in a URL is not a GET variable. Use `?year=myyear` instead. – evolutionxbox Mar 10 '17 at 09:48
  • 2
    Possible duplicate of [Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?](http://stackoverflow.com/questions/940905/can-i-read-the-hash-portion-of-the-url-on-my-server-side-application-php-ruby) – evolutionxbox Mar 10 '17 at 09:50
  • If you don't want to redirect to another page then use ajax – Snehal Gongle Mar 10 '17 at 12:05

5 Answers5

0

in simple terms we use ? or / to get the variables and not #

Snehal Gongle
  • 337
  • 3
  • 16
0

don't used # sign

<script>
  var myyear;
  window.location.href = "signup.php?year=" + myyear; 
</script>

and you can get

<?php
  if(isset($_GET['year'])){
    $year = $_GET['year'];
 }
?>
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
0

Actually you just missing one thing on your href.

window.location.href = "signup.php?year=" + myyear

this code results to url with *signup.php?year=2017

Php You can get it using $_GET['year'] now.

Reyan Tropia
  • 140
  • 1
  • 10
0

Convert the url string to a PHP url object with the function parse_url and dereference its "fragment" key like this:

$url=parse_url("www.example.com/example/?shareURL&adherentID=ascd#123");
echo $url["fragment"]; 

The above code returns 123. working example here - http://codepad.org/r8icljcW

Mani7TAM
  • 469
  • 3
  • 10
-1

If you want to make use of $_GET then you should use a ? instead of #. The ? symbol is the indicator for a GET, not the #.

So simply change your URL structure to:

<script>
  window.location.href = "signup.php?year=" + myyear; 
</script>

and then, as you already did, grab the value with the GET.

<?php
  if(isset($_GET['year'])){
    $year = $_GET['year'];
 }
?>

If you want to use more parameter, use the &symbol to separate them. You can add as many as you want, you just have to follow the `&key1=value1&key2=value2? structure and you can expand it as long as you want. Example:

<script>
  window.location.href = "signup.php?year=" + myyear&month=5; 
</script>

Now you could do:

<?php
  if(isset($_GET['year'])){
    $year = $_GET['year'];
    $month = $_GET['month']; // Would assign 5 to month.
 }
?>
Twinfriends
  • 1,972
  • 1
  • 14
  • 34
  • If I use a "?" : window.location.href = "signup.php?year=" , then is loading all the page and everything disappear. I created tabs from javascript and I wanted to send the name of tab in url and get in php. I want to work in same page, without direct me to another page. – Mihaela Tudurache Mar 10 '17 at 10:06
  • Then you'll have to use javascript. PHP is running on serverside. You can't "send" a value to PHP after the page is loaded, thats not possible since the PHP code is executed before the page is displayed to the user. So if you want to work without any reload, use javascript for this task. A good way to send data to your PHP script without reloading the page is `AJAX` - simply google for it, you should find tons of examples. – Twinfriends Mar 10 '17 at 10:09
  • Would be great if mr. downvoter could explain why he downvoted here? :D – Twinfriends Apr 07 '17 at 06:40