...

View Full Version : <br/> to newline trouble



mbarandao
12-03-2010, 02:37 PM
Good day all:

I've been experiencing a great difficulty resolving a problem concerning replacing

<br/> to an actual newline inside a textarea box. Much of the problem has been because the data in the textarea is populated via a php script in this manner:


echo "formObj.servicedesc.value = '".toSafeString($inf["servicedesc"])."';\n";


Inside my textarea box I get the following:


<textarea><br>On 12/01/10 @ 09:51<br>We performed the following:<br>
information update<br/><br/>----End of Service Report----<br/><br/>On 12/01/10 @ 09:43<br/>We performed the following:<br/>
information update<br/><br/>----End of Service Report----<br/><br/>On 11/29/10 @ 11:55<br/>We performed the following:<br/>
Information update<br/><br/>----End of Service Report----<br/><br/>On 11/29/10 @ 04:41<br/>We performed the following:<br/>
information update<br/><br/>----End of Service Report----<br/><br/>On 11/25/10 @ 09:34<br/>We performed the following:<br/>
Information updateds<br/><br/>----End of Service Report----<br/><br/>On 11/25/10 @ 08:56<br/>We performed the following:<br/>
We are performing an oil change<br/>Changing new brakes<br/>New tires<br/><br/>----End of Service Report----<br/>On 11/24/10 @ 09:43<br/>We performed the following:<br/>
3Click here to add details of todays service<br/><br/>----End of Service Report----<br/><br/>On 11/24/10 @ 09:15<br/>We performed the following:<br/>
<br/>On 11/22/10 @ 02:24<br/>We performed the following:<br/>
Click here to add details of todays serviced<br/><br/>----End of Service Report----<br/></textarea>";


Whereas, my ideal outcome is:


On 12/01/10 @ 09:51
We performed the following:

information update

----End of Service Report----

On 12/01/10 @ 09:43
We performed the following:

information update

----End of Service Report----

On 11/29/10 @ 11:55
We performed the following:

Information update

----End of Service Report----


My textarea box is written in html as follows:

<textarea name="servicedesc" id="servicedesc" cols=60 rows=12 disabled="true" style="position: absolute; right: 13; top: 260">

I have used numerous preg_replace functions but unable to resolve. I'm think I may be placing it incorrectly.

Any assistance is greatly appreciated!
Mossa

DataTalk
12-03-2010, 02:46 PM
function replacecarriagereturn(textarea,replaceWith)
{
textarea.value = escape(textarea.value);
//encode all characters in text area
//to find carriage return character
for(i=0; i < textarea.value.length; i++)
{
//loop through string, replacing carriage return
//encoding with HTML break tag
if(textarea.value.indexOf("%0D%0A") > -1)
{
//Windows encodes returns as \r\n hex
textarea.value=textarea.value.replace("%0D%0A",replaceWith);
}
else if(textarea.value.indexOf("%0A") > -1)
{
//Unix encodes returns as \n hex
textarea.value=textarea.value.replace("%0A",replaceWith);
}
else if(textarea.value.indexOf("%0D") > -1)
{
//Macintosh encodes returns as \r hex
textarea.value=textarea.value.replace("%0D",replaceWith);
}
}
textarea.value=unescape(textarea.value);
//decode all characters in text area back
}

mbarandao
12-03-2010, 02:54 PM
Thanks for the expeditious response, Data Talk. As I am quite new to this, could you please tell me where should I place this function?

I'm assuming that it may go inside the php script that is retrieving the data from mysql and displaying in the html if such is the case here is my php script:



<?php
$connection = mysql_connect("localhost:xxxx","xxxxx","xxxxxx");
mysql_select_db("shop",$connection);

function toSafeString($string) {
$string = preg_replace('/[\r\n]/s', '\\n', $string);
//$string = preg_replace('/<br \/>/', '\n', $string);//I have tried this with no success
return $string;
}

if(isset($_GET['getClientId'])){
$clientId = preg_replace("/[^0-9|a-z|A-Z]/si", "", $_GET['getClientId']);
$res = mysql_query("select * from ajax_client where clientID='".$_GET['getClientId']."'") or die(mysql_error());
//$res = mysql_query("UPDATE ajax_client SET drivenmileage=currentmileage-previousmileage");// this is the correct string for mileage substraction however it clashed with client lookup
if($inf = mysql_fetch_array($res)){
echo "formObj.firstname.value = '".toSafeString($inf["firstname"])."';\n";
echo "formObj.lastname.value = '".toSafeString($inf["lastname"])."';\n";
echo "formObj.address.value = '".toSafeString($inf["address"])."';\n";
echo "formObj.zipcode.value = '".toSafeString($inf["zipcode"])."';\n";
echo "formObj.city.value = '".toSafeString($inf["city"])."';\n";
echo "formObj.state.value = '".toSafeString($inf["state"])."';\n";
echo "formObj.dhtmlgoodies_category.value = '".toSafeString($inf["dhtmlgoodies_category"])."';\n";
echo "formObj.dhtmlgoodies_subcategory.value = '".toSafeString($inf["dhtmlgoodies_subcategory"])."';\n";
echo "formObj.caryear.value = '".toSafeString($inf["caryear"])."';\n";
echo "formObj.servicedesc.value = '".toSafeString($inf["servicedesc"])."';\n";
echo "formObj.theDate.value = '".toSafeString($inf["theDate"])."';\n";
echo "formObj.areacode.value = '".toSafeString($inf["areacode"])."';\n";
echo "formObj.phoneprefix.value = '".toSafeString($inf["phoneprefix"])."';\n";
echo "formObj.phonesufix.value = '".toSafeString($inf["phonesufix"])."';\n";
echo "formObj.email.value = '".toSafeString($inf["email"])."';\n";
echo "formObj.cellareacode.value = '".toSafeString($inf["cellareacode"])."';\n";
echo "formObj.cellprefix.value = '".toSafeString($inf["cellprefix"])."';\n";
echo "formObj.cellsufix.value = '".toSafeString($inf["cellsufix"])."';\n";
echo "formObj.previousmileage.value = '".toSafeString($inf["previousmileage"])."';\n";
//echo "formObj.currentmileage.value = '".toSafeString($inf["currentmileage"])."';\n";
echo "formObj.servicearea.value = '".toSafeString($inf["servicearea"])."';\n";
echo "formObj.commentonclient.value = '".toSafeString($inf["commentonclient"])."';\n";
echo "formObj.drivenmileage.value = '".toSafeString($inf["drivenmileage"])."';\n";

}
else
{
echo "formObj.firstname.value = '';\n";
echo "formObj.lastname.value = '';\n";
echo "formObj.address.value = '';\n";
echo "formObj.zipcode.value = '';\n";
echo "formObj.city.value = '';\n";
echo "formObj.state.value = '';\n";
echo "formObj.dhtmlgoodies_category.value = '';\n";
echo "formObj.dhtmlgoodies_subcategory.value = '';\n";
echo "formObj.caryear.value = '';\n";
echo "formObj.servicedesc.value = '';\n";
echo "formObj.theDate.value = '';\n";
echo "formObj.areacode.value = '';\n";
echo "formObj.phoneprefix.value = '';\n";
echo "formObj.phonesufix.value = '';\n";
echo "formObj.email.value = '';\n";
echo "formObj.cellareacode.value = '';\n";
echo "formObj.cellprefix.value = '';\n";
echo "formObj.cellsufix.value = '';\n";
echo "formObj.previousmileage.value = '';\n";
//echo "formObj.currentmileage.value = '';\n";
echo "formObj.servicearea.value = '';\n";
echo "formObj.commentonclient.value = '';\n";
echo "formObj.drivenmileage.value = '';\n";

}

}

?>

How should I implement your suggestion inside this file?

DataTalk
12-03-2010, 03:11 PM
did you try adding "<br> in front of your code?



echo "<br>formObj.firstname.value = '".toSafeString($inf["firstname"])."';\n";

DataTalk
12-03-2010, 03:16 PM
Another way would be



<table border="0" width="100%" cellspacing="1" style="border-collapse: collapse">
<tr>
<?php
echo "<td>output code in here</td>"
echo "<td>output code in here</td>"
?>
</tr>
</table>

mbarandao
12-03-2010, 03:17 PM
Yes! this too causes the script to become unresponsive. (writing in response to
echo "<br>formObj.firstname.value = '".toSafeString($inf["firstname"])."';\n"; suggestion

mbarandao
12-03-2010, 03:24 PM
<table border="0" width="100%" cellspacing="1" style="border-collapse: collapse">
<tr>
<?php
echo "<td>output code in here</td>"
echo "<td>output code in here</td>"
?>
</tr>
</table>

For this, I need a bit of clarification!

DataTalk
12-03-2010, 03:41 PM
just output your code in html, should work fine



<table border="0" width="100%" cellspacing="1" style="border-collapse: collapse">
<tr>
<!-- put this above your php code -->
<?php
// copy your code in here



// echo your output code in the <td> tags
echo "<td>output code in here</td>"
echo "<td>output code in here</td>"

?>
<!-- put this below your php code -->
</tr>
</table>

mbarandao
12-03-2010, 04:02 PM
I'm not sure that this would be ideal for my task. Employing this strategy, I believe would greatly impact the structure of the page that is displaying the data.

I may have an incorrect understanding of your implementation suggestion, but the following is my html that receives the data via an ajax script



<body onLoad="initFormEvents()"; "document.onmousemove= document.onkeypress= function()" style="background-color: #C0C0C0" topmargin="6" marginheight="6">
<div style="position: absolute; width: 1116px; height: 100px; z-index: 2; left: 11px; top: 27px; background-color: #00FFFF" id="layer2">
<img border="0" src="transmission-repair.png" width="1117" height="100"></div>
<br>
<p>&nbsp;</p>
<form name="clientForm" action="ajax-client_lookup.html" method="post">
<fieldset style="width: 100%; height: 470">
<legend align="left"><b><font size="4" color="#FF0000">Client information</font></b></legend>
<div style="position: absolute; width: 138px; height: 149px; z-index: 3; left: 443px; top: 230px; background-color: #000000; bottom:-7" id="layer3">
<DIV style="position: absolute; left: 4px; top: 7px" name="image" id="image"></DIV>


&nbsp;</div>
<table width="1142">
<tr>
<td><label for="clientID">License Plate Number:</label></td>
<td>
<input name="clientID" id="clientID" size="17" maxlength="16" style="background-color: #00FFFF" value="Enter Car Plate#"> &nbsp;&nbsp;
<a class="button" href='#'><span>CLICK TO SEARCH</span></a>
</b></td>
<td>
<p align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</p>
<div style="position: absolute; width: 519px; height: 455px; z-index: 1; left: 621px; top: 192px; background-color:#FFFFFF" id="layer1">

<table border="0" width="100%" height="372">
<tr>
<td height="26">
<p align="center"><font size="4">Vehicle Details<hr color="#C0C0C0" size="1">
</font></td>
</tr>
<tr>
<td VALIGN="top" width="1%" style="border-left: 2px dotted" color="#CCCCCC;">
<p align="right"><label for="theDate">Customer Since:</label>
<input type="text" name="theDate" size="20" style="padding: 0" disabled="true"></p>
<p align="left">
<label for="dhtmlgoodies_category">Make:</label>
<input type="text" name="dhtmlgoodies_category" id="dhtmlgoodies_category" size="14" style="padding: 0" disabled="true">
<label for="dhtmlgoodies_subcategory">Model:</label>
<input type="text" name="dhtmlgoodies_subcategory" size="15" style="padding: 0" disabled="true">
<label for="caryear">Year:</label>
<input type="text" name="caryear" size="5" style="padding: 0" disabled="true"></p>
<label for="previousmileage">Last Recorded Mileage:</label>
<input type="text" name="previousmileage" id="previousmileage" size="20" disabled="true"><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label for="drivenmileage">Driven Miles
between the last two visits:</label>
<input type="text" name="drivenmileage" id="drivenmileage" size="5" disabled="true">
<br>
<label for="servicearea">We Last Worked on your vehicle in this area:</label>
<input type="text" name="servicearea" size="20" style="padding: 0" disabled="true"><br>
<br>
<label for="servicedesc">Service History:</label>
&nbsp;
<!--<div id="servicedesc" name="servicedesc"></div> -->
<textarea name="servicedesc" id="servicedesc" cols=60 rows=12 disabled="true" style="position: absolute; right: 13; top: 260"></textarea></td>
</tr>
</table>

</div>
</td>
</tr>
<tr>
<td><label for="firstname">First name:</label></td>
<td>
<input name="firstname" id="firstname" size="20" maxlength="255" disabled="true"></td>
<td>
&nbsp;</td>
</tr>
<tr>
<td><label for="lastname">Last name:</label></td>
<td>
<input name="lastname" id="lastname" size="20" maxlength="255" disabled="true"></td>
<td>
&nbsp;</td>
</tr>
<tr>
<td><label for="address">Address:</label></td>
<td>
<input name="address" id="address" size="20" maxlength="255" disabled="true"></td>
<td>
&nbsp;</td>
</tr>
<tr>
<td><label for="zipcode">Zip code:</label></td>
<td><input name="zipcode" id="zipcode" size="8" maxlength="5" disabled="true"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><label for="city">City:</label></td>
<td><input name="city" id="city" size="20" maxlength="255" disabled="true"></td>
<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
</tr>
<tr>
<td><label for="state">State:</label></td>
<td>
<input name="state" id="state" size="20" maxlength="255" disabled="true"></td>
<td>
&nbsp;</td>
</tr>
<tr>
<td><label for="tel_number">Phone #:</label></td>
<td>
<input name="areacode" id="areacode" size="3" maxlength="3" disabled="true">-<input name="phoneprefix" id="phoneprefix" size="3" maxlength="3" disabled="true">-<input name="phonesufix" id="phonesufix" size="4" maxlength="4" disabled="true"></td>
</tr>

<tr>
<td><label for="email">Email:</label></td>
<td>
<input name="email" id="email" size="42" maxlength="25" disabled="true"></td>
<td>
&nbsp;</td>
</tr>
<tr>
<td><label for="cell_number">Alt. Phone#:</label></td>
<td>
<input name="cellareacode" id="cellareacode" size="3" maxlength="3" disabled="true">-<input name="cellprefix" id="cellprefix" size="3" maxlength="3" disabled="true">-<input name="cellsufix" id="cellsufix" size="4" maxlength="4" disabled="true"></td>
<td>&nbsp;</td>
</tr>
<tr>
<td><label for="commentonclient">Special Comment on Client:</label></td>
<td>
<p align="left">
<textarea name="commentonclient" id="commentonclient" cols=36 rows=6 disabled="true"></textarea>
</tr>
<td>
&nbsp;</tr>
</table>
</form>
<div id="instruct"><h5>INSTRUCTIONS:</h5>TYPE LICENSE PLATE NUMBER AND HIT TAB KEY <p>NOTE: NO CHANGES ARE MADE ON THIS SCREEN. USE EDIT CLIENT FOR SUCH TASK!</p></div>
&nbsp;&nbsp;</fieldset>
<td>&nbsp;</td>
<td>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<div id="menuoption"><a href="home.php"> RETURN TO MAIN</a>
--|-- <a href="addclient.php">ADD NEW CLIENT </a>&nbsp;--|--
<a href="addentry.php">EDIT ClIENT</a>&nbsp;&nbsp;&nbsp;
<br></div>
<div id="logos"><p align="right">&nbsp;<i><font size="2" color="#FFFFFF"><img border="0" src="images/systempowered3.png" ></font></p></div>
</body></html></table>


this is the ajax script that calls the php


var ajax = new sack();
var currentClientID=false;
function getClientData()
{
var clientId = document.getElementById('clientID').value.replace(/[^0-9|a-z|A-Z]/g,'');//here to incorporate the client id with numbers and letters
if(clientId.length >0 && clientId!=currentClientID){//here to change the client id lenght
currentClientID = clientId
ajax.requestFile = 'getClient.php?getClientId='+clientId; // Specifying which file to get
ajax.onCompletion = showClientData; // Specify function that will be executed after file has been found
ajax.runAJAX(); // Execute AJAX function
}

}

function showClientData()
{
var formObj = document.forms['clientForm'];
eval(ajax.response);
//ajax.getElementByID().response;
}


function initFormEvents()
{
document.getElementById('clientID').onblur = getClientData;
document.getElementById('clientID').focus();
}
//window.onload = initFormEvents;
</script>


Note: I mention ajax with no implied request for support in such area; as my concern, I believe is with php.

Mossa-

DataTalk
12-03-2010, 04:14 PM
Have you considered your echo "output" in an iframe

mbarandao
12-03-2010, 04:30 PM
Thanks for the continued suggestions. I'm will attempt a solution that address my problem.

How would this work based on the articulated concern?
ie: if this is my iframe,



<iframe name="servicedesc" frameborder=0 scrolling=no width=100% src="" onload="adjustMyFrameSize();" height="300" align="left"></iframe>


what would be the value of src=""?

DataTalk
12-03-2010, 06:31 PM
I'm not sure how you are requesting the data here?

if you are posting the data you would target that frame

<form method="post" action="yourIframename.php" target="NameOfIframe">

DataTalk
12-03-2010, 07:34 PM
Sorry! I have been trying to piece together what you are trying to do..

After viewing your form code, I use a different method to load form data and have not seen data loaded that way.

FYI:
the method i use is


if($_GET['id']) {
// select database
$result = mysql_query("SELECT * FROM businesses WHERE id=".$_GET['id']);
$row = mysql_fetch_object($result) ;?>
<input type="hidden" name="id" value="<?php print $_GET['id'] ?>" >
<?php } ?>

<input name="Bus_Name" type="text" value="<?php print $row->Bus_Name ?>">

This method will also load all the related data,
Sorry i cant help you on this

Lamped
12-03-2010, 08:33 PM
function toSafeString($string) {
$string = preg_replace('/[\r\n]/s', "\n", $string);
$string = preg_replace('/<br\s?\/?>/i', "\n", $string);
return $string;
}

mbarandao
12-03-2010, 08:55 PM
Data Talk, thank you for your attempts!

Lamped, I have tried that function with no success!

Lamped
12-03-2010, 08:56 PM
"No success"... Well, did anything change? :p

JohnDubya
12-03-2010, 10:07 PM
mbarandao, I'm assuming that the actual <br/> tags are stored in your database, correct?

If that is the case, you can simply do something like this in your toSafeString() function:



function toSafeString($string) {
$string = str_replace('<br/>', "\n", $string);
$string = preg_replace('/[\r\n]/s', '\\n', $string);
return $string;
}


That will replace all <br/> tags with a new line character, which is what will create a new line in a textarea. Notice the double quotes around the \n. Those are essential.

And an FYI: to be valid, there needs to be a space between the br tag and the forward slash (e.g. "<br />").

Lamped
12-03-2010, 10:10 PM
mbarandao, I'm assuming that the actual <br/> tags are stored in your database, correct?

He seems to have a mix of <br> and <br />, hence the preg approach above.

firepages
12-04-2010, 12:47 AM
... what does your current toSafeString() function do ... ? perhaps it replaces < & > with &gt; etc ? in which case there is nothing to replace in the suggestions given above ??

+ I would adjust to
$string = str_replace(array('<br />','<br/>'),"\n",$string); which will do good and bad tags without the overhead of regex

mbarandao
12-04-2010, 01:47 AM
Great news! With JohnDubya suggestion of


function toSafeString($string) {
$string = str_replace('<br/>', "\n", $string);
$string = preg_replace('/[\r\n]/s', '\\n', $string);
return $string;
}

the issue has been resolved! it was in particular the first $string.

To all of the great experts on this forum, I'm most appreciative for your selfless time.

The Best,
Mossa



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum