Jump to content

Generating valid JS from PHP


Pioden

Recommended Posts

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>

Link to comment
Share on other sites

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!! :-(

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>');";

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.