browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need PHP help?

Get answers from our community of PHP experts on BYTES! It's free.

Editing HTML in a form?

Schraalhans Keukenmeester
Guest
 
Posts: n/a
#1: Feb 15 '07
I want to build a very basic online text editor, to allow customers to
modify their own sites' html content using a simple form with textarea
element.

I read the HTML file like so:

<?PHP

function EditText ($filepath)
{
if (!$file = file($filepath)) return false;
foreach ($file as $line) {
$value .= $line; // contains all text in file when done
}
$html = "<html><head><title>Editor</title></head>
<body>
<form method='post' action='processtext.php'>
<textarea rows='40' cols='80' name='txt' value='$value'>
<input type='submit'>
</form>
</body>
</html>";
echo $html;
return true;
}

EditText ('/sample/index.html');
?>

The problem comes when the parsed file containing html elements itself
is echoed to the browser. In itself, PHP does what it's asked, but all
browsers I tried choke on the contents of the TEXTAREA field. Best
example may be: try parsing the file containing the script itself, it
becomes a real mess.

I tried htmlspecialchars, this did not fix it. I tried addslashes to
only escape the quotes in the parsed file, also no success.

I saw one example online where 'they' preg_replaced about every possible
(x)html and php syntax element, resulting in an immensely large and slow
script. Can't imagine that's the proper way forward.

If at all possible I'd like to stay away from Javascript. Any idea if
this is a realistic approach? Are there any useable scripts available? I
searched google a lot, but I keep ending up at sites that offer html or
php editors for local use, not in a browser.

Thanks for any good tips!



Captain Paralytic
Guest
 
Posts: n/a
#2: Feb 15 '07

re: Editing HTML in a form?


On 15 Feb, 16:35, Schraalhans Keukenmeester <bitbuc...@invalid.spam>
wrote:
Quote:
I want to build a very basic online text editor, to allow customers to
modify their own sites' html content using a simple form with textarea
element.
>
I read the HTML file like so:
>
<?PHP
>
function EditText ($filepath)
{
if (!$file = file($filepath)) return false;
foreach ($file as $line) {
$value .= $line; // contains all text in file when done
}
$html = "<html><head><title>Editor</title></head>
<body>
<form method='post' action='processtext.php'>
<textarea rows='40' cols='80' name='txt' value='$value'>
<input type='submit'>
</form>
</body>
</html>";
echo $html;
return true;
}
>
EditText ('/sample/index.html');
?>
>
The problem comes when the parsed file containing html elements itself
is echoed to the browser. In itself, PHP does what it's asked, but all
browsers I tried choke on the contents of the TEXTAREA field. Best
example may be: try parsing the file containing the script itself, it
becomes a real mess.
>
I tried htmlspecialchars, this did not fix it. I tried addslashes to
only escape the quotes in the parsed file, also no success.
>
I saw one example online where 'they' preg_replaced about every possible
(x)html and php syntax element, resulting in an immensely large and slow
script. Can't imagine that's the proper way forward.
>
If at all possible I'd like to stay away from Javascript. Any idea if
this is a realistic approach? Are there any useable scripts available? I
searched google a lot, but I keep ending up at sites that offer html or
php editors for local use, not in a browser.
>
Thanks for any good tips!
I haven't examined too closely how Peter does it, but FacileForms
allows one to put HTML & PHP in textareas that then get stored on the
host.

You could download that package and take a look at the various classes
to see how it's done.

Schraalhans Keukenmeester
Guest
 
Posts: n/a
#3: Feb 15 '07

re: Editing HTML in a form?


Captain Paralytic wrote:
Quote:
On 15 Feb, 16:35, Schraalhans Keukenmeester <bitbuc...@invalid.spam>
wrote:
Quote:
>[snip]
>Thanks for any good tips!
>
I haven't examined too closely how Peter does it, but FacileForms
allows one to put HTML & PHP in textareas that then get stored on the
host.
>
You could download that package and take a look at the various classes
to see how it's done.
>
Hadn't seen Facile forms yet. Looks like a rather big package, but
perhaps I can find the details I'm after. Tried a similar approach with
my provider's cms package built-in text editor, but got lost completely
in its messy hardly documented code.

I'll let you know if/when I find something useful there!
Thanks for your input Captain!

Sh.
Ian Taylor
Guest
 
Posts: n/a
#4: Feb 15 '07

re: Editing HTML in a form?


Schraalhans Keukenmeester wrote:
Quote:
I want to build a very basic online text editor, to allow customers to
modify their own sites' html content using a simple form with textarea
element.
>
I read the HTML file like so:
>
<?PHP
>
function EditText ($filepath)
{
if (!$file = file($filepath)) return false;
foreach ($file as $line) {
$value .= $line; // contains all text in file when done
}
$html = "<html><head><title>Editor</title></head>
<body>
<form method='post' action='processtext.php'>
<textarea rows='40' cols='80' name='txt' value='$value'>
<input type='submit'>
</form>
</body>
</html>";
echo $html;
return true;
}
>
EditText ('/sample/index.html');
?>
>
The problem comes when the parsed file containing html elements itself
is echoed to the browser. In itself, PHP does what it's asked, but all
browsers I tried choke on the contents of the TEXTAREA field. Best
example may be: try parsing the file containing the script itself, it
becomes a real mess.
>
I tried htmlspecialchars, this did not fix it. I tried addslashes to
only escape the quotes in the parsed file, also no success.
>
I saw one example online where 'they' preg_replaced about every possible
(x)html and php syntax element, resulting in an immensely large and slow
script. Can't imagine that's the proper way forward.
>
If at all possible I'd like to stay away from Javascript. Any idea if
this is a realistic approach? Are there any useable scripts available? I
searched google a lot, but I keep ending up at sites that offer html or
php editors for local use, not in a browser.
>
Thanks for any good tips!
htmlentities() is probably what you're looking for:

<textarea name='txt'>".htmlentities($value)."</textarea>

Also note the correction of the textarea content format!

HTH,

Ian.
Rik
Guest
 
Posts: n/a
#5: Feb 15 '07

re: Editing HTML in a form?


On Thu, 15 Feb 2007 17:35:21 +0100, Schraalhans Keukenmeester
<bitbucket@invalid.spamwrote:
Quote:
<textarea rows='40' cols='80' name='txt' value='$value'>
Unlike other HTML element, this should be:
<textarea>$value</textarea>

Also, you might want to look into stuff like TinyMCE etc.
--
Rik Wasmus
Schraalhans Keukenmeester
Guest
 
Posts: n/a
#6: Feb 15 '07

re: Editing HTML in a form?


Ian Taylor wrote:
Quote:
Schraalhans Keukenmeester wrote:
Quote:
>I want to build a very basic online text editor, to allow customers to
>modify their own sites' html content using a simple form with textarea
>element.
>>
>I read the HTML file like so:
>>
><?PHP
>>
> function EditText ($filepath)
> {
> if (!$file = file($filepath)) return false;
> foreach ($file as $line) {
> $value .= $line; // contains all text in file when done
> }
> $html = "<html><head><title>Editor</title></head>
> <body>
> <form method='post' action='processtext.php'>
> <textarea rows='40' cols='80' name='txt' value='$value'>
> <input type='submit'>
> </form>
> </body>
> </html>";
> echo $html;
> return true;
> }
>>
> EditText ('/sample/index.html');
>?>
>>
>The problem comes when the parsed file containing html elements itself
>is echoed to the browser. In itself, PHP does what it's asked, but all
>browsers I tried choke on the contents of the TEXTAREA field. Best
>example may be: try parsing the file containing the script itself, it
>becomes a real mess.
>>
>I tried htmlspecialchars, this did not fix it. I tried addslashes to
>only escape the quotes in the parsed file, also no success.
>>
>I saw one example online where 'they' preg_replaced about every possible
>(x)html and php syntax element, resulting in an immensely large and slow
>script. Can't imagine that's the proper way forward.
>>
>If at all possible I'd like to stay away from Javascript. Any idea if
>this is a realistic approach? Are there any useable scripts available? I
>searched google a lot, but I keep ending up at sites that offer html or
>php editors for local use, not in a browser.
>>
>Thanks for any good tips!
>
htmlentities() is probably what you're looking for:
>
<textarea name='txt'>".htmlentities($value)."</textarea>
>
Also note the correction of the textarea content format!
>
HTH,
>
Ian.
Gee, how did I miss that. Thx. Must have been late when I put this
together. Thanks a bunch! Duh! Kindergarten stuff. Why didn't I learn a
skill .... ;-) Great, now I'm done. Works for me like this!

Sh.
Closed Thread