Page 1 of 1

HTML textarea collapses new lines

Posted: Thu Apr 11, 2013 12:37 pm
by rallen102
I have read the other posts that describe this problem. The problem still persists for me. I have a textarea that the users may enter whatever they wish. They expect to see the same format when they print the document. When they enter:
Now is the time…
for all good
men…
The result is:
Now is the time…for all good men…
I am using PHP 5.1.6 and Apache 2.2.3. I have tried Firefox 20.0 and IE 10 for Windows 7. The end users will be using IE 7 to IE 10 with only a few using Firefox and Safari.
Here is the creation of the textarea:
<textarea class="textarea" rows="4" cols="90" name="lease" id="lease " onBlur="ajaxLease()">
<?php echo nl2br($stuff['lease']); ?>
</textarea>
ajaxLease() is a javascript function that gathers information and passes it to a PHP script:

Code: Select all

$month = $_REQUEST['month'];  $year = $_REQUEST['year'];  $lease = $_REQUEST['lease'];
// $lease = str_replace(array('\r', '\r\n', '\n'),'<br />',$lease);
// $lease = stripslashes($_REQUEST['lease']);
// $lease = mysql_real_escape_string($_REQUEST['lease']);
$query = "UPDATE activities SET lease='".$lease."' WHERE  month='".$month."' AND year='".$year."';";
$qry_result = mysql_query($query) or die(mysql_error().$query);
Can anyone tell me what I am doing wrong?

Re: HTML textarea collapses new lines

Posted: Fri Apr 12, 2013 2:39 pm
by social_experiment
I've seen that single quotes around \r\n is not the same as double quotes around \r\n. Try the line below

Code: Select all

<?php
$lease = str_replace(array("\r", "\r\n", "\n"),'<br />',$lease);
?>

Re: HTML textarea collapses new lines

Posted: Fri Apr 12, 2013 5:08 pm
by rallen102
This created no change in the output.

Re: HTML textarea collapses new lines

Posted: Fri Apr 12, 2013 5:27 pm
by pickle
What happens if you do no escaping but echo the content in <pre> tags?

Re: HTML textarea collapses new lines

Posted: Fri Apr 12, 2013 5:44 pm
by Jade
Are you running any kind of cleaning on your $_REQUEST content that could be adding backslashes to the text? If that's the case then your \r\n that you're trying to replace would actually be \\r\\n because you're trying to escape them.

Re: HTML textarea collapses new lines

Posted: Fri Apr 12, 2013 5:51 pm
by social_experiment
Jade wrote:Are you running any kind of cleaning on your $_REQUEST content
good point ^

did another check on this line

Code: Select all

<?php $lease = str_replace(array("\r", "\r\n", "\n"), '<br />', $lease); ?>
if you use single quotes around the new line values (\r, \r\n, \n) they won't be converted to line breaks. they need to be wrapped in double quotes.

Re: HTML textarea collapses new lines

Posted: Tue Apr 16, 2013 12:02 pm
by rallen102
I have tried each of these suggestions and a few more. It seems that the formatting of the input is being stripped in the Javascript document.getElementById('lease').value. I have tried document.getElementById('lease').innerHTML but I really don't know how to use this,
just for clarification here is the Javascript function:
function ajaxLease(){
var ajaxRequest;
try{ ajaxRequest = new XMLHttpRequest();
} catch (e){
try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ alert("Your browser broke!"); return false; } } }
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){ var ajaxDisplay = document.getElementById('lease'); ajaxDisplay.innerHTML = ajaxRequest.responseText; } }
var property = document.getElementById('property').value;
var month = document.getElementById('month').value;
var year = document.getElementById('year').value;
var lease = document.getElementById('lease').innerHTML;
var queryString = "?property=" + property + "&month=" + month + "&year=" + year + "&lease=" + lease;
ajaxRequest.open("POST", "./include/lease.php" + queryString, true);
ajaxRequest.send(null);
}

Javascript is not my forte. Can you help me with this?
Thanks

Re: HTML textarea collapses new lines

Posted: Tue Apr 16, 2013 3:20 pm
by Jade
what code is in your include/lease.php? My guess is that it's not wrapping the results that it's passing to your ajaxDisplay.innerHTML in nl2br which is why you're loosing the returns when the ajax response comes back.

Re: HTML textarea collapses new lines

Posted: Wed Apr 17, 2013 1:40 pm
by rallen102
The include/lease.php is the code in the original post. You man notice that I have tried several methods and have commented them out when they did not work.

Re: HTML textarea collapses new lines

Posted: Wed Apr 17, 2013 2:49 pm
by Jade
In your javascript try this:

Code: Select all

function ajaxLease(){
var ajaxRequest;
try{ ajaxRequest = new XMLHttpRequest();
} catch (e){
try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ alert("Your browser broke!"); return false; } } }
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){ 
    var ajaxDisplay = document.getElementById('lease');
    ajaxDisplay.innerHTML = ajaxRequest.responseText.replace(/\r\n/g, "<br />").replace(/\n/g, "<br />"); 
}
 }
var property = document.getElementById('property').value;
var month = document.getElementById('month').value;
var year = document.getElementById('year').value;
var lease = document.getElementById('lease').innerHTML;
var queryString = "?property=" + property + "&month=" + month + "&year=" + year + "&lease=" + lease;
ajaxRequest.open("POST", "./include/lease.php" + queryString, true);
ajaxRequest.send(null); 

Re: HTML textarea collapses new lines

Posted: Mon Apr 22, 2013 12:47 pm
by rallen102
This is not working. Maybe I should just forget the Ajax and just use PHP. This would require refreshing the page after each edit but at least that would work.