1

I need to grab the value of URL after #. For example in this URL: http://url.com/index.php#33/1032. I just need to get "33/1032" But PHP seems to not allow any value after #. So I used JS. Here is how I am doing it.

<script> 
  function curState(){
    var state = window.location.hash.substr(1);
    return document.write(state);
  }
</script>

Now that I have the url I need to pass this portion of the URL 33/1032 back again to the url after POST call. Here is how I am trying to do:

<?php  
  $copyVal = " <script> curState(); </script> ";
  echo $copyVal; // displays value in browser perfectly
?>

Now,

<form action="index.php#<?php echo $copyVal ?>" method = "post" >
    // form code
</form>

After submit it displays

http://url.com/index.php# <script> curState(); </script>

But I want

http://url.com/index.php#33/1032

Please help.

Thank You.

asax
  • 237
  • 6
  • 21
  • how to past value via JS in form POST? just the numbers. For eg. like above: 33/1032 – asax Jul 29 '15 at 04:46
  • You need to learn about the page lifecycle. PHP and JavaScript do not run at the same time. Hence your issue. The hash is not sent to the server. If you want it sent to the server, use querystrings and modern browsers support the HTML5 History API so you do not have to use the hash anymore. – epascarello Jul 29 '15 at 04:58

4 Answers4

1

Use jQuery -

<form action="index.php" method = "post" id="myform" >

The jQuery will be -

var state = window.location.hash.substr(1);
$('#myform').attr('action', $('#myform').attr('action') + '#' + state);

FIDDLE

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
  • Your solution works. But only takes previous value. We have to hard press the url to get curent value. The url http://url.com/index.php#44/234 .. the #44/234 part changes dynamically... so when we send value via post it has to grab the most recent value – asax Jul 29 '15 at 07:18
0

Instead of PHP code, use JavaScript:

copyVal = curState();
copyVal = "index.php#"+copyVal;

function for_action() 
{
   document.getElementById('myForm').action = copyVal;
}

Now, In form tag:
<form id="myForm" onsubmit="for_action();" method = "post" >
  • This is because your function curState(); is returning undefined. Make sure that the url you are testing on has the '#' in it. I tried it using a static url and the code is working. –  Jul 29 '15 at 06:55
  • Everythings looks right. The url has # and exactly as you gave. – asax Jul 29 '15 at 07:04
  • remove 'document.write()' from return statement in your curState() function. We are assigning value back to variable so can't use it there. –  Jul 29 '15 at 07:20
  • Your solution works. But only takes previous value. We have to hard press the url to get curent value. in the url url.com/index.php#44/234 .. the #44/234 part changes dynamically... so when we send value via post it has to grab the most recent value.. not the one that was hard pressed. Think like in google maps. The coordinate in the URL changes as we move around. My values also changes. – asax Jul 29 '15 at 07:26
0

First figure out your problem:

PHP is a server side scripting language. Server-side scripting is distinguished from client-side scripting where embedded scripts, such as JavaScript, are run client-side in a web browser, but both techniques are often used together.

So codes under <?php tag will run first in server side and give output as HTML and/or JavaScript and/or CSS respectively. So

<?php  
  $copyVal = " <script> curState(); </script> ";
  echo $copyVal; // displays value in browser perfectly
?>

this code will run in the server and first assign <script> curState(); </script> as a string in php variable $copyVal. And secondly, when you echo $copyVal, this will output <script> curState(); </script> in your source code.

AS <script> curState(); </script> is a Javascript code as well so curState(); function call your javscript function you have define earlier. On that function you have used document.write to write something in your browser, so you see the output in your browser. But if you see the source code of your page, you can still see that there is no specific Hash value but a JavaScript code [<script> curState(); </script>].

As similar as that when you print your php variable $copyVal in

<form action="index.php#<?php echo $copyVal; ?>" method = "post" >

this variable output the JavaScript code rather than any hash value.

Second, How to do this?

You can do this in many way with php or JavaScript. If you want to do this with php, one simple way is that, first parse_url your current url. and than get the fragment value. like-

$url=parse_url("http://url.com/index.php#33/1032");
echo $url["fragment"]; //This variable contains the fragment

If you are not sure, how to grab the current url in php see Get the full URL in PHP

Community
  • 1
  • 1
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
0

you can take the link and use the function substr..it does what you want after a specific sign:

$id = substr($url, strrpos($url, '#') + 1);

this should do the job

Rares Biris
  • 195
  • 1
  • 4
  • 19