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

4 Answers 4

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

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.

10
  • 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("&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;"); Commented Dec 16, 2011 at 10:35
  • Copying your alert to 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. Commented Dec 16, 2011 at 10:42
  • Well this is what it currently outputs in the JS section jsfiddle.net/xVCqH Commented Dec 16, 2011 at 10:48
  • You can also checkout the page here weer.patrickwobben.nl/admin/pagina.php of course it's the outputted HTML code so. Commented Dec 16, 2011 at 10:52
  • You've still got line breaks in your JS code. jsfiddle.net/2wPHv is an example of what you've got vs what you need to change it to. Commented Dec 16, 2011 at 10:53
1

Do you want to use json_encode?

5
  • "I'm trying to dump a PHP array to a JavaScript one. (without using any extra extensions)" - first line of my post Commented Dec 20, 2011 at 10:39
  • Right, that's what that does. That is not an extra extension; is built in. Commented Dec 20, 2011 at 17:34
  • @AniCator forgot to tag. Also, it is best not to reinvent the wheel. Commented Dec 20, 2011 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. Commented Jan 15, 2012 at 12:05
  • @AniCator this is not jQuery, and this is not an addon. standard JavaScript. use the language. Commented Jan 15, 2012 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\';
3
  • I tried to use nl2br (also tried the same using str_replace("\n","<br/>",htmlentities($lijst["tekst"][$index], ENT_QUOTES)) ) but for some reason the code still breaks. Commented Dec 16, 2011 at 10:20
  • str_replace("\n",""); Did you try to replace "\n" with "" <-empty ? Commented Dec 16, 2011 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. Commented Dec 16, 2011 at 11:39

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.