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