简体   繁体   中英

Calling PHP function nl2br in html doc

I am working on a site in which I have a user entry field that I would like to insert line breaks after every new line in the user input without making an entirely new php page. Here's my code (js):

var newstreamSummary = document.getElementById("addNewstreamSummaryBox").value;
        if(newstreamSummary !== ""){
            document.getElementById("previewNewstreamSummary").innerHTML = newstreamSummary;
            /* newstreamSummary = <?php echo nl2br(newstreamSummary);?> */
        }

That block lives in a js function called showPreview(), when running it says the showPreview() is not defined. It also does not work when I put the php call in a separate <script> tag. I would like to call this function without having a new page as this is the only time in the project where I'm using php currently. Or if there's a js function that does the same thing. Thanks all for any advice.

Php is server side. The only way of using php in that case would be making an Ajax call to a php script which processes your test and returns this text processed.

There is no need for that in your case. Just add this line after your if statement:

newStringSummay = newStringSummay.replace(/\n/g, "<br>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM