Blog

Newest Post
Blog Archives
Blog Categories

Portfolio

3D Models
Banners
Brochures
Business Cards
Custom Programming
Environments
Flash Projects
Game Design
Image Altering
Web Design

About

Resume
Recommendation Letters

Contact

August 5, 2008

Javascript broke with multiple line variables from PHP

Filed under: JavaScript,PHP,The Coding World — admin @ 7:26 am

I have several sites where I use a pop up table to display information if the user rolls over and image. Example, if the user wants to view notes on a certain client, they can roll over a little “note” image to view the notes about the client pulled from a SQL database. The code I use for a message may be:

View Code JAVASCRIPT
note[0] = new Array("","<?php echo $message; ?>;");

That would work if $message was all one line. But as with all notes fields, you can never determine when the person entering data hits “Enter”. If the person did hit enter, your javascript would be broke and it would look like this in the source code.

View Code JAVASCRIPT
note[0] = new Array("","This is the start of the note field. I hit enter after this sentence twice.
 
Then I started typeing again");

This broke the Javascript. To fix this you can just swap the hidden special characters with a html line break.

Working code:

View Code JAVASCRIPT
note[0] = new Array("","<?php echo str_replace("\r\n","<br>;", $message); ?>;");

This would return:

View Code JAVASCRIPT
note[0] = new Array("","This is the start of the note field. I hit enter after this sentence twice<br><br>;Then I started typeing again");

Leave a Reply

© - Digital Epiphany Designs, Online Portfolio for Web Design, Custom Programming, Grapic Design and Game Design.