I have a block of JavaScript code that is designed to be embedded into any web page. It displays a list of items from a database. In the .js file is actually a lot of PHP code within JavaScript.

The problem: The cut-n-paste widget does not actually show up. It's just blank. There is no error data in Apache or PHP, so I am stumped. This is happening on all 3 of my servers and in both IE and Firefox.

Example: http://vectrotest.co.cc/list-widget.php

Half way down the page it says 'The widget looks like this:', but as you can see no widget is actually displayed there where it's supossed to be.


This is the JavaScript code designed to be embedded:

<p><script type="text/javascript" src="http://www.vectrotest.co.cc/list.js"></script></p>

This is what's actually inside of list.js (there is JavaScript mixed with PHP):

document.write('<style type="text/css">option {padding:1px 3px; }</style>');
<?php

require('./includes/config.php');
require('./includes/functions.php');
require('./includes/database.php');

$db = new Database();
if (!$db->connect($db_host, $db_user, $db_pass, $db_name))
        pretty_death($db->error);

$sql = "SELECT id, proxy, hits, time
                FROM {$db_tp}proxy
                ORDER BY time DESC
                LIMIT 0, 50";
$res = $db->query($sql);
?>
document.write('<form method="get" name="form" action="http://www.<?php echo $cfg['domain'] ?>/redirect.php" target="_blank" style="border:1px solid #ccc;padding:3px 0;font-size:10px;width:190px;text-align:center;">');
document.write('<b title="All Links Open In New Window/Tab">50 Newest Proxies</b><br />');
document.write('<select name="id" size="12" style="width:180px;border:1px solid #ccc;margin:3px 0;" onclick="document.form.submit();">');
<?php
while ($row = $db->fetch_assoc($res)){ ?>
document.write('<option value="<?php echo $row['id'] ?>"><?php echo rtrim(str_replace(array('http://','https://','www.'),'',$row['proxy']),'/') ?></option>');
<?php } ?>
document.write('</select><br /><a href="http://www.<?php echo $cfg['domain'] ?>/" title="Get A Free List Widget For Your Website or Blog!" target="_blank" style="color: black; text-decoration: none">Powered by <?php echo $cfg['domain'] ?></a></form>');

I'm not sure if this is a server problem, a browser problem a JavaScript problem or a PHP problem.

i dont think the js will read the php, regardless of the browser. js is client side, php is server side. i may be wrong.

try this instead:

rename it to list.php and include it with php (provided you are using php for the web page)

I'm pretty sure that js cannot read php but php can read js.

commented: Perfect and elegant solution +2

Thanks for your response, gotboots. Yes, as far as I know JavaScript has always been client-side and PHP is server-side.

One thing I neglected to mention is this works on other servers, like this one which is on a different hosting company: http://www.listproxysites.com/list-widget.php

I will still try renaming it and calling it the PHP way. That's an interesting idea.

Renaming it to list.php worked.

The .js file was not being parsed because the server doesn't have the proper file handlers. Additionally, setting the handlers in .htaccess didn't seem to work, either.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.