Click to See Complete Forum and Search --> : preserve whitespace in a textarea


bustya
10-06-2008, 02:47 AM
I've been reading the manual and not finding this so I'm hoping one of you can point me to the function I'm looking for.

I'm trying to preserve whitespace in a textarea when a form errors. When a user submits the form with a required field blank he's returned to the form with his submitted values intact. The only problem here is if the user added linebreaks the value of the textarea would be like:



Hello.\r\n\My name is John.


So in the event of an error I'd like to return the user to the form with the text in the textarea formatted like:


Hello.
My name is John.

scragar
10-06-2008, 02:59 AM
I'm sure textarea's preserve the already existing new lines...

Anyway, the best solution for turning new line characters into <br>s it to plain use nl2br() (http://php.net/nl2br)

bustya
10-06-2008, 04:39 AM
I've checked my DB and the whitespace is in fact preserved there (if the form is submitted successfully), but for some reason when the user is sent back to the form to correct errors (i.e. "no subject"), the linebreaks in the textarea are displayed in regular expression, which is really bad because these are characters I'm preg_matching as illegal characters, so the user ends up having to manually clean these before resubmitting the form.

I'm echoing the value in the textarea like this (the $form->error just tells the user what's wrong with the attempted message):


<textarea name="message" id="message" cols="50" rows="10"><?echo $form->value("message"); ?></textarea>

<? echo $form->error("message"); ?>



And here's the function that processes the form:


function procCompose(){
global $session, $form;
/* compose attempt */
/* Convert SUBJECT to all lowercase */
$_POST['subject'] = strtolower($_POST['subject']);

$retval = $session->sessCompose($_POST['receiver'], $_POST['sender'], $_POST['subject'], $_POST['message']);
$fix = $_SERVER['QUERY_STRING'];
/* compose successful */
if($retval){
$_SESSION['compose'] = true;

header("Location: $session->referrer?$fix");

}
/* compose failed */
else{

$_SESSION['value_array'] = $_POST;
/* $_SESSION['value_array'] is being sent back to the form to be corrected */
$_SESSION['error_array'] = $form->getErrorArray();
header("Location: $session->referrer?$fix");
}
}


I have two more files associated with this but I believe the problem is somewhere in the function above.

Just for good measure, here's the error checking for the "message" field...


$field = "message"; //Message field
if(!$submessage || strlen($submessage = trim($submessage)) == 0){
$form->setError($field, "You need a message too");
}
else{
/* Check if valid message */
$submessage = stripslashes($submessage);
$submessage = str_replace("'","'",$submessage);
$submessage = str_replace('"','"',$submessage);
if(!preg_match('/^[-\w+!#$%&:;.()\<>?"\'@*\/=|,`{}~\n\\r\\s\[\]]+$/', $submessage)){
$form->setError($field, "Illegal characters.");
}

}



I tried removing trim from the above but it didn't help either.

bustya
10-07-2008, 11:51 AM
Hmm, halfway there...

This takes care of the r\ and n\


$_SESSION['value_array'] = str_replace("\r", '',$_SESSION['value_array']);
$_SESSION['value_array'] = str_replace("\n", '',$_SESSION['value_array']);



but I still haven't figured out how to reformat the value inside of the textarea, it's all on one line.

So now the result in the event of an error in the form is:


Hi.My name is John.


When the original value was:

Hi.
My name is John.


Any idea how I could return them as separate lines?

My only concern here is how they appear inside the textarea when an error is found in the form.

xvszero
10-07-2008, 03:18 PM
I think something like this might work?

$message = str_replace("<br>", chr(13).chr(10), $message)

scragar
10-07-2008, 03:32 PM
I think something like this might work?

$message = str_replace("<br>", chr(13).chr(10), $message)

don't do that, use nl2br to add a <br> then use his method for stripping the new lines to get rid of them, your method isn't going to work for linux, mac, BSD, unix, most mobile OSs etc, which don't use the windows \n\r interpretation of a new line.

xvszero
10-07-2008, 03:41 PM
No wonder it took so long to get it working that way. Hmm.

So let me try to wrap my mind around this. What is actually stored in my MySQL database when someone posts with line breaks through say... Linux? I'm still uncertain as to what I'd be converting to <br> if not the \n\r.

scragar
10-07-2008, 03:43 PM
linux uses \n on it's own, I'm sure it's either mac or BSD that uses \r\n

either way you can't assume it's one or the other, best way to deal with it is to just remove the \r's and replace the \n's

xvszero
10-07-2008, 03:59 PM
Ohh... what is the negative of doing something like...

$message = str_replace("<br>", chr(13).chr(10), $message)
$message = str_replace("<br>", chr(10), $message)

I think I tried that in the past and had problems, but I forget what.

bustya
10-08-2008, 02:25 AM
For my issue, the value being passed to the database (a successful form submission) is fine, the space is preserved exactly how the user formatted it. It's only when the form fails (required field left blank or captcha doesn't match) and the user is sent back to the form where the $_SESSION['value_array'] is echoed inside of the textarea that the carriage returns and newlines are added.

Adding <br /> won't work for me. One of the textareas is for user-submitted CSS, so <br /> wouldn't work there. The best I can come up with so far is what I have in my last post with one slight modification...

replacing the \r with ' (no space here)'
and the \n with ' (about five spaces here) '

...that's good enough until I figure something else out.