Welcome, Guest. Please login or register.
Did you miss your activation email?
02/08/12, 09:48
Home Help Search Login Register
News: Parsley Flex framework review featuring quiz application, in our Flex frameworks series
Flex SDK 4.5 mobile roadmap: begin with your mobile development
Swiz Flex framework review featuring quiz application
New homepage we release our new Homepage, take a look ...

+  Flash-db
|-+  Server side Scripting and Database Support
| |-+  PHP, Perl, ASP, JSP, CFM (Moderators: Flash-db, Musicman, vesa kortelainen, Ronald Wernecke, Jorge Solis, nothingGrinder)
| | |-+  Problems with line breaks :-\
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Print
Author Topic: Problems with line breaks :-\  (Read 1520 times)
D-Shap
Jr. Programmer
**
Posts: 52



View Profile WWW Email
« on: 02/17/04, 19:58 »

Hey Everyone,

I am working on a PHP script that retreives values from a db and then writes them to a page in a JavaScript array so that they can be quickly accessed by some other Jscript functions on the page.  The db values are values that have all been submitted by a form on another page by a user.  The problem is this:  When a user creates a line break in the text area (presses enter), it adds the value to the javascript array, but it actually breaks the line in the Jscript code.  This causes a problem because then it is an invalid way to set an element of an Array in Jscript.  I've attempted many times to use string replacing functions in PHP to solve the problem, but it always seems to make that break.  Any ideas?

Thanks so much!

- Dan
Logged
Jorge Solis
Administrator
Systems Administrator
*****
Posts: 14593


View Profile
« Reply #1 on: 02/18/04, 03:46 »

Can you post what did you try?

Jorge
Logged

D-Shap
Jr. Programmer
**
Posts: 52



View Profile WWW Email
« Reply #2 on: 02/18/04, 16:50 »

Hi Jorge,

I've got a loop through the database rows that dynamically creates the elements in the JS array.

Code:

<?php<br><br>print "<br><script language=\"Javascript\"><br>var infoArray;<br>infoArray = new Array();";<br><br>$getData mysql_query("SELECT * FROM Information"$link);<br>while ($entry2 mysql_fetch_array($getData))<br>      {<br>      $TheInfo $entry2[Info];<br>      $TheInfo str_replace("\n""<br>"$TheInfo);<br>      $TheInfo addslashes($TheInfo);<br>      print "\n\tinfoArray[".$entry2[Day]."] = \"".$TheInfo."\";";<br>      }<br><br>print "</script>";<br>?>


OK this works perfectly without line breaks in the db values.  The addslashes() is a built-in function to automatically escape quotation marks and back-slashes.

Here is what the one element in the array looks like when a db value has a line break in it though:

Code:

<script language="JavaScript">
var infoArray;
infoArray = new Array();
infoArray[0] = "hi this value doesn't have line breaks and works fine";
infoArray[1] = "this one has a line break and
<br>the line has been broken";
</script>


It breaks the line in the JS code for setting the array element and that is invalid and won't work.

Do you have any ideas on how to get around this?

Thanks SO much,

- Dan
Logged
D-Shap
Jr. Programmer
**
Posts: 52



View Profile WWW Email
« Reply #3 on: 02/19/04, 01:06 »

AHHHHHH,

Problem solved  Grin

Just to let everyone who doesn't already know...

Line breaks for windows are done with \n AND \r.

So I used str_replace("\r", "", $TheInfo); after the one for "\n" and everything works fine.

Thanks for the help that you probably were generously going to give me, you always have been able to solve any problem  Smiley

- Dan
Logged
Pages: [1] Print 
« previous next »
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
anything