php variable into javascript function??
how to pass php variable into javascript function??
i tried this way, but it doesn' work!!
<?php $year = "2002";?>
<script type="text/javaScript">
var theyear='<? echo "$year" ?>'
function setcountdown(theyear,themonth,theday,thehour,themin,thesec){
yr=theyear;mo=themonth;da=theday;hr=thehour;min=themin;sec=thesec
}
</script>
pls advice
Suzanne posted this at 03:42 — 14th November 2002.
She has: 5,507 posts
Joined: Feb 2000
<? $year = "2002";?>
<script type="text/javaScript">
var theyear='<? echo $year; ?>'
function setcountdown(theyear,themonth,theday,thehour,themin,thesec){
yr=theyear;mo=themonth;da=theday;hr=thehour;min=themin;sec=thesec
}
</script>
Abhishek Reddy posted this at 03:54 — 14th November 2002.
He has: 3,348 posts
Joined: Jul 2001
You probably also want to add a semicolon at the end of the line
var theyear='<? echo $year; ?>';
for safe practice.
Suzanne posted this at 04:09 — 14th November 2002.
She has: 5,507 posts
Joined: Feb 2000
I find that the semi-colon on the end of JavaScript lines causes more problems than not having it -- is your experience different, Abhi? I haven't written it a lot recently, so I'm kind of rusty on the particulars.
http://secrets.synapticimpulse.com/crap.php
http://secrets.synapticimpulse.com/crap.phps
My test page and source.
Suzanne
zollet posted this at 04:22 — 14th November 2002.
He has: 1,016 posts
Joined: May 2002
Instead of
<?php
echo $year;
?>
<?= means <?php echo
Abhishek Reddy posted this at 04:23 — 14th November 2002.
He has: 3,348 posts
Joined: Jul 2001
I don't think it matters much. I just think it's politically incorrect. I cringe at the sight of unfinished lines, mixed quotes, inconsistent tabbing, etc.
What problems have you had with semi-colons? I've never had any difficulties, except for when I use it in the wrong places (eg: function bob(); {}),
zollet posted this at 04:27 — 14th November 2002.
He has: 1,016 posts
Joined: May 2002
I didn't know you could write javascript without ending the lines. Maybe that is something they've added to make it easier for people that are new to javascript as god know how many times I've seen people forgetting them.
I agree with Abhi, even though it might work without the semi-colon, it _should_ be there.
Suzanne posted this at 04:30 — 14th November 2002.
She has: 5,507 posts
Joined: Feb 2000
Heh, in the wrong places, lol, those are damned hard to find, too.
I've just had some weird errors where lines that should be terminated seem to be terminating the script. It's probably me, though. I'll go over what I'm doing more carefully next time and see if I can see a particular pattern.
Zollet, I've heard that, but I've always been afraid to go perl on php (obfuscating the code instead of being explicit). Good reminder, though, thank you.
joyce posted this at 06:09 — 14th November 2002.
They have: 164 posts
Joined: Nov 2001
hi, thanks for the reply. i tried all tat u suggest but i still can't get the php variable passed over....
Abhishek Reddy posted this at 06:20 — 14th November 2002.
He has: 3,348 posts
Joined: Jul 2001
Do you get an error message? How are you able to tell that the variable is not being passed?
If you look at the links Suzanne posted, you will see that her code works. Is it similar to what you have?
joyce posted this at 06:31 — 14th November 2002.
They have: 164 posts
Joined: Nov 2001
no errors...my javascript is a countdown timer. i actually have to set the year, month, day, hour, minutes and second in the function but i intend to pass in the variables of yr, month..etc from php to javascript.
i know it's not working cos it display
NaN days, NaN hours, NaN minutes, and NaN seconds left until to buy this product!!
instead of the days, hour and minutes...
<?
$year = "2002";
$month = "11";
$day = "15";
?>
<script type="text/javaScript">
var theyear='<? echo $year; ?>'
var themonth='<? echo $month; ?>'
var theday='<? echo $day; ?>'
var thehour='00'
var themin='00'
function setcountdown(theyear,themonth,theday,thehour,themin,thesec){
yr=theyear;mo=themonth;da=theday;hr=thehour;min=themin;sec=thesec
}
setcountdown(theyear,themonth,theday,thehour,themin)
</script>
Abhishek Reddy posted this at 06:43 — 14th November 2002.
He has: 3,348 posts
Joined: Jul 2001
Check the output HTML source. If the values don't show up in the resulting code, then the problem lies with the javascript, rather than the PHP. If not, then we'll have to rethink the PHP.
Suzanne posted this at 15:30 — 14th November 2002.
She has: 5,507 posts
Joined: Feb 2000
Agreed, let's see how you're outputing the results.
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.