Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Using Javascript Inside of english.php?


Goanna

Recommended Posts

Posted

Here is a little javascript I have that says different statements during different times of the day.

 

<script language="Javascript">
<!--
{
helixtime = new Date()
hrstime = helixtime.getHours()
if (hrstime < 4)                {timemsg = "You're up pretty late!"}
if (hrstime > 3 && hrstime <6)  {timemsg = "Good Morning!, You're up <B>really</B> early!"}
if (hrstime > 5 && hrstime <7)  {timemsg = "Good Morning!, You're up early!"}
if (hrstime > 6 && hrstime <12) {timemsg = "Good Morning!"}
if (hrstime > 11 && hrstime <18){timemsg = "Good Afternoon!"}
if (hrstime >17)                {timemsg = "Good Evening!"}
document.write(timemsg)}
// -->
</script>

 

I wanted to use it to personalize the welcome text, where OSC greets the user. I tried doing it like this,

define('TEXT_GREETING_PERSONAL', '<script language="Javascript">
<!--
{
helixtime = new Date()
hrstime = helixtime.getHours()
if (hrstime < 4)                {timemsg = "Welcome back <span class="greetUser">%s!</span> You're up pretty late!"}
if (hrstime > 3 && hrstime <6)  {timemsg = "Good Morning <span class="greetUser">%s!</span>, You're up <B>really</B> early!"}
if (hrstime > 5 && hrstime <7)  {timemsg = "Good Morning <span class="greetUser">%s!</span>, You're up early!"}
if (hrstime > 6 && hrstime <12) {timemsg = "Good Morning <span class="greetUser">%s!</span>"}
if (hrstime > 11 && hrstime <18){timemsg = "Good Afternoon <span class="greetUser">%s!</span>"}
if (hrstime >17)                {timemsg = "Good Evening <span class="greetUser">%s!</span>"}
document.write(timemsg)}
// -->
</script> Would you like to see which <a href="%s"><u>new products</u></a> are available to purchase?');

define('TEXT_GREETING_PERSONAL_RELOGON', '<small>If you are not %s, please <a href="%s"><u>log yourself in</u></a> with your account information.</small>');
define('TEXT_GREETING_GUEST', '<script language="Javascript">
<!--
{
helixtime = new Date()
hrstime = helixtime.getHours()
if (hrstime < 4)                {timemsg = "Welcome <span class="greetUser">Guest!</span> You're up pretty late!"}
if (hrstime > 3 && hrstime <6)  {timemsg = "Good Morning <span class="greetUser">Guest!</span>!, You're up <B>really</B> early!"}
if (hrstime > 5 && hrstime <7)  {timemsg = "Good Morning <span class="greetUser">Guest!</span>!, You're up early!"}
if (hrstime > 6 && hrstime <12) {timemsg = "Good Morning <span class="greetUser">Guest!</span>!"}
if (hrstime > 11 && hrstime <18){timemsg = "Good Afternoon <span class="greetUser">Guest!</span>!"}
if (hrstime >17)                {timemsg = "Good Evening Welcome <span class="greetUser">Guest!</span>!"}
document.write(timemsg)}
// -->
</script> Would you like to <a href="%s"><u>log yourself in</u></a>? Or would you prefer to <a href="%s"><u>create an account</u></a>?');

 

But that did not work. Then instead of opening and closing statements (<script language="Javascript"></script>) I tried it like this,

define('TEXT_GREETING_GUEST', '<body onLoad="javascript: [<!--
{
helixtime = new Date()
hrstime = helixtime.getHours()
if (hrstime < 4)                {timemsg = "Welcome <span class="greetUser">Guest!</span> You're up pretty late!"}
if (hrstime > 3 && hrstime <6)  {timemsg = "Good Morning <span class="greetUser">Guest!</span>!, You're up <B>really</B> early!"}
if (hrstime > 5 && hrstime <7)  {timemsg = "Good Morning <span class="greetUser">Guest!</span>!, You're up early!"}
if (hrstime > 6 && hrstime <12) {timemsg = "Good Morning <span class="greetUser">Guest!</span>!"}
if (hrstime > 11 && hrstime <18){timemsg = "Good Afternoon <span class="greetUser">Guest!</span>!"}
if (hrstime >17)                {timemsg = "Good Evening Welcome <span class="greetUser">Guest!</span>!"}
document.write(timemsg)}
// -->];"> Would you like to <a href="%s"><u>log yourself in</u></a>? Or would you prefer to <a href="%s"><u>create an account</u></a>?</body>');

 

And I still cant get it to work. How exactly can I do this? I would like to get this working. Any help is appreciated.

Posted

a different way to do this is to define each greeting separately - then where you want the greeting to show do an if statement on the time of day to print the relevant greeting.

Your online success is Paramount.

Posted

Just out of interest where is this pulling the time from ?

 

helixtime = new Date()

hrstime = helixtime.getHours()

Your online success is Paramount.

Posted

The problem is that PHP runs on the server and the JavaScript runs on the client. So you cannot mix the two up on the server and have the results sent to the client. It would be best to simply insert the JavaScript in Index.php file like this:

 

 

Around line 300:

 

 ? ? ? ? ?
<tr>
?<td class="main"><?php echo tep_customer_greeting(); ?></td>
</tr>

 

Add your JavaScript outside of the PHP tags so it runs on the client machine:

 

 ? ? ? ? ?
<tr>
?<td class="main">

<script language="Javascript">
<!--
{
?helixtime = new Date()
?hrstime = helixtime.getHours()
?if (hrstime < 4) ? ? ? ? ? ? ? ?{timemsg = "You're up pretty late!"}
?if (hrstime > 3 && hrstime <6) ?{timemsg = "Good Morning!, You're up <B>really</B> early!"}
?if (hrstime > 5 && hrstime <7) ?{timemsg = "Good Morning!, You're up early!"}
?if (hrstime > 6 && hrstime <12) {timemsg = "Good Morning!"}
?if (hrstime > 11 && hrstime <18){timemsg = "Good Afternoon!"}
?if (hrstime >17) ? ? ? ? ? ? ? ?{timemsg = "Good Evening!"}
?document.write(timemsg)}
// -->
</script>

<?php echo tep_customer_greeting(); ?></td>
</tr>

 

HTH

I'd rather be flying!

Posted

Well, how about if I put the PHP inside the Javascript instead of the other way around? Would that work?

 

I need to do it in a way so I can place the customer greeting inside of the statement at different areas, as puttting the word Guest or the customers name in front or in back of every sentance wouldnt look right, some of them need it to show up mid sentance.

 

Or, how could I do this same thing using PHP instead of Javascript? Can sommeone give me an example?

Posted

Okay, someone took the liberty of writing this same script for me in PHP instead of Java. here it is,

 

<?PHP 
$hours = date("h"); 
$ampm = date("a"); 



/* AM or PM */ 

if($ampm == "am") 
{ 
if ($hours < 4) { 
$msg = "Your up pretty late!"; 
} 
If ($hours > 4 and $hours < 6) { 
$msg = "Good Morning!, You're up <B>really</B> early!"; 
} 
If ($hours > 5 and $hours < 7) { 
$msg = "Good Morning!, You're up early!"; 
} 
If ($hours > 6 and $hours < 12) { 
$msg = "Good Morning!"; 
} 
} 
else 
{ 
if ($hours > 1 and $hours < 6) { 
$msg = "Good Afternoon!"; 
} 
If ($hours > 5){ 
$msg = "Good Evening!"; 
} 

} 

Echo "$msg"; 

?>

 

Now, I tried putting this inside english.php and then I put $msg inside of the define statement for user greetings, but it doesnt work. In fact, any way I enter it into english.php it messes it up, and none of the statements get parsed. I did remove the opening and closing tags and also the Echo "$msg"; from the statement as well before I inserted it. I put it directly before the define greetings area.

 

What would be the correct method of using this time script with the define statements for the user and guest welcome?

Archived

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

×
×
  • Create New...