Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Reply
inserting javascript into a php file?
Old 02-14-2004, 01:49 AM inserting javascript into a php file?
Novice Talker

Posts: 11
Trades: 0
I have the cutest little javascript that shows the phases of the moon. I want to put it on a page on my php site- but I am having trouble getting it to show
I HAD it working, this morning, but I want to make the returns, text calls from the php languags file. And because it's so dang late where I am, I overwrote the backup file.. right now I am getting mostly a blank page.
can I post the scripts here, if anyone wants to explain to me how to do this? It's a cute script, and someone else might like to use it anyway. It works just fine on any html page
__________________


...when the going gets tough- the
tough get llamas
Dragonmom is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-14-2004, 11:54 PM
ACJavascript's Avatar
Humble Mod

Posts: 548
Location: CT, USA
Trades: 0
Please do post it
__________________

Please login or register to view this content. Registration is FREE
- 100 Satisfied Customers - Custom Programming and Web Development
ACJavascript is offline
Reply With Quote
View Public Profile Visit ACJavascript's homepage!
 
Old 02-15-2004, 02:01 AM here's the script...
Novice Talker

Posts: 11
Trades: 0
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta name="author" content="Alan Palmer, and Revised by DragonMother.com for the new millenium">
<meta name="description" content="A collection of examples of JavaScript and DHTML - Moon Phases">
<meta name="keywords" content="JavaScript, javascript, jscript, JScript, DHTML, dhtml, dynamic HTML, script, scripts, Netscape, Microsoft, moon phases">
<title>revised Moon Phases</title>
<!-- This script is featured at JavaScript Planet at
http://www.intricate.com/javascript/ Check out
JavaScript Planet for more scripts, links, etc.

Please leave this comment intact. -->

<script language="JavaScript" type="text/javascript">
<!--
function getMoonAge(year, month, day)
{
d = Math.floor(year/20)
r = year-(d*20) //r is the remainder of (year/20)

while (r>9)
{
r = r-19
}

r = r*11

while (r>29)
{
r = r-30
}

if (month<3)
{
month = month+2
}

r = r+month+day

if (year<100)
{
r = r-4
}
else
{
r = r-8.3
}

while(r>29)
{
r = r-30
}

while(r<0)
{
r = r+30
}

return r
}

function getMoonPhase(moonAge)
{
if (moonAge<2) return "new"
if (moonAge<5) return "waxing cresent"
if (moonAge<11) return "first quarter"
if (moonAge<13) return "waxing gibbous"
if (moonAge<16) return "full"
if (moonAge<20) return "waning gibbous"
if (moonAge<24) return "last quarter"
if (moonAge<29) return "waning cresent"
if (moonAge<30) return "new"
}

function getMoonPhaseImg(moonAge)
{
if (moonAge<2) return "moonnew"
if (moonAge<5) return "waxingcresent"
if (moonAge<11) return "firstquarter"
if (moonAge<13) return "waxinggibbous"
if (moonAge<16) return "moonfull"
if (moonAge<20) return "waninggibbous"
if (moonAge<24) return "lastquarter"
if (moonAge<29) return "waningcresent"
if (moonAge<30) return "moonnew"
}


monthNames = new Array(13)
monthNames[1] = "Jan"
monthNames[2] = "Feb"
monthNames[3] = "March"
monthNames[4] = "April"
monthNames[5] = "May"
monthNames[6] = "June"
monthNames[7] = "July"
monthNames[8] = "Aug"
monthNames[9] = "Sept"
monthNames[10] = "Oct"
monthNames[11] = "Nov"
monthNames[12] = "Dec"

dayNames = new Array(8)
dayNames[1] = "Sun"
dayNames[2] = "Mon"
dayNames[3] = "Tues"
dayNames[4] = "Weds"
dayNames[5] = "Thurs"
dayNames[6] = "Fri"
dayNames[7] = "Sat"

function getLongDate(dateObj)
{
theDay = dayNames[dateObj.getDay()+1]
theMonth = monthNames[dateObj.getMonth()+1]
theDate = dateObj.getDate()
theYear = dateObj.getFullYear()
return ""+theDay+", "+theMonth+" "+theDate+", "+theYear
}

function getNextFull(moonAge)
{
currMilSecs = (new Date()).getTime()
daysToGo = 15 - moonAge
while(daysToGo<2)
{
daysToGo = daysToGo+29
}
milSecsToGo = daysToGo*24*60*60*1000
nextMoonTime = currMilSecs+milSecsToGo
nextMoonDate = new Date(nextMoonTime)
return nextMoonDate
}

function getNextNew(moonAge)
{
currMilSecs = (new Date()).getTime()
daysToGo = 29 - moonAge
while(daysToGo<2)
{
daysToGo = daysToGo+29
}
milSecsToGo = daysToGo*24*60*60*1000
nextMoonTime = currMilSecs+milSecsToGo
nextMoonDate = new Date(nextMoonTime)
return nextMoonDate
}
//-->
</script>
</head>
<body bgcolor="#fffffff" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">

<script language="JavaScript" type="text/javascript">
<!--

theDate = new Date()
theYear = theDate.getYear()
theMonth = theDate.getMonth()+1
theDay = theDate.getDate()
theMoonAge = getMoonAge(theYear, theMonth, theDay)
theMoonPhase = getMoonPhase(theMoonAge)
document.write(" The moon is "+theMoonPhase+"<br>")

theMoonPhase = getMoonPhaseImg(theMoonAge)
document.write("<img src='"+ escape(theMoonPhase)+".gif' align='left' hspace='20'>")

document.write("<p>Today's date is: "+getLongDate(theDate)+"</p>")

document.write("<p>Next <strong>new</strong> moon: ")
document.write(""+getLongDate( getNextNew(theMoonAge) )+"</p>")
document.write("<p>Next <strong>full</strong> moon: ")
document.write(""+getLongDate(getNextFull(theMoonA ge))+"</p>")
document.write('<br clear=all>')

//-->
</script>
<hr><div align="center"><font size="-1"><script language="JavaScript" type="text/javascript">
<!-- // new options introduced by Bernhard Friedrich; should work in all browsers
// additional code to display date in Month Day, Year format by Robert Crooks
var lutext;
var lutime;
var ludm;
var ludd;
var ludy;
function sstr(a,b){ //extract substrings
ret=lutime.substring(a,b);
if (ret=="Jan" || ret=="01") ret="1";
if (ret=="Feb" || ret=="02") ret="2";
if (ret=="Mar" || ret=="03" || ret=="Mrz") ret="3";
if (ret=="Apr" || ret=="04") ret="4";
if (ret=="May" || ret=="05" || ret=="Mai") ret="5";
if (ret=="Jun" || ret=="06") ret="6";
if (ret=="Jul" || ret=="07") ret="7";
if (ret=="Aug" || ret=="08") ret="8";
if (ret=="Sep" || ret=="09") ret="9";
if (ret=="Oct" || ret=="Okt") ret="10";
if (ret=="Nov") ret="11";
if (ret=="Dec" || ret=="Dez") ret="12";
return ret;
}
lutime = unescape(document.lastModified);
if (lutime.length == 17) { // Netscape 3 and higher, Internet Explorer 4
ludm = sstr(0,2);
ludd = sstr(3,5);
ludy = sstr(6,8);
}

if (lutime.length == 19) { // Netscape 3 and higher, Internet Explorer 4 and higher, 4-digit year
ludm = sstr(0,2);
ludd = sstr(3,5);
ludy = sstr(6,10);
}

if (lutime.length == 25 || lutime.length == 24) { // Netscape 2
ludm = sstr(4,7);
ludd = sstr(8,10);
ludy = sstr(20,24);
}
if (lutime.length == 29) { // Opera 3
ludm = sstr(8,11);
ludd = sstr(5,7);
ludy = sstr(12,16);
}
if (lutime.length == 23) { // Internet Explorer 3
ludm = sstr(3,6);
ludd = sstr(7,9);
ludy = sstr(19,23);
}
lutext = "";

monthName = new Array(12)
monthName[1] = 'January'
monthName[2] = 'February'
monthName[3] = 'March'
monthName[4] = 'April'
monthName[5] = 'May'
monthName[6] = 'June'
monthName[7] = 'July'
monthName[8] = 'August'
monthName[9] = 'September'
monthName[10] = 'October'
monthName[11] = 'November'
monthName[12] = 'December'
yearNow = null
if (ludy.length==2) {
if (ludy >= 90) {
yearNow = 19
}
else {
yearNow = 20
}
}
else yearNow=""
lutext += ludd+" "+ monthName[ludm] +" "+ yearNow + ludy +" ";

// -->
</script></font>
</body>
</html>



If you want them, you can get the images at http:www.dragonmother.com/moonphases/
but they are messy I think...
__________________


...when the going gets tough- the
tough get llamas
Dragonmom is offline
Reply With Quote
View Public Profile
 
Old 02-15-2004, 02:07 AM
ACJavascript's Avatar
Humble Mod

Posts: 548
Location: CT, USA
Trades: 0
Wow, very nice script!

I can think of alot of people that would want to use this.

Do you know PHP?

If so then just place the HTML page within the php function you want and it should dispaly just fine.

If not then Just place the HTML into your favorite editor and save it as moonphase.php and you have a php script.

if you already have it in the php script and getting an error, what is the exact error?
__________________

Please login or register to view this content. Registration is FREE
- 100 Satisfied Customers - Custom Programming and Web Development
ACJavascript is offline
Reply With Quote
View Public Profile Visit ACJavascript's homepage!
 
Old 02-15-2004, 02:13 AM
Novice Talker

Posts: 11
Trades: 0
here's the php page as I *tried* to make it happen- i was able to put the javascript head part onto a separate file. The problem is that I don't knwo what I'm doing, really- I'm a woodworker and only got into php because I am too cheap to buy a fiinished shopping cart I downloaded the free one from OSC and have been spending time instead of money ever since
I'd like to make the "the moon is new" part much much longer- I have magical information to impart with each phase. I'd like to make that a php script, getting only the text that is needed for the phase the moon is in. At the moment, the page disappears as soon as th ejavascript starts!
http://www.dragonmother.com/moonphases.php
Quote:
<?php
/*
$Id: shipping.php,v 1.19 2002/07/21 23:38:57 hpdl Exp $

osCommerce, Open Source E-Commerce Solutions
http://www.oscommerce.com

Copyright (c) 2002 osCommerce

Released under the GNU General Public License
*/

require('includes/application_top.php');

require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_MOON);

$breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_MOON, '', 'NONSSL'));
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML <?php echo HTML_PARAMS; ?>>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<TITLE>DragonMother's Book Of Spells::Moon Phases</TITLE>
<BASE href="<?php echo (getenv('HTTPS') == 'on' ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<LINK rel="stylesheet" type="text/css" href="stylesheet.css">


<?php require(DIR_WS_INCLUDES . 'moonphases.js'); ?>
</HEAD>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<TABLE border="0" width="100%" cellspacing="3" cellpadding="3">
<TR>
<TD width="<?php echo BOX_WIDTH; ?>" valign="top"><TABLE border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="0" cellpadding="2">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //--> </TABLE></TD>
<TD valign="top"><TABLE border=0 cellspacing=0 cellpadding=0 >
<TR>
<TD><IMG src="1upleft.gif" border=0 cellpadding=0 height=40 width=40></TD>
<TD background="1top.gif" border=0 height=40></TD>
<TD> <IMG src="1upright.gif" border=0 height=40 width=40></TD></TR>
<TR><TD background="1left.gif" border=0 width=40></TD>

<!-- body_text //-->
<TD width="100%" valign="top"><TABLE border="0" width="100%" cellspacing="0" cellpadding="0">
<TR>
<TD><TABLE border="0" width="100%" cellspacing="0" cellpadding="0">
<TR>
<TD class="pageHeading"><?php echo HEADING_TITLE; ?></TD>
</TR>
</TABLE></TD>
</TR>
<TR>
<TD><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></TD>
</TR>
<TR>
<TD><TABLE border="0" width="100%" cellspacing="0" cellpadding="2">
<TR>
<TD class="main">
<script language="JavaScript" type="text/javascript">
<!--

theDate = new Date()
theYear = theDate.getYear()
theMonth = theDate.getMonth()+1
theDay = theDate.getDate()
theMoonAge = getMoonAge(theYear, theMonth, theDay)

theMoonPhase = getMoonPhase(theMoonAge) ;
document.write(/"<?php echo '<img src=/'"+ escape(theMoonPhase)+".gif' align="right" hspace='20'>"'); ?>
document.write("<?php echo 'The moon is <strong>"+theMoonPhase+"<br>") '); ?>
document.write("<?php echo 'Next <strong>new</strong> moon: ") ' ?>
document.write("<?php echo '<P>"+getLongDate( getNextNew(theMoonAge) )+"&nbsp;&nbsp;") '); ?>
document.write("<?php echo 'Next <strong>full</strong> moon: ") '); ?>
document.write("<?php echo '+getLongDate(getNextFull(theMoonAge))+"</p>") '); ?>
document.write('<?php echo '<br clear=all>') '); ?>
//-->
</script>
<NOSCRIPT>
</td>
</TR>
<TR>
<TD align="right" class="main"><BR><?php echo '<a href="' . tep_href_link(FILENAME_DEFAULT, '', 'NONSSL') . '">' . tep_image_button('button_continue.gif', IMAGE_BUTTON_CONTINUE) . '</a>'; ?></TD>
</TR>
</TABLE></TD></TABLE></TD>
<!-- body_text_eof //-->
<TD background="1right.gif" border=0 width=40></TD>

<TR>
<TD> <IMG src="1bottomleft.gif" border=0 height=40 width=40></TD>

<TD background="1bottom.gif" border=0 height=40></TD>

<TD> <IMG src="1bottomright.gif" border=0 height=40 width=40></TD>
</TR></TABLE></TD></TABLE>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<BR>
</Body>
</HTML>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
thanks in advance, for any help anyone is willing to give me!
__________________


...when the going gets tough- the
tough get llamas

Last edited by Dragonmom; 02-15-2004 at 01:03 PM..
Dragonmom is offline
Reply With Quote
View Public Profile
 
Old 02-15-2004, 02:18 AM
ACJavascript's Avatar
Humble Mod

Posts: 548
Location: CT, USA
Trades: 0
Try this:

replace <?php require(DIR_WS_INCLUDES . 'moonphases.js'); ?>

with

<script language="javascript" src="moonphases.js">
</script>
__________________

Please login or register to view this content. Registration is FREE
- 100 Satisfied Customers - Custom Programming and Web Development
ACJavascript is offline
Reply With Quote
View Public Profile Visit ACJavascript's homepage!
 
Old 02-15-2004, 12:55 PM
Novice Talker

Posts: 11
Trades: 0
I fell out last night, while you were helping me!
The error seems to be here;
Quote:
<script language="JavaScript" type="text/javascript">
<!--

theDate = new Date()
theYear = theDate.getYear()
theMonth = theDate.getMonth()+1
theDay = theDate.getDate()
theMoonAge = getMoonAge(theYear, theMonth, theDay)

theMoonPhase = getMoonPhase(theMoonAge) ;
document.write("<img src=/'"+ escape(theMoonPhase)+".gif' align="right" hspace='20'>")

document.write("The moon is <strong>"+theMoonPhase+"<br>") '); ?>
document.write("Next <strong>new</strong> moon: ") ' ?>
document.write("<P>"+getLongDate( getNextNew(theMoonAge) )+"&nbsp;&nbsp;")
document.write("Next <strong>full</strong> moon: ")
document.write("+getLongDate(getNextFull(theMoonAg e))+"</p>")
document.write("<br clear=all>")
//-->
I'm lousy at this! I've even bought books on php- and i can't get myself to read more than three pages!
but, you know, I really did alter the numbers to make ithe script correct for 2000! I don't know HOW I did, except for some trial and error...
I'll clean up the moon images or anyone who is using it can make new ones.
__________________


...when the going gets tough- the
tough get llamas
Dragonmom is offline
Reply With Quote
View Public Profile
 
Old 02-15-2004, 07:42 PM
ACJavascript's Avatar
Humble Mod

Posts: 548
Location: CT, USA
Trades: 0
I looked at that script you showed
lookat this part here:

theMoonPhase = getMoonPhase(theMoonAge) ;
document.write("<img src=/'"+ escape(theMoonPhase)+".gif' align="right" hspace='20'>")
document.write("The moon is <strong>"+theMoonPhase+"<br>") '); ?>

remove that (bold faced ?> ). see how that works.
__________________

Please login or register to view this content. Registration is FREE
- 100 Satisfied Customers - Custom Programming and Web Development
ACJavascript is offline
Reply With Quote
View Public Profile Visit ACJavascript's homepage!
 
Old 02-15-2004, 09:02 PM
Novice Talker

Posts: 11
Trades: 0
nope, that wasn't it.. I see a couple others that I left in there too, cleaned them out.
What's weird is that the failure seems to be in the first document.write staement. It doesn't matter which statement comes first. I can switch them around- the syntax coloring gizmo shows any first statement as incorrect.
Waah!
__________________


...when the going gets tough- the
tough get llamas
Dragonmom is offline
Reply With Quote
View Public Profile
 
Old 02-16-2004, 12:27 PM
ACJavascript's Avatar
Humble Mod

Posts: 548
Location: CT, USA
Trades: 0
Look here:

document.write("The moon is <strong>"+theMoonPhase+"<br>") ');

theres an extra ');


On the first document.write you have the function escape(theMoonPhase). But I dont see that function in your script. THat may be cuasing the error.
__________________

Please login or register to view this content. Registration is FREE
- 100 Satisfied Customers - Custom Programming and Web Development
ACJavascript is offline
Reply With Quote
View Public Profile Visit ACJavascript's homepage!
 
Old 02-16-2004, 09:43 PM
Novice Talker

Posts: 11
Trades: 0
Okay, I got rid of the extra ');
What is the escape function? It was in the original code, which worked fine- untill I re-arranged anything, like the list of write functions...
So I didn't know to take it out. But it still isn't going...
There must be something that encapsulates the javascript so the server won't try to read it as php- but I'm talking out my ***, really I don't know!
__________________


...when the going gets tough- the
tough get llamas
Dragonmom is offline
Reply With Quote
View Public Profile
 
Old 02-17-2004, 06:29 AM
scottfree's Avatar
Extreme Talker

Posts: 234
Location: Hamburg
Trades: 0
Hi there. I just tried the script really quickly, I am not too good with javascript. I just put the HTML into php quotes like this: <? echo " -->then the HTML here<-- "; ?>. Then replaced all " with \" and the file runs fine as a php document. I ran this on a local server and had no problems except that the images donīt show as I donīt have them.
Basically this means that there canīt be a problem with the javascript as it runs fine.
Good luck.
__________________
I think, therefore I am..... I think.
scottfree is offline
Reply With Quote
View Public Profile Visit scottfree's homepage!
 
Old 02-17-2004, 06:38 AM
scottfree's Avatar
Extreme Talker

Posts: 234
Location: Hamburg
Trades: 0
Basically as far as I know, if you want to insert any HTML or javascript or anything else into php, you can insert it as an echo""; statement and it is parsed straigth into HTML when the file is called.
Important to remember is that all double quotes (") need to be commented out (\") or it leads to problems when the php is parsed into HTML.
__________________
I think, therefore I am..... I think.
scottfree is offline
Reply With Quote
View Public Profile Visit scottfree's homepage!
 
Old 02-17-2004, 11:28 AM
Skilled Talker

Posts: 72
Trades: 0
I would use a javascript include to include javascript source. This saves considerable bandwidth usage and increases speed for non java enabled browsers and searchengines. It also reduces the size of the pages making them load quicker for everyone.

Another good speed trick is to set image sources to be http://www.yourdomain.com/1.jpg and http://yourdomain/1.jpg alternatively, this allows users twice the number of concurrent server gets.
__________________
.:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE
(such as a birthday)
Please login or register to view this content. Registration is FREE
Florist Comparison Shopping :.
alansmith is offline
Reply With Quote
View Public Profile
 
Old 02-17-2004, 12:16 PM
Novice Talker

Posts: 11
Trades: 0
Quote:
Originally posted by alansmith
I would use a javascript include to include javascript source. This saves considerable bandwidth usage and increases speed for non java enabled browsers and searchengines. It also reduces the size of the pages making them load quicker for everyone.

Alan, if this is what I think it is, it would have been my next question. Instead of the simple "the moon is new" sentences, I want to have a long paragraph for each phase. When I look at the page source, I see ALL the returns listed- so there would be seven long paragraphs loading invisibly, right? How can I make the script only show the one return per phase, even in the source? Can I do that?
I thought some sort of return(contents of file) I tried it as <?php require FILENAME ?> but when I looked at the source code, ALL the flies were read. Which sort of negated the whole exercise.



Thank you, guys, for helping me with this!
__________________


...when the going gets tough- the
tough get llamas
Dragonmom is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to inserting javascript into a php file?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.55633 seconds with 12 queries