0

This is similar to PHP generated content inside Javascript breaking script, however I'm not able to understand where I'm wrong.

Here is the brief. I'm trying to output 10 strings from MySQL DB into a javascript array s, (s[1] to s[10]) (stored in one MySQL table under columns pt1 to pt1 - info so code is understandable to all). (Note $apos is just the apostrophe string - not a concern here).

<?php 
echo "<script>\n";
echo "var s = [];\n";
 for($i=1;$i<=10;$i++) {
 echo "s[".$i."] = ".$apos.$r['pt'.$i].$apos.";\n"; 
 } 
echo "</script>";
 ?>

It produces the right code when I look at the source by 'View Source' in browser:

<script>
var s = [];
s[1] = 'a';
s[2] = 'b';
.
.
s[10] = 'j';
</script>

However, this doesn't work as a script (meaning if I check in Google developer tools, and I click on content inside the 'script' tag elements, it is told to be 'text', and not 'script' in the bar below).

However if I remove the PHP, and manually write the whole script the same way, it works just fine. I have tried removing the \n linebreaks in the PHP code, but still the same problem.

Something in PHP is breaking the script. Can you help?

10
  • What do you mean by 'doesn't work as a script'? Commented Sep 4, 2014 at 9:08
  • 4
    My personal preference is var s=<?php echo json_encode($info);?>; Commented Sep 4, 2014 at 9:09
  • @jeff I've clarified that point in the question now. Let me know if its still unclear. Commented Sep 4, 2014 at 9:15
  • When you say "look at the source file" do you mean using "view source" at the browser? Commented Sep 4, 2014 at 9:19
  • @Bob Yes. The problem is very similar in spirit to the earlier problem link which I've posted in the start of the question. Commented Sep 4, 2014 at 9:22

2 Answers 2

0

My best guess is that one (or more) of your strings contains a character (such as the apostrophe) which breaks the PHP output. Still the best solution was already prosposed by Passery in your comments section:

echo '<script>';
echo 'var s = ' . json_encode($info);
echo '</script>';
Sign up to request clarification or add additional context in comments.

1 Comment

I agree with Passerby's answer, and it works. But whether the apostrophe is breaking it or not I'm not certain, as the 'View source' files for the working and non-working codes are exactly the same!
0

I think you may have been misreading the developer tools. When I look closely at Chrome, it does indeed identify your script as text, possibly because the default type is text/javascript. Putting an explicit type declaration on the <script> tag made no change.

However, when I add echo "alert('s[1]='+s[1]);"; after the for loop, the alert displays, and I am able to see the values of the other s[i] with the debugger.

In other words, I think your generated JavaScript worked all along.

I know you've already fixed the problem another way. I post this answer only in case it may help others.

Here is what I used for testing:

<?php 
$a = "-abcdefghij";
$apos="'";
echo "<script type=\"text/javascript\">";
echo "var s = [];";
for($i=1;$i<=10;$i++) {
 echo "s[".$i."] = ".$apos.$a[$i].$apos.";\n"; 
} 
echo "alert('s[1]='+s[1]);";
echo "</script>";
?>

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.