mcmuney Posted March 1, 2008 Share Posted March 1, 2008 Does PHP work the same way inside a .js file as it does on a .php file? Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/ Share on other sites More sharing options...
trq Posted March 2, 2008 Share Posted March 2, 2008 If you setup your server to parse .js files as php, yes. AddType application/x-httpd-php .php .js Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481249 Share on other sites More sharing options...
mcmuney Posted March 2, 2008 Author Share Posted March 2, 2008 Well, I need the .js to function as both, will the server parsing the .js file as .php disable any js functions? Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481301 Share on other sites More sharing options...
trq Posted March 2, 2008 Share Posted March 2, 2008 Well, I need the .js to function as both, will the server parsing the .js file as .php disable any js functions? No. Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481343 Share on other sites More sharing options...
mcmuney Posted March 2, 2008 Author Share Posted March 2, 2008 Ok, I added the suggested code on the .htaccess file. Then I just entered the code below in my .js file, but the script that's utilizing the .js file stopped working: $sql = mysql_query("SELECT link_id,bid_amount FROM tbl_bids ORDER BY DESC LIMIT 1"); $row = mysql_fetch_array($sql); echo $row[bid_amount]; Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481399 Share on other sites More sharing options...
trq Posted March 2, 2008 Share Posted March 2, 2008 That is some terrible php coding, can we see the actual .js file? Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481419 Share on other sites More sharing options...
mcmuney Posted March 2, 2008 Author Share Posted March 2, 2008 What makes it so terrible, that's a basic SQL command. Anyhow, not sure if you need the full js file or not, but here's a top half: /* $sql = mysql_query("SELECT link_id,bid_amount FROM tbl_bids ORDER BY DESC LIMIT 1"); $row = mysql_fetch_array($sql); echo $row[bid_amount];*/ // URL zum kleinen Bild / URL to small image var pagerSmallImg = 'Need to pull URL from database here'; // URL zu page_s.swf / URL to small page swf var pagerSmallSwf = 'page_s.swf'; // URL zum großen Bild / URL to big image var pagerBigImg = 'Need to pull URL from database here'; // URL zu page_b.swf / URL to big page swf var pagerBigSwf = 'page_b.swf'; // Wackelgeschwindigkeit der Ecke 1-4 (2=Standard) // Movement speed of small page 1-4 (2=Standard) var speedSmall = 1; // Bild spiegelt sich in der aufgeschlagenen Ecke ( true | false ) // Mirror image ( true | false ) var mirror = 'true'; // Farbe der aufgeschlagenen Ecke wenn mirror false ist // Color of pagecorner if mirror is false var pageColor = 'ffffff'; // Zu öffnende URL bei klick auf die geöffnete Ecke // URL to open on page click var jumpTo = 'Need to pull URL from database here' // Öffnet den link im neuen Fenster (new) oder im selben (self) // Browser target (new) or self (self) var openLink = 'self'; // Öffnet das pagepeel automatisch wenn es geladen ist (false:deaktiviert | 0.1 - X Sekunden bis zum öffnen) // Opens page automaticly (false:deactivated | 0.1 - X seconds to open) var openOnLoad = false; // Sekunden bis sich das pagepeel wieder schließt, funktioniert nur im Zusammenhang mit der openOnLoad-Funktion // Second until page close after openOnLoad var closeOnLoad = 3; // Ecke in der das Pagepeel erscheinen soll (lt: linke obere Ecke | rt: rechte obere Ecke ) // Set direction of page in left or right top browser corner (lt: left | rt: right ) var setDirection = 'lt'; // Weiches einblenden des page wenn Bild geladen (0-5: 0=aus, 1=langsam, 5=schnell ) // Fade in page if image completly loaded (0-5: 0=off, 1=slow, 5=fast ) var softFadeIn = 1; /* * Ab hier nichts mehr ändern / Do not change anything after this line */ // Flash check vars var requiredMajorVersion = 6; var requiredMinorVersion = 0; var requiredRevision = 0; Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481459 Share on other sites More sharing options...
trq Posted March 2, 2008 Share Posted March 2, 2008 What makes it so terrible is the fact it has absolutely no error catching and attempts to use an undefined constant. Why its not working is becuase your php is not within <?php ?> tags. Try... <?php $sql = "SELECT link_id,bid_amount FROM tbl_bids ORDER BY DESC LIMIT 1"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $row = mysql_fetch_array($sql); echo $row['bid_amount']; } } ?> Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481460 Share on other sites More sharing options...
mcmuney Posted March 2, 2008 Author Share Posted March 2, 2008 I had tried <? ?> earlier, but didn't work, but the script worked <?php... thanks. But the php doesn't work inside the var statement, see below, it shows the php code instead of showing the result: var pagerSmallImg = 'Need to pull data from DB here like <?php echo $row[bid_amount];?>'; //it doesn't work if I use the 'bid_amount' Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481468 Share on other sites More sharing options...
trq Posted March 2, 2008 Share Posted March 2, 2008 Should work fine... var pagerSmallImg = 'Need to pull data from DB here like <?php echo $row["bid_amount"];?>'; Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481481 Share on other sites More sharing options...
mcmuney Posted March 2, 2008 Author Share Posted March 2, 2008 Nope, the result still shows the php code, see below: %3C?php%20echo%20$row[%22bid_amount%22];?%3E Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481492 Share on other sites More sharing options...
trq Posted March 2, 2008 Share Posted March 2, 2008 Did your first example work? It would appear that your file is not being parsed as php. Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481494 Share on other sites More sharing options...
trq Posted March 2, 2008 Share Posted March 2, 2008 Ok, I found a few threads around suggesting that you might only need... AddType x-httpd-php .php .js in your .htaccess file. I've not used the .htaccess file for this type of thing so I'm not sure. Its worth a try though. Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481497 Share on other sites More sharing options...
s0c0 Posted March 2, 2008 Share Posted March 2, 2008 I've had success forcing html files to parse PHP, but when I've tried it in other file types such as XML I have had bad luck. What are you trying to accomplish? Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481498 Share on other sites More sharing options...
mcmuney Posted March 2, 2008 Author Share Posted March 2, 2008 thorpe: No, the first didn't show any result. You might be right, because the script stopped working completely in IE browser (works in FireFox, what I mean by works is that the script still runs with the php code, not that I see database results). I've also tried the new htaccess line you suggested, but same result. s0c0: I'm running a script that pulls data from a .js file, which requires me to change the data often manually. I'm trying to automate the process by pulling the data from the database. Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481501 Share on other sites More sharing options...
trq Posted March 2, 2008 Share Posted March 2, 2008 Just to try something, can you rename the js file to php and see if it runs. Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481502 Share on other sites More sharing options...
thebadbad Posted March 2, 2008 Share Posted March 2, 2008 You could just serve a PHP file as javascript: js.php <?php header("Content-type: application/x-javascript; charset=utf-8"); ?> // javascript and PHP goes here, like: var somevar = document.getElementById("<?php echo $element; ?>"); And you would just link to the file like this: <script type="text/javascript" src="js.php"></script> Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481526 Share on other sites More sharing options...
mcmuney Posted March 2, 2008 Author Share Posted March 2, 2008 I tried renaming it to PHP, but the script stopped working. I didn't try renaming the var commands like "thebadbad" suggested because there are too many var commands. Is there a JS include command? This I can include a PHP file, which will have the results? Link to comment https://forums.phpfreaks.com/topic/93922-php-on-js-files/#findComment-481846 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.