2

I am trying to assign values in javascript assigned from PHP and the use document.write() to output them. The problem is when I do this, a complete blank screen shows up but no errors are ever thrown. But if I take the PHP out and put in a value such as 'ABC' it works. And example of my code can be this:

var comment_text="<?php echo $value['comment_text'];?>";
var bodyelement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
newdiv.style.textAlign = 'center';
            newdiv.style.zIndex = 10001;
            newdiv.style.left = (<?php echo $comment_x;?>+getPos('browserwindow',"Left")-23) + 'px';
            newdiv.style.top = (<?php echo $comment_y;?>+getPos('browserwindow',"Top")-90) + 'px';
            newdiv.style.position = 'absolute';
newdiv.innerHTML = comment_text;
        bodyelement.appendChild(newdiv);

I do have an PHP error log and no errors are beign thrown either. The values are retrieved from the database, the probem comes with outputting them. *UPDATE* Ok, I had this problem before.

Basically a newline is created like this:

var comment_text="cool Beans

";

I have tried to remove the newline with string replace but doesn't seem to work. Why would a new line like this cause this error?

7
  • 1
    Did you view the source? Is the PHP outputted correctly? Commented Mar 22, 2011 at 13:04
  • Check your Apache logs for errors. If you're getting a blank screen it's possible that PHP errors are showing up in the log. Commented Mar 22, 2011 at 13:05
  • 1
    what is output -> is it $commnent_x, $comment_y, and $value['comment_text'] show up ? Commented Mar 22, 2011 at 13:06
  • +1 for php in html in a string in php to be passed to javascript Commented Mar 22, 2011 at 13:08
  • $commnent_x, $comment_y, etc has no affect. Only when I echo $value['comment_text'] does the blank screen occur Commented Mar 22, 2011 at 13:13

5 Answers 5

3

Your issue is cleary in the output from PHP. If you get a blank page, means you most likely have a PHP issue that is HALTING the processing of said page.

As PHP is parsed before anything is sent to the viewer, this will result in a blank / error page.

When you substitute your $value['comment_text'] for ABC you remove the location that causes the error.

I am going to assume that $value['comment_text'] is either a result of a function, or a Database query, try just outputting the $value['comment_text'] first, then worry about sticking it in JS (which will work if your PHP code works).

As I don't see any of your PHP code, I cannot help you further.

Sign up to request clarification or add additional context in comments.

4 Comments

The values are retrieved from the database yes. The values are retrieved because I've done a print_r($value) and everything output. So the information is there.
are your database results in array() form or object?
@Devin Dixon, you should escape the quotes in comment_text: <?php echo addslashes($value['comment_text']); ?>.
@Devin Dixon and Czechnology, I would recommend using json_encode() rather than addslashes(): var comment_text="<?php echo $value['comment_text'];?>" should be written as var comment_text=<?php echo json_encode($value['comment_text']);?>, which also does the quoting for you and supports special characters etc.
1

Use

var comment_text=String(<?php echo json_encode($value['comment_text']);?>);

instead of

var comment_text="<?php echo $value['comment_text'];?>";

This will protect you from cross-site-scripting attacks by escaping all special characters like backslashes, quotes or line feeds.

The String(...) ensures that comment_text has type String and is not interpreted as a number (if $value['comment_text'] is has a number type).

Comments

0

If PHP is causing an error (sounds like it is) you can turn on your error reporting to see the issues

error_reporting(E_ALL)

Comments

0

The solution was just using trim.

echo trim($value['comment_text']);

2 Comments

How is this a solution? What actually broke it? Spaces in your comment? OR your newline method?
Its a solution that apparentls worked sometimes but not others. json_encode works %100 of the time
0

I recommend you use a heredoc for the javascript code with %s in the js. and use sprintf to substitute the variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.