Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a variable I need to drop into a javascript file and can't seem to get any results. I've tried making the .js into a php and adding an echo but it doesn't work.

I have a file that calls this in it

<script type="text/javascript" src="/file.php"></script>

Inside of file.php I have this line

style: {
    color: '#<?php echo $_SESSION['colorOne']; ?>'
}

Everything works perfect when I replace the php with an actual color (#FFFFFF). I run into problems when I add the php.

share|improve this question
1  
style: is css , not javascript – Cherniv Sep 9 '13 at 5:49
    
Its part of a graphing script that uses it. – Milksnake12 Sep 9 '13 at 5:50
    
style is used for styling(css). Make your question clear – Moeed Farooqui Sep 9 '13 at 5:50
    
@Cherniv It may be a snippet from an Object literal. The : after style wouldn't really belong if CSS. – Jonathan Lonowski Sep 9 '13 at 5:50
    
@JonathanLonowski you're 100% , i need to drink some coffee – Cherniv Sep 9 '13 at 5:52
up vote 2 down vote accepted

PHP can emulate any content you'd like, even Images, PDF and Office files.

First, don't confuse Javascript with CSS.

<link rel="stylesheet" href="/file.php">

At the beginning of the file.php, make sure you start the session:

<?php
session_start();
?>
.style: {
    color: #<?php echo $_SESSION['colorOne']; ?>;
}

If this does not work, you should debug if your session is init and working correctly, like make a new PHP file and put in <?php session_start(); print_r($_SESSION); ?>

share|improve this answer
    
why did you initiate session here? !!!! – mithunsatheesh Sep 9 '13 at 5:51
    
Why would you use a session that you didn't initiate? – DanFromGermany Sep 9 '13 at 5:52
2  
adding session_start() worked, Thanks! – Milksnake12 Sep 9 '13 at 5:53

You need to call session_start() function before getting value of session, so, you need to put:

session_start();

At the top of that file.

Also, <script type="text/javascript" src="/file.php"></script> is for JavaScript file, not external style sheet, and last note is you can print the value without single quotes:

color: #<?php echo $_SESSION['colorOne']; ?>
share|improve this answer
    
Repeated answer but this got it working, Thanks!' – Milksnake12 Sep 9 '13 at 5:55
    
not repeated, there is just 20 seconds different! – user1646111 Sep 9 '13 at 6:03
1  
SO definitely needs a "post at the same time buddy" badge. For answers posted within 30 seconds. – bart s Sep 9 '13 at 6:19

Your file needs to be processed by PHP when it gets requested. When you are on Apache you need to add

AddType application/x-httpd-php .MYFILEEXTENSION

to your .htaccess file. im not quite sure about nginx.

As a general idea, i've allways done this in the index.php file. Just print something into a global variable like

window.phpTransitionVariables = { ... };

with a script tag like this

<script type="text/javascript">
    <? //print my php variables
</script>
share|improve this answer

write

<?php
header('content-type: text/css');
?>

on begin from the css file with php extension

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.