Why is it that in php you can open up a script tag like so and do the following:
<?php
print("<script>");
print("alert('hey');");
print("</script>");
?>
but you can't do this:
<?php
print("<script>");
print("document.getElementById('fName').value = localStorage.getItem('FirstName');");
print("</script>");
?>
I just can't understand what the difference is, if you can get javascript to work inside php, why can't you tap into localstorage?
There is no dependency on the content of the Javascript. PHP does not "filter" your output and your browser won't recognize that it has been the output of a PHP script.
Does the script work if you don't output it that way and instead write it statically into your document? Please note that the script does not automatically add line breaks and possibly procudes invalid Javascript code if you forget the semicolon.
Can you paste the output of your browser's error console?
What browser are you viewing this in? IIRC, localStorage isn't even supported prior to IE8 for example. We're going to need more information about the client side of things for what is, when it comes down to it, a client-side Javascript problem.
As ymmd says, it doesn't matter that you're using PHP; in fact, it's an unnecessary complication for the purposes of this question.
You can't run the browser javascript in PHP. What you are doing is writting some text, that is send to the browser, and it interpret the text as javascript, and run it.
What you can do is write text javascript that wen run on the clientt calls back to the server with the reply (with ajax or other), but you will need to write all that communication.