Jump to content

Php on .js files?


mcmuney

Recommended Posts

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

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

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

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

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

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

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.