doddsey_65 Posted January 10, 2011 Share Posted January 10, 2011 can i add php variables into a js file? i have tried this: document.location = <?php echo $_SESSION['fwd_page']; ?>; but it doesnt work. Quote Link to comment https://forums.phpfreaks.com/topic/223913-php-variable-in-javascript-file/ Share on other sites More sharing options...
nderevj Posted January 10, 2011 Share Posted January 10, 2011 You actually need to create a PHP file (with a PHP extension so that it is parsed by PHP's engine). Something like: // Script's Filename: javascript_location.php (note the extension) header("content-type: application/x-javascript"); echo "document.location = {$_SESSION['fwd_page']};"; The header is needed to have PHP and the web server generate a javascript file. Quote Link to comment https://forums.phpfreaks.com/topic/223913-php-variable-in-javascript-file/#findComment-1157214 Share on other sites More sharing options...
doddsey_65 Posted January 10, 2011 Author Share Posted January 10, 2011 the problem i have is that this is for my login system. the login page is login.php which sets variables and pulls the content from login_page.html. when they click login the posted data is posted via a javascript file called login_functions.js, which is using jquery to stay on current page without having to reload. this login_functions.js passes the posted data to login_process which check the info posted, then sends a response back to login_functions.js. if the response is good then login_functions.js redirects. i want it to redirect to the cookie which has been set(they are redirected to the page they were going to before having to login). so what about turning the page into login_functions.php and having: <script type="text/javascript"> to enclose the js and then: document.location = <?php echo $_COOKIE['fwd_page']; ?>; for the redirect. Quote Link to comment https://forums.phpfreaks.com/topic/223913-php-variable-in-javascript-file/#findComment-1157215 Share on other sites More sharing options...
nderevj Posted January 11, 2011 Share Posted January 11, 2011 Your theory of operation sounds good. You would probably need to update your "login_process" to know that its going to be communicating with a PHP script (containing javascript). On the other hand, I think you can use javascript to access cookie data directly. If my hunch is correct then you don't need to have a PHP-Javascript file. Quote Link to comment https://forums.phpfreaks.com/topic/223913-php-variable-in-javascript-file/#findComment-1157738 Share on other sites More sharing options...
doddsey_65 Posted January 11, 2011 Author Share Posted January 11, 2011 sorry i forgot to update this. I solved it by adding: document.location = "./login_redirect.php" then in login_redirect.php i would redirect to the correct page based on sessions. Quote Link to comment https://forums.phpfreaks.com/topic/223913-php-variable-in-javascript-file/#findComment-1157739 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.