I am messing around with WYSIWYG-Editors and would like to get the current content of it with php and append it to html or lets say a <p>
. I have to say that I am a PHP-beginner, but i've figured out how to get the content of a .txt
and echo
it to my html.
<?php
$textfile = "text.txt";
$text = file($textfile);
echo $text
?>
very simple. But there must be a possibility to "replace" the text.txt
with a wysiwyg-editor.
Does anyone of you have a hint, i would really appreciate that.
Thanks
Edit: In Detail that means, i have a website index.html
, with some text-content. I don't want to use a CMS
but kind of a other html which can the user access textedit.html
and type some sentences in a WYSIWYG
editor like CK-Editor
or TinyMCE
. textedit.html
accesses the index.html
and changes a defined <p>
tag.
The content of the editor will come in most cases from a POST Request of a form.
<form action="process.php" method="POST">
<textarea name="text" id="editor"></textarea>
<input type="submit" value="Submit" />
</form>
And then in process.php
:
<?php
$content = $_POST['text'];
echo $content;
?>
Of course you have to add some validation to this. However this should give you a simple idea and get you started.
You can also start a google search like this: "form processing php".
You need some kind of server-side action to do this. This is not possible with pure HTML! You need to add a server-side backend.
Just a little diagramm to illustrate this:
editor.html
| sends changes to backend
v
backend (changes the contents of the frontend)
|
v
content.html
This is a very poor design (to change the contents of the html file directly) but the principal is the same. In "good" setups you would have a database which holds the contents and the frontend would pull from there and the backend pushes. But with pure HTML this is not possible!
So let me give you some boilerplate:
index.php:
<html>
<head>
<!-- add more stuff here -->
</head>
<body>
<h1>Your Site</h1>
<!-- add more stuff here -->
<p>
<?php
echo file_get_contents('article.txt');
?>
</p>
<!-- add more stuff here -->
</body>
</html>
editor.html:
<html>
<head>
<!-- add more stuff here -->
</head>
<body>
<h1>Your Site - Editor</h1>
<!-- add more stuff here -->
<form action="process.php" method="POST">
<input type="password" name="pwd" placeholder="Password..." />
<textarea name="text" id="editor"></textarea>
<input type="submit" value="Submit" />
</form>
<!-- add more stuff here -->
</body>
</html>
process.php:
<?php
if(!isset($_POST['text']) {
die('Please fill out the form at editor.html');
}
if($_POST['pwd'] !== 'your_password') {
die('Password incorrect');
}
file_put_contents('article.txt', $_POST['text']);
header("Location: index.php");
?>
This is a VERY basic boilerplate and should help you to get started. Tweak it as you go.