Jump to content

this php function with javascript just isn't working


pouncer

Recommended Posts

[code]
function GetGreeting() {
return '<script>
var objDate = new Date();
if (objDate.getHours() < 12) document.write("Good morning");
elseif (objDate.getHours() < 16) document.write("Good afternoon");
else document.write("Good evening");
</script>';
}
[/code]

It's a function inside my php class, but when i do $log->GetGreeting() it just doesn't work.
If I use this

[code]
function GetGreeting() {
?>
<script>
document.write("dfsdfsd");
</script>
<?php
}
[/code]

then $log-GetGreeting() puts  dfsdfsd on the page - fine.

But this doesnt work:

[code]
function GetGreeting() {
?>
<script>
var objDate = new Date();
if (objDate.getHours() < 12) document.write("Good morning");
elseif (objDate.getHours() < 16) document.write("Good afternoon");
else document.write("Good evening");
</script>
<?php
}
[/code]
Then I think the problem is with your javascript code. View the source, if everything is printed to the html page, then it worked. If your HTML page contains this:
<script>
var objDate = new Date();
if (objDate.getHours() < 12) document.write("Good morning");
elseif (objDate.getHours() < 16) document.write("Good afternoon");
else document.write("Good evening");
</script>

Then the problem is NOT the PHP, but the JS. Does JS have an elseif construct?
Why are you doing this in Javascript, when it can done in PHP just as easily:
[code]<?php
if (date('G') < 12) echo 'Good Morning';
elseif (date('G') < 16) echo 'Good Afternoon';
else echo 'Good Evening';
?>[/code]

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.