Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-04-2012, 06:50 PM   PM User | #1
failbot8000
New to the CF scene

 
Join Date: Sep 2012
Location: Buffalo
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
failbot8000 is an unknown quantity at this point
Cool Javascript calling PHP

Hello,

I am trying to write a javascript function to periodically update a div element. I want to use PHP to get the actual information. I know the periodic update works, but I can't get anything to display.

My code:

Code:
<script>
function updateDateTime()
{

<?php
	ob_start();
	$today = getdate();
	echo $today[month] . ' ' . $today[mday] . ', ' . $today[year] . ' ' . ($today[hours] >= 12 ? $today[hours] - 12 : $today[hours]) . ':' . ($today[minutes] < 10 ? ('0' . $today[minutes]) : $today[minutes]) . ($today[hours] >= 12 ? 'pm' : 'am');
	$string=ob_get_contents();
	ob_end_clean();
	print("var outputString = " . $string . " ;");
	print("document.getElementById('datediv').innerHMTL=outputString;");
?>

}

var int=self.setInterval(function(){clock()},1000);
function clock()
{
	updateDateTime();
}

</script>
Any ideas?
failbot8000 is offline   Reply With Quote
Old 10-04-2012, 07:17 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,636
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yeah, you can't do this at all.
PHP cannot be called by a JS function. So your javascript will always be:
PHP Code:
<script>
function 
updateDateTime()
{
var 
outputString October 42012 6:06pm;
document.getElementById('datediv').innerHMTL=outputString;
}

var 
int=self.setInterval(function(){clock()},1000);
function 
clock()
{
    
updateDateTime();
}

</script> 
To have PHP called, you need to implement an AJAX solution and call a PHP script in the background. I don't know if I'd recommend doing that every second, and for a clock that should all be done in the client side, not the server side. The server side can take responsibility of providing the initial time if you are looking for server specific time instead of a client time.
Fou-Lu is offline   Reply With Quote
Old 10-04-2012, 08:19 PM   PM User | #3
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
do a forum search for "refresh div ajax" and you will get quite a few examples to learn from. It's a fairly common task so there are alot of examples.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:57 PM.


Advertisement
Log in to turn off these ads.