Pioden Posted June 2, 2008 Share Posted June 2, 2008 Hi I'm using PHP to create a javascript file. The problem is my output doesn't appear to be valid :-( Can anyone suggest where I'm going wrong? I'm new to javascript so all help is appreaciated. My code looks like this. <?php require_once('../config.php'); require_once('session.php'); // Select images from resources db $sql = "SELECT res_name FROM resources WHERE res_type = 'img'"; // LIMIT $offset, $limit $result = @mysql_query($sql,$connection) or die("Couldn't execute images query.". $php_errormsg . mysql_error()); while ($row = mysql_fetch_array($result)) { $res_name = $row['res_name']; $display .= "document.write(\"<tr><td width=\"130\"><img src=\"../resources/images/$res_name\"></td><td>$res_name</td></tr>\");"; } echo "Header(\"content-type: application/x-javascript\");"; echo "document.write(\"<table>\");"; echo "$display"; echo "document.write(\"</table>\");"; ?> I then call this file from another file with <script type="text/javascript" src="my_file.php"></script> Quote Link to comment Share on other sites More sharing options...
hansford Posted June 2, 2008 Share Posted June 2, 2008 you must echo "<script language='javascript'>" before echoing document.write and then after all the js echo "</script>"; Quote Link to comment Share on other sites More sharing options...
Pioden Posted June 2, 2008 Author Share Posted June 2, 2008 I don't think so. That seems to be covered by the header. If I comment out individual lines I *think* the problem lies with $display. $display .= "document.write(\"<tr><td><img src=\"../resources/images/$res_name\"></td><td>$res_name</td></tr>\");"; This seems to break the Javascript - I just don't know why!! :-( Quote Link to comment Share on other sites More sharing options...
hansford Posted June 2, 2008 Share Posted June 2, 2008 ok I see what youre doing-sorry. Heres the fix: --------------------- echo "Header(\"content-type: application/x-javascript\");"; should be just the following with no echo ------------------------ Header('content-type: application/x-javascript'); Quote Link to comment Share on other sites More sharing options...
obsidian Posted June 2, 2008 Share Posted June 2, 2008 Why are you using application/x-javascript for your header? Whenever I have to dynamically generate a JavaScript file, I just use text/javascript. I don't know that this would actually cause an issue, but it's worth a try. Quote Link to comment Share on other sites More sharing options...
Pioden Posted June 2, 2008 Author Share Posted June 2, 2008 God this is weird!! I reckon this must be something like a parsing error which none of us can see! I'm trying this now ... <script type="text/javascript"> <?php require_once('../config.php'); require_once('session.php'); require_once('../functions.php'); // Select images from resources db $sql = "SELECT res_name FROM resources WHERE res_type = 'img'"; // LIMIT $offset, $limit $result = @mysql_query($sql,$connection) or die("Couldn't execute images query.". $php_errormsg . mysql_error()); while ($row = mysql_fetch_array($result)) { $res_name = $row['res_name']; $display .= "document.write(\"<tr><td><img src=\"../resources/images/$res_name\"></td><td>$res_name</td></tr>\");"; } ?> document.write("<table>"); document.write("<tr><td><img src=\"../resources/images/$res_name\"></td><td>$res_name</td></tr>"); document.write("<tr><td><img src=\"../resources/images/$res_name\"></td><td>$res_name</td></tr>"); document.write("<tr><td><img src=\"../resources/images/$res_name\"></td><td>$res_name</td></tr>"); <?php // echo "$display"; ?> document.write("</table>"); </script> With $display commented out I get an output in my browser. BUT the image source still has the slashes in front of the quotes. Erase the slashes and the output is not displayed. Does this ring any bells with someone? I acn't see what's wrong myself. Quote Link to comment Share on other sites More sharing options...
hansford Posted June 2, 2008 Share Posted June 2, 2008 similar code works fine for me, but I can't get it to display if I echo the header() or if I don't use the single quotes with document.write like: $doc = "document.write('img src=\"./images/2.jpg\">');"; Quote Link to comment Share on other sites More sharing options...
Pioden Posted June 2, 2008 Author Share Posted June 2, 2008 Thanks for the tip hansford. I tried your suggestion but no success. Can anyone post some similar code which they know works. I could do with something to dissect to analyse what's wrong. TIA Huw Quote Link to comment Share on other sites More sharing options...
haku Posted June 2, 2008 Share Posted June 2, 2008 I can't see where you have actually told us what the problem is yet! Quote Link to comment Share on other sites More sharing options...
Pioden Posted June 2, 2008 Author Share Posted June 2, 2008 The outputted javascript doesn't parse properly - it doesn't appear to be valid javascript even though superficially everything *looks* just fine. Quote Link to comment Share on other sites More sharing options...
hansford Posted June 2, 2008 Share Posted June 2, 2008 heres some simple code replace the image directory and imagename with your files and see if it displays -------------------- heres the writescript.php code ----------------- <?php $doc = "document.write('<img src=\"./images/2.jpg\">');"; Header('content-type: application/x-javascript'); echo $doc; ?> ------------------ heres the writescript.html code ------------------------ <html> <body> <script type="text/javascript" src="writescript.php"></script> </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
Pioden Posted June 2, 2008 Author Share Posted June 2, 2008 Thanks hansford - that worked. I'll take a break and then try and rebuild everything based on your code. I'll get back here tomorrow with an update. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
hansford Posted June 2, 2008 Share Posted June 2, 2008 Im just glad we got it fixed. I re-did your code minus the database stuff to try and see what you were seeing - this works. ---------------------------------- $res_name = "2.jpg"; $display = ""; for($i = 0; $i < 10; $i++){ $display .= "document.write('<tr><td width=\"130\"><img src=\"../resources/images/$res_name\"></td><td>$res_name</td></tr>');"; } Header('content-type: application/x-javascript'); echo "document.write('<table>');"; echo $display; echo "document.write('</table>');"; Quote Link to comment Share on other sites More sharing options...
Pioden Posted June 3, 2008 Author Share Posted June 3, 2008 Thanks hansford. I'm rapidly finding out that escaping characters (and other stuff) in JS seems to be a black art. I've added some more functionality to my code this morning and came across this beauty. $display .= "document.write('<tr><td id=\"<img src=\'t$base_address/resources/images/$res_name\' alt=\'Image\'>\" ><img src=\"$base_address/resources/images/thumbnails/$res_name\"></td></tr>');"; } generates this in html (notice the stray t before $base address in img src) <img src="thttp://192.168.217.128/eos_newydd_2/resources/images/121244560501-12-05_1621.jpg" alt="Image" /> If I remove the t the code outputs this <img src="../resources/images/121244560501-12-05_1621.jpg" alt="Image" /> The base address disappears and is replaced with ".." WTF !!! It doesn't seem to have any logic to it at all!! Quote Link to comment Share on other sites More sharing options...
hansford Posted June 3, 2008 Share Posted June 3, 2008 you shouldnt need the base address. If I'm putting an image tag in html I do like this because the server already knows the base. <img src="./images/img03.jpg" width='125' height='50'> Quote Link to comment Share on other sites More sharing options...
Pioden Posted June 3, 2008 Author Share Posted June 3, 2008 Base address is there because I may move the admin section to a part of the server that only I have FTP access to. That's why! Experimental at the moment but it could come in handy! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.