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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.