PHP into Javascript
whats wrong with this:
<?php
$php_var = "from PHP";
?>
var js_var =
<?php
print $php_var
?>
document.write(js_var);
for this example I get nothing, and when adding PHP variables into a Javascript function all I get (if anything) is NaN echo'd on the page
Abhishek Reddy posted this at 05:07 — 27th December 2003.
He has: 3,348 posts
Joined: Jul 2001
Gotta have quotes around the value in the JS var.
var js_var="
<?php
print $php_var
?>
Busy posted this at 08:38 — 27th December 2003.
He has: 6,151 posts
Joined: May 2001
tried that, gives me an error with no results in IE6 but nothing in NS, can't track down the error in IE, says "Error: Not implemented" with a line and character that doesn't match anything.
[edit]found this thread with same outcome and script idea, doesn't help me thou as it was last year and never answered back, so here is my script.[/edit]
included js file #1 (one of 2)
var myCountdown = new Array();
var repeat = false;
function checkPlural(noun, value) {
noun = ((value == 1) || (value == 0)) ? noun : (noun += "s");
return noun;
}
function updateDisplay(text, id) {
var tag = document.getElementById(id);
if (tag.firstChild) {
tag.firstChild.nodeValue = text;
}
else {
textNode = document.createTextNode(text);
tag.appendChild(textNode);
}
return;
}
function doCountdown() {
for (i = 0; i < myCountdown.length; i++) {
if (!myCountdown[i].expired) {
var currentDate = new Date();
var eventDate = myCountdown[i].eventDate;
var timeLeft = new Date();
timeLeft = eventDate - currentDate;
msPerDay = 24 * 60 * 60 * 1000;
msPerHour = 60 * 60 * 1000;
msPerMin = 60 * 1000;
msPerSec = 1000;
daysLeft = Math.floor(timeLeft / msPerDay);
hoursLeft = Math.floor((timeLeft % msPerDay) / msPerHour);
minsLeft = Math.floor(((timeLeft % msPerDay) % msPerHour) / msPerMin);
secsLeft = Math.floor((((timeLeft % msPerDay) % msPerHour) % msPerMin) / msPerSec);
day = checkPlural("day", daysLeft);
hour = checkPlural("hour", hoursLeft);
minute = checkPlural("minute", minsLeft);
second = checkPlural("second", secsLeft);
if ((daysLeft == 0) && (hoursLeft == 0) && (minsLeft == 0) && (secsLeft == 0)) {
updateDisplay(myCountdown[i].onevent, myCountdown[i].tagID);
}
else {
if (daysLeft <= -1) {
updateDisplay(myCountdown[i].afterevent, myCountdown[i].tagID);
myCountdown[i].expired = true;
}
else {
updateDisplay(daysLeft + " " + day + " " + hoursLeft + " " + hour +
" " + minsLeft + " " + minute + ", and " +
secsLeft + " " + second + " left until " +
myCountdown[i].event, myCountdown[i].tagID);
repeat = true;
}
}
}
}
if (repeat) {
repeat = false;
window.setTimeout("doCountdown()", 1000);
}
else {
return;
}
}
function setEventDate(year, month, day, hour, minute, second) {
this.eventDate = new Date(year, month - 1, day, hour, minute, second);
return;
}
function addCountdown(countdown) {
myCountdown[myCountdown.length] = countdown;
return;
}
function Countdown() {
this.tagID = "";
this.eventDate = new Date();
this.setEventDate = setEventDate;
this.event = "";
this.onevent = "";
this.afterevent = "";
this.expired = false;
}
included js file #2 (where PHP variables are set)
var mycountdown = new Countdown();
var cd_year = "<?php echo $ends_year?>";
var cd_month = "<?php echo $ends_month?>";
var cd_day = "<?php echo $cd_day?>";
var cd_hours = "<?php echo $cd_hours?>";
var cd_minutes = "<?php echo $cd_minutes?>";
var cd_seconds = "<?php echo $cd_seconds?>";
mycountdown.tagID = "mycountdowndiv";
mycountdown.setEventDate(cd_year, cd_month, cd_day, cd_hours, cd_minutes, cd_seconds);
mycountdown.event = "The result";
mycountdown.onevent = "The moment you have been waiting for...!";
mycountdown.afterevent = "Should of been quicker";
addCountdown(mycountdown);
displayed on page via
<div id="mycountdowndiv"></div>
'PHP variables are from database, so for example set them from the main page to 2 days from todays date or something
oh and the body onload
onload=doCountdown();
it works if you set the numbers to something, but when php variables sent to the javascript best I can get to display is NaN ....
tried long PHP, short and most in between <?php, <?=, print, echo, with single quotes, double quotes, quotes inside, quotes outside, quotes inside and outside, brackets, brackets with quotes ...
it has to be something simple as it works without the php.
Abhishek Reddy posted this at 10:17 — 27th December 2003.
He has: 3,348 posts
Joined: Jul 2001
What does the output look like? Can you paste the JS as it is when PHP sets the value?
JeevesBond posted this at 12:49 — 27th December 2003.
He has: 3,956 posts
Joined: Jun 2002
Try removing the PHP and just using text values...Find whether it is the PHP or JS which is at fault.
Busy posted this at 22:33 — 27th December 2003.
He has: 6,151 posts
Joined: May 2001
output is :
NaN days NaN hours NaN minutes, and NaN seconds left until The result
if you change this line
mycountdown.setEventDate(cd_year, cd_month, cd_day, cd_hours, cd_minutes, cd_seconds);
to numbers, ie:
mycountdown.setEventDate(2004, 12, 12, 0, 0, 0);
it will work, so its not getting the PHP values, but if I echo the PHP values (on the main page, not in the included js file) they are there but somehow not registering in the javascript file.
Thats why I gave that very basic example (post #1), it has to be something very simple I'm over looking - php related
Busy posted this at 22:51 — 27th December 2003.
He has: 6,151 posts
Joined: May 2001
if I do this on the main page:
<?php
$php_var = "from PHP";
?>
var js_var="
<?php
print $php_var
?>
document.write(js_var);
var cd_year = "
<?php
echo $ends_year
?>
var cd_month = "
<?php
echo $ends_month
?>
var cd_day = "
<?php
echo $cd_day
?>
var cd_hour = "
<?php
echo $cd_hour
?>
var cd_min = "
<?php
echo $cd_min
?>
var cd_sec = "
<?php
echo $cd_sec
?>
the output is:
--------------------------------------------------
---------------------------------------------------
var cd_year = "2003"; var cd_month = "12"; var cd_day = "2"; var cd_hour = "3"; var cd_min = "13"; var cd_sec = "55";
so the PHP variables do have values but the PHP to Javascript isn't displaying
Suzanne posted this at 22:53 — 27th December 2003.
She has: 5,507 posts
Joined: Feb 2000
so you have the php in a .js file? How would that get parsed? The JavaScript won't be parsed as php, it's a type text/javascript?!
Busy posted this at 22:57 — 27th December 2003.
He has: 6,151 posts
Joined: May 2001
the main script has PHP in .js file, but last example isn't (but does have the type)
PHP should be parsed first, then values given to JS, no?
Suzanne posted this at 23:03 — 27th December 2003.
She has: 5,507 posts
Joined: Feb 2000
The problem, I think, is the order, no?
You have a .php page that is parsed -- things are sent without parsing, generally the included .js are just straight html, right? So they are included not as part of the .php page, just as JavaScript.
When using JavaScript and PHP, all the PHP has to be parsed FIRST, then the JavaScript can use it. It can't be parsed while inside a JavaScript file (.js) -- afaik, if the JavaScript is written on the page directly, not included, it should work. If it's included, it skips the parsing and the PHP won't be, well, parsed.
http://www.php-forum.com/p/viewtopic.php?t=2412
So you would be better to write the JavaScript with PHP.
Busy posted this at 23:25 — 27th December 2003.
He has: 6,151 posts
Joined: May 2001
Kinda makes sense not being able to parse inside .js file
I placed the PHP/javascript on the page and now I get a result, not the one I need but is something (result is "should of been quicker" so means its not getting the year or month correctly)
Oh and I figured out why the basic example wasn't working, anyone spot the problem now
<?php
$php_var = "from PHP";
?>
<style type="text/javascript">
var js_var =
<?php
print $php_var
?>
document.write(js_var);
style>
style should be script,
lack of sleep, dead lines and to much going on at the same time causes premature aging, insanity loss and wrinkles
Thanks
Suzanne posted this at 00:50 — 28th December 2003.
She has: 5,507 posts
Joined: Feb 2000
ha! I run into that problem myself and I still didn't see it! Doh!
ROB posted this at 01:34 — 28th December 2003.
They have: 447 posts
Joined: Oct 1999
fyi external js files and stylesheets can contain php code as long as they have a php extension.
For example:
<script language="javascript" type="text/javascript" src="./myjavascriptfile.php"></script>
Busy posted this at 02:16 — 28th December 2003.
He has: 6,151 posts
Joined: May 2001
Well after all that (finally got it working properly) it doesn't work in Opera6, looking at Operas help forums it seems the version 6 browser has some bugs where javascript is concerned, very slow on document.write and weird results as documented by the staff there.
Am downloading version 7 now (1.3kb/s) and see if that helps, if not have to either find another script or boogie up a browser redirect to a noscript tag lot.
becoming a isolated monk is looking more and more promising
[edit]Opera7 displays as it should[/edit]
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.