Line breaking on textfields, how to you fix this?

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
User avatar
pallarisari
Forum Newbie
Posts: 17
Joined: Sat Apr 16, 2005 12:52 pm

Line breaking on textfields, how to you fix this?

Post by pallarisari »

Hi, Ill explain my current setup before i describe the problem.
Im running a web form in html which is processing that data via submit.php which posts the data into a text file which is subsequently read back into a data.php file.

The problem is this:
When a user creates a new line in a text file this doesnt seem to be picked up and stored in the txt file and so the text just spans across the page. Can anybody help?

heres my submit.php code

Code: Select all

<?php
include("header.inc");
//writing data to log
{
    $TimeFrame = $_POST['TimeFrame'];
	$filename = "data.txt"; // File which holds all data
    $content = "<b>$TimeFrame\n</b>"; // Content to write in the data file
	$fp = fopen($filename, "a"); // Open the data file, file points at the end of file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}

$Time = $_POST['Time'];
if(!$Time) // If time scale isnt entered show error message
{
    echo "Please enter a time scale.";
    exit;
}
else
{
    $filename = "data.txt"; // File which holds all data
    $content = "$Time\n<br>"; // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	// Promotional text and message and percent
	{
    $promotionaltxt = $_POST['promotionaltxt'];
	$filename = "data.txt"; // File which holds all data
    $content = "<br><b>$promotionaltxt\n</b>"; // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	{
	$Promo = $_POST['Promo'];
	$filename = "data.txt"; // File which holds all data
    $content = "$Promo\n<br>"; // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	{
	$Promotional = $_POST['Promotional'];
    $filename = "data.txt"; // File which holds all data
    $content = "<br>$Promotional\n<br>"; // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	// ending promotional element field data input
	
	// starting large projects data field
	{
	$largeprojectstxt = $_POST['largeprojectstxt'];
	$filename = "data.txt"; // File which holds all data
    $content = "<br><b>$largeprojectstxt\n</b>"; // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	{
	$LProject = $_POST['LProject'];
	$filename = "data.txt"; // File which holds all data
    $content = "$LProject\n<br>"; // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	{
	$LargeProjects = $_POST['LargeProjects'];
	$filename = "data.txt"; // File which holds all data
    $content = "<br>$LargeProjects\n<br>"; // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	//end large projects
	//start admin data fields
	{
	$admintxt = $_POST['admintxt'];
	$filename = "data.txt"; // File which holds all data
    $content = "<br><b>$admintxt\n</b>"; // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	{
	$AdminPercent = $_POST['AdminPercent'];
	$filename = "data.txt"; // File which holds all data
    $content = "$AdminPercent\n<br>"; // Content to write in the data file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	}
	{
	$Admin = $_POST['Admin'];
	$filename = "data.txt"; // File which holds all data
    $content = "$Admin\n<br><br><br><br>"; // Content to write in the data file
	$fp = fopen($filename, "a"); // Open the data file, file points at the end of file
	$fw = fwrite( $fp, $content ); // Write the content on the file
	
// Function to breakup log words in message -------------------------
function wordbreak($text, $wordsize) {

if (strlen($text) <= $wordsize) { return $text; } # No breaking necessary, return original text.

$text = str_replace("\n", "", $text); # Strip linefeeds
$done = "false";
$newtext = "";
$start = 0; # Initialize starting position
$segment = substr($text, $start, $wordsize + 1); # Initialize first segment

while ($done == "false") { # Parse text

	$lastspace = strrpos($segment, " ");
	$lastbreak = strrpos($segment, "\r");

	if ( $lastspace == "" AND $lastbreak == "" ) { # Break segment
		$newtext .= substr($text, $start, $wordsize) . " ";
		$start = $start + $wordsize; }
	else { # Move start to last space or break
		$last = max($lastspace, $lastbreak);
		$newtext .= substr($segment, 0, $last + 1);
		$start = $start + $last + 1;
	} # End If - Break segment

	$segment = substr($text, $start, $wordsize + 1);

	if ( strlen($segment) <= $wordsize ) { # Final segment is smaller than word size.
		$newtext .= $segment;
		$done = "true";
	} # End If - Final segment is smaller than word size.

} # End While - Parse text

$newtext = str_replace("\r", "\r\n", $newtext); # Replace linefeeds

return $newtext;

} # End of function - Word Break
	
	
	fclose( $fp ); // Close the file after writing
	if(!$fw) echo "Couldn't write the entry.";
    else echo "Your entry has been successful";
}
?> 
<?php
include("footer.inc");
?>
my submit code is a little messy but its the first time ive coded in this language. Unfortuantely my limitations are that i cant use sql databases and has to run as a flat file system via txt files.

thank you very much
Ari :.
R0d Longfella
Forum Newbie
Posts: 20
Joined: Fri Apr 08, 2005 7:17 am

Post by R0d Longfella »

My guess is your newlines are being picked up, but just don't show up in the browser. Try using "view source" in IE to check if you can see the newlines. The problem is that a newline doesn't doe anything in HTML, it's just regarded as a whitespace. If you wan't something to start at a new line, then you need to use a break. (ie. <br>) Try using nl2br (), which converts all newlines to breaks.

Goodluck!

R0d
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is this being done on a Windows machine? You need to use the 't' open modifier for Windows machines and text files. Such as:

Code: Select all

fopen( $filename, 'at' );
that tells windows to use text mode for file accessing.. which will allow it to write \r\n to the file, thus most editors will display the data correctly.

If this is being done on another OS besides Windows, but viewing the file on Windows, that may be the case, depending on how your machine transferred the file...
User avatar
pallarisari
Forum Newbie
Posts: 17
Joined: Sat Apr 16, 2005 12:52 pm

Post by pallarisari »

Unfortunately that doesnt seem to work. I am using a windows machine but i dont have php installed on it. Im working offline and upload files to my hosting account.
User avatar
pallarisari
Forum Newbie
Posts: 17
Joined: Sat Apr 16, 2005 12:52 pm

Post by pallarisari »

heres a link to my form. Feel free to enter data and test the output.
http://www.gnrlies.com/test/form/input.htm
R0d Longfella
Forum Newbie
Posts: 20
Joined: Fri Apr 08, 2005 7:17 am

Post by R0d Longfella »

I feel sorry to repeat myself, but the problem really is caused by the newlines and the breaks. I just posted the data, and in the browser it showed up like this:

Code: Select all

Week Commencing: asdfasdf 

Promotional: 90% 

adsfasdf qwreqwreqwt tyrtuyrtuy bn,mbn,m 

Large Projects: 0%
While in notepad it looks like this:

Code: Select all

<br><br><b>Week Commencing: 
</b>asdfasdf
<br><br><b>Promotional: 
</b>90%
<br><br>adsfasdf
qwreqwreqwt
tyrtuyrtuy
bn,mbn,m
<br><br><b>Large Projects: 
</b>0%
As you can see, the newlines are really in place, but you must use breaks to show it in the browser. Try using nl2br, since that function replaces all newlines to <br>.

Code: Select all

print htmlspecialchars (nl2br ($sWorkload));
Have fun!
User avatar
pallarisari
Forum Newbie
Posts: 17
Joined: Sat Apr 16, 2005 12:52 pm

Post by pallarisari »

prob is im new to php and so i dont know where to use that code :(

sorry to be a pain, could you possibly apply it to one of my multiple text boxes in the above code so i can use as an example to follow? Im new at php and so i dont know how to incorporate this tag :oops:

thx
ari
User avatar
pallarisari
Forum Newbie
Posts: 17
Joined: Sat Apr 16, 2005 12:52 pm

Post by pallarisari »

Code: Select all

print htmlspecialchars (nl2br ($sWorkload));

dude you are a complete winner, its work. thank you so much :) :) :)
User avatar
pallarisari
Forum Newbie
Posts: 17
Joined: Sat Apr 16, 2005 12:52 pm

Post by pallarisari »

Thanks so much for the help on that one, another quick formating question.
Can i apply a predefined css class for one of my fields so that the formating attributes i.e. colour font size are associated with it?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why couldn't you?
User avatar
pallarisari
Forum Newbie
Posts: 17
Joined: Sat Apr 16, 2005 12:52 pm

Post by pallarisari »

not sure why it didnt work :roll:

i used the other method though and got it working. Is there a way of feeding in css class values to certain data?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

might be how your CSS is structured, or how the code works.
User avatar
pallarisari
Forum Newbie
Posts: 17
Joined: Sat Apr 16, 2005 12:52 pm

Post by pallarisari »

i think it was my code to be honest. First time ive ever coded in php so I think its a little messy and occasionally conflicting. but hey its working now. thanks for your help everyone
R0d Longfella
Forum Newbie
Posts: 20
Joined: Fri Apr 08, 2005 7:17 am

Post by R0d Longfella »

Sorry for having you waiting so long.

You could apply css formating on a single element by giving that element an id, and using # in the stylesheet to point to the element.

In HTML:

Code: Select all

<input type=text id=in1 name= value=>
Stylesheet:

Code: Select all

#in1 { font: Arial, 12pt; }
Hope it works!

R0d
Post Reply