Page 1 of 1

TextArea form processing problem

Posted: Sat Jul 29, 2006 2:31 pm
by Erfangc
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello, I am new to the php developer's network as well as the php language itself. Please forgive me if this is the wrong forum to ask this
sort of question.

On one of my web page, a php script is supposed to retrieve a string from the global array $_POST then store the information into MySQL. On another page, I provided means for the user to view this information by accessing the database. However, whenever the data is displayed there seems to be no carrige return or new lines at all. All the characters are lined up in their cell in a marathon (the text only goes to the next line when it reaches the border of the cell, which has a fixed width). Here is the partial script I used to perform this task:

submission.php

[syntax="html"]<P>
   	<textarea name="content" rows="20" cols="60">
process_submission.php[/syntax]

Code: Select all

<?php
	//Attempt To Connect To Database
	$content = $_POST['content'];
	$title = $_POST['text_title'];
	$author = $_POST['author'];
	$host = "localhost"; //Subject To Change On The Production Server
	$user = "avantsci";
	$password = "delta08";
	$connection = mysql_connect($host,$user,$password) or die(mysql_error());
	mysql_select_db("avantsci?user?submission");
	$query = "INSERT INTO submissions (title, author, content) VALUES ('".$title ."', '".$author."', '".$content."')";
	$results = mysql_query($query) or die (mysql_error());
	include 'pre_content.php';	
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Jul 29, 2006 2:38 pm
by volka
In HTML a linebreak is represented by the element <br />.
The data of a <textarea> element is treated like pre-formatted text (the <pre> element), a linebreak shows up as \r and/or \n (i.e. chr(13) or chr(10))
So you need the \r \n to become <br /> when you output the text as html again.

see http://de2.php.net/nl2br

Posted: Sat Jul 29, 2006 2:50 pm
by Erfangc
thanks for the prompt reply.
p.s I will no doubt follow the convention for posting code in the future.

The new line problem is solved, what about tabs?

Posted: Sat Jul 29, 2006 3:17 pm
by Erfangc
The new line problem is solved, what about tabs?

Posted: Sat Jul 29, 2006 4:40 pm
by Erfangc
I have tried str_replace("\t"......) but it kind of didnt work. I know \t as an escape character from Java but I m not sure if the same applys in php

Posted: Sat Jul 29, 2006 4:43 pm
by volka
It's the same special character.

Code: Select all

$out = str_replace("\t", '&nbsp;&nbsp;', $in);
echo $out;