Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
LinkBack Thread Tools Display Modes
Share |
  #1 (permalink)  
Old 12-03-2011, 03:29 PM
vectro's Avatar
Contributing Member
 
Join Date: 12-29-08
Location: U.S.A.
Posts: 436
iTrader: 0 / 0%
Embedded .js File Not Showing Up

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:

Code:
<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):

Code:
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.
__________________
Resources for Proxy Webmasters
 
Reply With Quote
  #2 (permalink)  
Old 12-03-2011, 06:30 PM
Contributing Member
Latest Blog:
None

 
Join Date: 11-30-11
Posts: 121
iTrader: 0 / 0%
It looks like you php isn't getting parsed, as it is raw in the code, and causing a javascript error.
 
Reply With Quote
  #3 (permalink)  
Old 12-04-2011, 09:59 AM
Junior Member
Latest Blog:
None

 
Join Date: 07-12-08
Posts: 48
iTrader: 0 / 0%
Vectro,

Simply rename list.js to list.php (or list.js.php for better organization) and include it the same way:
Code:
<script type="text/javascript" src="http://www.vectrotest.co.cc/list.php"></script>
or(correspondingly)
Code:
<script type="text/javascript" src="http://www.vectrotest.co.cc/list.js.php"></script>
.
 
Reply With Quote
  #4 (permalink)  
Old 12-04-2011, 12:01 PM
Contributing Member
Latest Blog:
None

 
Join Date: 11-30-11
Posts: 121
iTrader: 0 / 0%
IMO I would try and figure out a way to take the PHP out of the javascript all together.
 
Reply With Quote
  #5 (permalink)  
Old 12-04-2011, 05:51 PM
Super Moderator
Latest Blog:
None

 
Join Date: 11-11-11
Location: Copenhagen, Denmark
Posts: 3,392
iTrader: 1 / 100%
Looks like 2WDHost.com is right, the php-script is not processed by php, because it dose not end in .php - The server dont know that it's a php-script and needs to be processed by php if the filename don't end with .php

On a sidenote, you will probably have another problem, my guess is that since the javascript should be processed by php, the output will be dynamic. Since javascript is cached on the client(like stylesheet), and some times on the server, and is not refreshed on each visit, you will need to add some kind of random string to the script to make sure a new new copy is loaded every time. Eg.:
HTML Code:
<script type="text/javascript" src="http://www.vectrotest.co.cc/list.php?somerandomstring"></script>
 
Reply With Quote
  #6 (permalink)  
Old 12-05-2011, 01:25 AM
JohnnyS's Avatar
Contributing Member
 
Join Date: 07-05-11
Location: philippines
Posts: 312
iTrader: 0 / 0%
Quote:
Originally Posted by vectro View Post
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:

Code:
<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):

Code:
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.
if you don't want to change list.js to list.js.php or list.php,
you may try this solution..

1. rename physical file list.js to list.php
2. then at the top of list.php add,
PHP Code:
<?php header("Content-type: text/javascript"); ?>
3. create an .htaccess file, if you already have one proceed to step 4
4. add this..
Code:
RewriteRule ^list.js list.php [QSA]
 
Reply With Quote
  #7 (permalink)  
Old 12-05-2011, 11:55 AM
vectro's Avatar
Contributing Member
 
Join Date: 12-29-08
Location: U.S.A.
Posts: 436
iTrader: 0 / 0%
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.

@JohnnyS

That RewriteRule was a good idea.
__________________
Resources for Proxy Webmasters

Last edited by vectro; 12-05-2011 at 12:10 PM.
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Link Around an Embedded swf File Omniscient Coding Forum 7 02-14-2011 06:50 AM
Change background of embedded SWF file Suchi Naik Coding Forum 1 11-07-2009 05:52 AM
/js file showing up - problem? SEO Dave Web Design Lobby 6 12-12-2008 04:56 PM
Embedded Video wideopen Tech Talk 3 05-05-2007 03:35 AM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 06:07 AM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2014 Escalate Media




Search Engine Optimization by vBSEO 3.6.0 RC 2 ©2011, Crawlability, Inc.