0

I'm writing mostly in PHP, but one function requires me to use JavaScript. I need to access a PHP variable in my JavaScript. I've placed the following code between the <head> tags:

<script>
    $(document).ready(function(){
        $(document).keyup(function(e) {
            if ($('.pho_big').is(':visible') && e.keyCode==27) { 
                var js_link = '<?php echo $p_link; ?>';
                window.location.href = js_link;
            }
        });
    });
</script>

In short, when the esc key is pressed, I want to go to $p_link. So I'm trying to copy $p_link to js_link and go there. Instead of getting the contents of $p_link, my browser is trying to go to <?php%20echo($p_link)%20?>, which is obviously incorrect.

I've already gone here, here, and here, all of which seem to tell me to do exactly what I'm doing. My knowledge of JavaScript is near zero, so I'm probably missing something simple, but I don't know enough to know what or troubleshoot.

Community
  • 1
  • 1
  • It appears that `$p_link` is an empty string or something that echos to that. Also please note that the way your doing it PHP is creating JavaScript on page load and so JavaScript will have a hard value of what `$p_link` is at the time of the page render and not when the they key is pressed. If it's an echo problem try `var_dump`ing it – JustGage Mar 10 '15 at 05:23
  • After your edit, if your getting the literal tags in your JavaScript that means it's not actually not passing through PHP, probably because you ended your file in `.js` and not `.php` – JustGage Mar 10 '15 at 05:25
  • in the (external) php file, $p_link is nothing more than a string of characters, with the occasional . and / The code I pasted is in a .tpl file (header.tpl), which is referenced in a .php file. Does that make a difference? – Rob Landauer Mar 10 '15 at 05:36
  • Anything that doesn't end in `.php` will not be interpreted. Also `include` and `require` can be thought of you just copying and pasting them inline into your file (from what I remember). – JustGage Mar 10 '15 at 05:40
  • I've used include with my php code, but I don't understand how it would help here. If I used it to paste my php variable in the JavaScript, you're telling me it still wouldn't intepret. And if I could simply handle the keystroke in my php code, I would. Either way, I think I only need one or two lines, not an entire file. – Rob Landauer Mar 10 '15 at 05:47
  • Basically your code is good, it's just not being processed by PHP which is most likey caused by this file not ending in `.php`. – JustGage Mar 10 '15 at 05:47
  • The only reason you would use include is if your where trying to process a file that is __not__ ending in `.php`. – JustGage Mar 10 '15 at 05:54
  • I just tried pasting the code at the top of my php file instead, and that seems to do the trick! Thanks for the pointers! – Rob Landauer Mar 10 '15 at 06:11
  • No problem, things can be tricky when you start. – JustGage Mar 10 '15 at 06:27

1 Answers1

1

it looks like <?php echo $p_link; ?> is not interpreted by php.so js_link contains string <?php echo $p_link; ?>.when you set window.location.href to <?php echo $p_link; ?> . browser tries to go to [less_than]?php%20echo($p_link)%20?[greater_than] which is url encoding of <?php echo $p_link; ?>.

(sorry,since I have low reputation, I cant post comment)

balajisoundar
  • 581
  • 2
  • 11
  • Yes, that's exactly what's happening, only with the actual [greater_than] tags in the url (I couldn't figure out how to make them appear normally here). WHY is that happening? – Rob Landauer Mar 10 '15 at 05:38
  • like @justGage told. your file might be ending with html or js extension.So php interpreter didn't had a chance to interpret it .check that file extension. – balajisoundar Mar 10 '15 at 05:45
  • you can create a variable like ``, somewhere in your php file (before including header.tpl) and use `window.location.href=window.js_link;` inside the event handling function. – balajisoundar Mar 10 '15 at 05:56