HTML textarea collapses new lines

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rallen102
Forum Newbie
Posts: 5
Joined: Thu Apr 11, 2013 12:29 pm

HTML textarea collapses new lines

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: HTML textarea collapses new lines

Post 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);
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
rallen102
Forum Newbie
Posts: 5
Joined: Thu Apr 11, 2013 12:29 pm

Re: HTML textarea collapses new lines

Post by rallen102 »

This created no change in the output.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: HTML textarea collapses new lines

Post by pickle »

What happens if you do no escaping but echo the content in <pre> tags?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: HTML textarea collapses new lines

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: HTML textarea collapses new lines

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
rallen102
Forum Newbie
Posts: 5
Joined: Thu Apr 11, 2013 12:29 pm

Re: HTML textarea collapses new lines

Post 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
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: HTML textarea collapses new lines

Post 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.
rallen102
Forum Newbie
Posts: 5
Joined: Thu Apr 11, 2013 12:29 pm

Re: HTML textarea collapses new lines

Post 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.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: HTML textarea collapses new lines

Post 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); 
rallen102
Forum Newbie
Posts: 5
Joined: Thu Apr 11, 2013 12:29 pm

Re: HTML textarea collapses new lines

Post 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.
Post Reply