Jump to content

php variable in javascript file


doddsey_65

Recommended Posts

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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.