1

i have problem with between php and jQuery/Javascript...It wont show up or can't work.

My Code:

if($bottom_1_banner == "true"){
        //$data_AD .= '$(\'.ban_bottom\').html(\''.htmlentities($bottom_banner).'\').text();';
        $data_AD .= '$(\'.ban_bottom\').html("'.htmlspecialchars($bottom_banner).'").text();';
}

Error Log: (Chrome/Safari)

<script type="text/javascript">
    $(document).ready(function() {
        $('.ban_bottom').html("&lt;!-- xxxxxxx --&gt;
******index.php:11 Uncaught SyntaxError: Unexpected token ILLEGAL******
            &lt;script type=&quot;text/javascript&quot;&gt;
            xxxxxxx_bid = &quot;xxxxxxxxxxx&quot;;
            &lt;/script&gt;
            &lt;script type=&quot;text/javascript&quot; src=&quot;http://xxx.xxxxxx.com/k.js&quot;&gt;&lt;/script&gt;
            &lt;!-- xxxxxxxx --&gt;").text();    });
</script>

OR

Error Log

Edited: Converted Image to Text.

user453089
  • 719
  • 2
  • 13
  • 23

5 Answers5

1

I had the same problem with the new line in JavaScript. If you are getting the value from MySql you can try this:

$php_string = str_replace('<br />','<br />\\',nl2br($php_string));

First replace the line-break with an html <br/> tag and then add the \ after the generated <br/> tag; So the JS line always ends with a \, that indicates a correct new line in the JavaScript code.

Scott
  • 21,211
  • 8
  • 65
  • 72
ZeV
  • 11
  • 1
0

Based by your code above, one thing for sure, you need to define $data_AD variable before using .= or just use = without .

toopay
  • 1,635
  • 11
  • 18
  • nope... i have 4 scripts add to one variable =) but i test one script first.. 3 of them scripts later. – user453089 Jul 27 '11 at 12:10
  • Maybe you need to check this thread too : http://stackoverflow.com/questions/5733275/chrome-uncaught-syntax-error-unexpected-token-illegal – toopay Jul 27 '11 at 12:14
0

Maybe do

$data_AD .= '$(\'.ban_bottom\').html("'.$bottom_banner.'").text();';

...

<?php echo str_replace('\n', '\\n', $data_AD); ?>

to remove the line breaks?

marc
  • 6,103
  • 1
  • 28
  • 33
0

Get rid of the new lines in the HTML. Javascript doesn't understand strings on multiple lines. You can alternatively replace the \n with \ \n:

This does not work:

var a = "This is a
string for me";

This works

var a = "This is a \
string for me";

or

var a = "This is a string for me";
methodin
  • 6,717
  • 1
  • 25
  • 27
  • nope only problem with ... jQuery cant create script.. but i found similar link: http://stackoverflow.com/questions/610995/jquery-cant-append-script-element – user453089 Jul 29 '11 at 12:43
0

This line of code works flawless here. I'd guess its an file encoding issue with the .php-file. Check for bad characters.

worenga
  • 5,776
  • 2
  • 28
  • 50