0

I'm trying to dump a PHP array to a JavaScript one. (without using any extra extensions) So far I've managed to make it dump the ID and titles of the text items it retrieves from the database but as soon as I try to dump the text item content the whole script breaks.

<script type="text/javascript">
var idList=new Array();
var titleList=new Array();
var contentList=new Array();
<?php
foreach($list["id"] as $index => $value)
{
    $content = htmlentities($list["tekst"][$index], ENT_QUOTES);
    echo('idList.push('.$list["id"][$index].');');
    echo('titleList.push("'.$list["title"][$index].'");');
    //echo('contentList.push("'.$content.'");');
}
?>
</script>

The line that breaks the whole script has been commented out. Here's one of the strings that is pushed to contentList:

&lt;p&gt;Ik ben onweer. ROMMELDEBOMMEL!&lt;/p&gt;
&lt;p&gt;Ik ben donker en duister maar ook heel belangrijk voor de natuur.&lt;/p&gt;
&lt;p&gt;ofzoiets... geen zin. lat0rzzzzzzzzzzzzzzzzzzzzzz&lt;/p&gt;

It's in Dutch but I assume you get the point.


EDIT: I tried using a method suggested in the 'linebreaks' question but the code still breaks. Here's the code it outputs:

contentList.push("&lt;p&gt;Ik ben onweer. ROMMELDEBOMMEL!&lt;/p&gt;"+
"&lt;p&gt;Ik ben donker en duister maar ook heel belangrijk voor de natuur.&lt;/p&gt;"+
"&lt;p&gt;ofzoiets... geen zin. lat0rzzzzzzzzzzzzzzzzzzzzzz&lt;/p&gt;");

EDIT #2: I noticed this error in my JS console "Uncaught SyntaxError: Unexpected token ILLEGAL"


EDIT #3: Switched to AJAX approach. Which makes this script obsolete. Thanks for the help guys. ;)

AniCator
  • 17
  • 4

4 Answers4

1

The issue is going to be that Javascript strings must begin and end on the same line unless you escape the newline characters. So, you'll need to strip out newlines (if they aren't important, just strip, otherwise replace with \n or <br /> or whatever makes sense for your context.

Info: http://www.willmaster.com/blog/javascript/strings-line-breaks.php

Interrobang
  • 16,984
  • 3
  • 55
  • 63
1

Javascript strings can't have line breaks like that in them - have a look at How do I break a string across more than one line of code in JavaScript? for various ways you can fix it.

Community
  • 1
  • 1
Michael Low
  • 24,276
  • 16
  • 82
  • 119
  • Even with that code it breaks as soon as I use $content. I tried using alert to be alerted etc. but even that breaks when I try to use $content. :S `alert("<p>Ik ben onweer. ROMMELDEBOMMEL!</p> "+ "<p>Ik ben donker en duister maar ook heel belangrijk voor de natuur.</p> "+ "<p>ofzoiets... geen zin. lat0rzzzzzzzzzzzzzzzzzzzzzz</p>");` – AniCator Dec 16 '11 at 10:35
  • Copying your alert to http://jsfiddle.net/zj3qF/1/ , it works with no errors. If you can change that to reproduce the error it would make it easier to fix. – Michael Low Dec 16 '11 at 10:42
  • Well this is what it currently outputs in the JS section http://jsfiddle.net/xVCqH/ – AniCator Dec 16 '11 at 10:48
  • You can also checkout the page here http://weer.patrickwobben.nl/admin/pagina.php of course it's the outputted HTML code so. – AniCator Dec 16 '11 at 10:52
  • You've still got line breaks in your JS code. http://jsfiddle.net/2wPHv/ is an example of what you've got vs what you need to change it to. – Michael Low Dec 16 '11 at 10:53
  • jsfiddle seems to have messed up the breaks because when I view the source code on the actual page (using Chrome) the line breaks are as you specified them in your fiddle. – AniCator Dec 16 '11 at 10:58
  • Interesting, it looks like a Chrome issue too then. View Source in Chrome shows no line breaks in the JS, but they are there if you View Source in IE or FF. – Michael Low Dec 16 '11 at 19:56
  • I've tried using substr to shorten the code so there would be no line breaks at all but it's still bugging out. Trying to analyze everything using FireBug now. – AniCator Dec 20 '11 at 10:28
  • As soon as I add the code for the contentList, all of the arrays breaks. It claims idList isn't defined while it has no problems with it when I don't fill the contentList. – AniCator Dec 20 '11 at 10:37
  • I noticed this: Uncaught SyntaxError: Unexpected token ILLEGAL – AniCator Dec 20 '11 at 11:24
1

Do you want to use json_encode?

tekknolagi
  • 10,663
  • 24
  • 75
  • 119
  • "I'm trying to dump a PHP array to a JavaScript one. (without using any extra extensions)" - first line of my post – AniCator Dec 20 '11 at 10:39
  • Right, that's what that does. That is not an extra extension; is built in. – tekknolagi Dec 20 '11 at 17:34
  • @AniCator forgot to tag. Also, it is best *not* to reinvent the wheel. – tekknolagi Dec 20 '11 at 17:34
  • Here's a late response to the reinvention part of you answer. I was working on this website for a school project and we try to avoid jQuery and other addons to make sure we have got a solid base. – AniCator Jan 15 '12 at 12:05
  • @AniCator this is not jQuery, and this is not an addon. standard JavaScript. use the language. – tekknolagi Jan 15 '12 at 18:02
1

Try to use this function

http://de3.php.net/manual/en/function.nl2br.php

or:

str_replace("\n","");

If you want to keep code line breaking in js, each line inside the string has to end with \n\

var string = 'test\n\
\n\
              testtestest\n\';
mat
  • 1,367
  • 1
  • 13
  • 18
  • I tried to use nl2br (also tried the same using str_replace("\n","
    ",htmlentities($lijst["tekst"][$index], ENT_QUOTES)) ) but for some reason the code still breaks.
    – AniCator Dec 16 '11 at 10:20
  • `str_replace("\n","");` Did you try to replace "\n" with "" <-empty ? – mat Dec 16 '11 at 11:30
  • I tried that too. mikel is helping out as well. Another friend of mine is looking into it. It should really work now but it still doesn't. :/ The line breaks are handle properly as far as I can see in the code. – AniCator Dec 16 '11 at 11:39