开发者

php data send to javascript problem

php echo's <br /> to empha开发者_如何学Gozise the javascript view to put a newline but when it comes to the javascript size it comes as &lt. br / &gt. and it is just displayed as it is. How can i send a <br /> as it is to the javascript size ?

the string that php sends

hello <br /> world

but javascript receives it as

hello &lt;br /&gt; world

How can i make javascript to understand a new line ?

echo "hello <br /> world";

sends the data to javascript

even

echo "hello \n world";

doesnt work

the javascript side

var txt = ajaxRequest.responseText;
$("#output tr td div").text(txt);


From your updated answer, change your code from:

var txt = ajaxRequest.responseText;
$("#output tr td div").text(txt);

to (note the .html):

var txt = ajaxRequest.responseText;
$("#output tr td div").html(txt);

and it'll work.


Ok so from your previous answers it looks like your doing an ajax req? It should look like this (if you're using PHP, JavaScript and JQuery):

PHP side (some_file.php):

<?php
$out = "hello\nworld";

echo json_encode($out);
exit;
?>

Javascript:

<script type="text/javascript">
function do_ajax() {
    $.get('some_file.php', {}, function (res) {
        // Res will be "hello\nworld";
        alert(res);
    }, 'json');
}
</script>


You want to make new line in JavaScript string, right? So PHP code:

echo "var asd = 'hello\\nworld';";

or

echo 'var asd = \'hello\nworld\';';

will result this:

var asd = 'hello\nworld';

You can now show effects with alert:

alert(asd);

which will show:

hello
world

That is new line in JavaScript.


Should be as simple as using decodeURI(var).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜