 |
| Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 24th, 2004, 01:42 PM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
javascript and php?
hey guys,
im using a countdown timer created in javascript there is a section of the code where you are able to set the date and time for the timer.
I want this to be possible in php, so that users to my site can set the start time of an auction.
heres what ive got so far:-
original javascript version
setcountdown(2004,05,25,00,00,01)
php version
setcountdown(</SCRIPT><?php echo date ("Y");?><SCRIPT>,</script><?php echo date ("M");?><script>,</SCRIPT><?php echo date ("d");?><SCRIPT>,00,00,01)
im not actually sure if i can use php with javascript, but this is the way ive attempted and although it doesnt give me any errors it doesnt display the timer?
any help?
CHEERS
ASH:D
__________________
My new web design domain
www.askmultimedia.co.uk
|
|

May 24th, 2004, 01:50 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
PHP won't actually interface with Javascript.. it just outputs it, just like it would HTML. You don't need the </script> tags in there, cause when you add those your output is not going to be as JavaScript.
Perhaps you were going for something like this?
<?php
echo 'setcountdown('.date('Y').', '.date('M').', '.date('d').", 00, 00, 01);\n";
?>
HTH!
Regards,
Rich
::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
|
|

May 24th, 2004, 01:59 PM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
so i cant set values of javascript with php?
ive tried to create a countdown timer for auctions that are stored in a database which is started when an auction is created by a user.
is there any other way this is possible?
cheers
ASH:D
|
|

May 24th, 2004, 02:04 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Did you read the example Rich posted? That tells you how.
Also, why not Google for "auction countdown" php and you will probably find quite a bit.
And, yes, you can set JS variables with PHP, check out the P2P PHP FAQ.
HTH,
Snib
<><
|
|

May 24th, 2004, 02:05 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
Quote:
quote:Originally posted by Ashleek007
so i cant set values of javascript with php?
ive tried to create a countdown timer for auctions that are stored in a database which is started when an auction is created by a user.
|
Yes you can create Javascript with PHP. But one thing you have to remember is JavaScript is a client-side language and PHP executes on the server, there is no mechanism for the two to "talk" to eachother. You can use PHP to create and output JavaScript.
<script language='JavaScript' type='text/javascript'>
var hello = '<?php echo 'Hello, world!'; ?>';
</script>
When PHP processes the file it'll output the JS variable with a value of 'Hello, world!'. Just as with your date, PHP will output the date information and JavaScript will execute with that information on the client computer.
Does all that make sense?
Regards,
Rich
::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
|
|

May 24th, 2004, 02:17 PM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
erm yeah i think so?!?!,
so if i use the javascript to set variables for each part of this code as youve shown then call them in the javascript code such as:-
<script language='JavaScript' type='text/javascript'>
var year ='<? php echo $year; ?>';
var month ='<? php echo $month; ?>';
etc....
then call them here:-
setcountdown(year,month,day,hour,min,sec)?
would this work??
would this create a timer that doesnt reset everytime the code is run?
or if it is run once and displayed would it countdown to zero?
cheers
ASH
|
|

May 24th, 2004, 02:25 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
I think your code would work.
|
|

May 24th, 2004, 02:46 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
|
|
Quote:
quote:Originally posted by Ashleek007
would this work??
would this create a timer that doesnt reset everytime the code is run?
or if it is run once and displayed would it countdown to zero?
|
Yeah that works too. Or you can simply output the date information into the function call like I showed you above:
echo 'setcountdown('.date('Y').', '.date('M').', '.date('d').", 00, 00, 01);\n";
As far as PHP is conserned its just outputting plain text, it doesn't care if it's JavaScript, HTML, CSS or whatever else. All you have to do is rememeber to escape quotes and other special characters.
Will it not reset every time the page is loaded? Maybe, maybe not. You're depending on the time on the server. I think it could work. As you have it the current year month and day are output on page load. I assume you're going to do the same for hours, minutes and seconds, at which point I think it could work the way you expect it to.
Regards,
Rich
::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
|
|

May 24th, 2004, 02:51 PM
|
|
Authorized User
|
|
Join Date: Jan 2004
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hmmmm, it works kind of
im trying to set the values of varibales in php to todays date and use them in javascript. This is what ive done:-
//php variables
$year = date("Y");
$month = date("M");
$day = date("d");
this doesnt work?
it produces NaN days when shown on the site?
i dont understand??
:DCheers
ASH
|
|

May 24th, 2004, 02:56 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
View the source of the page and tell us what the values of the JS variables end up being.
Snib
<><
|
|
 |