php textbox + html tags

how to write to a file, including html tags

Page 1 of 1

2 Replies - 2654 Views - Last Post: 18 May 2009 - 09:31 AM Rate Topic: -----

#1 sunion   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 92
  • Joined: 24-August 08

php textbox + html tags

Post icon  Posted 18 May 2009 - 07:51 AM

Hey, I've been making a site in php for 2 days now.
It looks good so far, but i got some probs.

The text that people can read on my index page is actually just a textfile that I outputted with an 'echo'.
Now I am trying to edit the textfile on a other page with a textbox and button.
The basics of my script works, on page load it shows the current text from the textfile in the textbox,
and when I click on the button it writes the text from the textbox to the file.

But the problem is: when I write something and I press 'enter' and submit it, the enter doesnt shows up in the index... It will just put all the text together and ignore the 'enter'.

This is the script that outputs it on my index:
	<?php
$file=fopen("indexmain.dt","r") or exit("Er is een probleem met de inhoud van de pagina!");
while (!feof($file))
  {
  echo fgetc($file);
  }
fclose($file);
?>


And this is the script to write:
<?php
if ($_POST["submit"] == "Submit")
{
$fp = fopen('indexmain.dt', 'w');
fwrite($fp, $_POST["tekst"]);
fclose($fp);
echo "gelukt";
}
?>


Here are the 2 pages if you wanna test it:
index: http://sunion.be/dtsite/index.php
writer(if you click submit just refresh the index to see the change.): http://sunion.be/dtsite/writer.php


I hope someone can help me?

-thanks in advance-

Is This A Good Question/Topic? 0
  • +

Replies To: php textbox + html tags

#2 ShaneK   User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: php textbox + html tags

Posted 18 May 2009 - 08:20 AM

It's the output you're having problems with, and there are much easier ways to do it.
Try this:
<?php
$file = file_get_contents("indexmain.dt");
if(!$file){
exit("Er is een probleem met de inhoud van de pagina!");
}else{
echo(nl2br($file));
}
?>


This is untested, but I'll explain it.
Instead of reading the file as an array and echoing it line by line, we're taking the entire file in as a string, then we're using nl2br (new line to break) which converts new lines to new lines and HTML line breaks. (From "\n" to "\n<br>") and we output the result of that with the echo...actually, we use the nl2br and the echo on the same line, but you get the point.

The problem you are facing is that new lines in text documents are not the same as new lines in HTML. You need an actual HTML line break in order for you to have one in your webpage.

Hope this helps.

Yours,
Shane~

This post has been edited by ShaneK: 18 May 2009 - 09:26 AM

Was This Post Helpful? 1
  • +
  • -

#3 sunion   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 92
  • Joined: 24-August 08

Re: php textbox + html tags

Posted 18 May 2009 - 09:31 AM

View PostShaneK, on 18 May, 2009 - 07:20 AM, said:

It's the output you're having problems with, and there are much easier ways to do it.
Try this:
<?php
$file = file_get_contents("indexmain.dt");
if(!$file){
exit("Er is een probleem met de inhoud van de pagina!");
}else{
echo(nl2br($file));
}
?>


This is untested, but I'll explain it.
Instead of reading the file as an array and echoing it line by line, we're taking the entire file in as a string, then we're using nl2br (new line to break) which converts new lines to new lines and HTML line breaks. (From "\n" to "\n<br>") and we output the result of that with the echo...actually, we use the nl2br and the echo on the same line, but you get the point.

The problem you are facing is that new lines in text documents are not the same as new lines in HTML. You need an actual HTML line break in order for you to have one in your webpage.

Hope this helps.

Yours,
Shane~


Aight, the untested method works perfectly, thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1