【问题标题】:How to save the content from a wysisyg inline-editor to a .php file如何将 wysisyg 内联编辑器中的内容保存到 .php 文件
【发布时间】:2014-06-04 17:47:10
【问题描述】:

我找到了一个“HTML5 WYSISYG Inline Editor”,我在我的本地主机上运行它(Ubuntu 14.04)。

这样做的目的是将其嵌入我的网站,并将其用作我网站的主要写作工具。我需要能够选择文件名或让它在文件名的空白处添加-

这是我为保存其内容而编写的代码

(CodePen from original author: HTML5 WYSISYG Inline Editor)

inline.php

  <form action="effe.php" method="post">

      <input type="text" name="author" value="" placeholder="Author">
      <input type="text" name="header" value="" placeholder="header">
      <input type="datetime" name="datetime" value="" placeholder="datetime">

    <div id='editor' contenteditable contextmenu="mymenu" name='editor'>
      <p>This is just some example text to start us off</p>
    </div>


    <div class="tags">

      <input type="text" name="" value="" placeholder="tag">
      <input type="text" name="" value="" placeholder="tag">
      <input type="text" name="" value="" placeholder="tag">

    </div>


    <input type="submit" value="Submit">
  </form>

保存.php

<?php

if (!empty($_POST))
{
    foreach ( $_POST as $key => $value )
    {
        if ( ( !is_string($value) && !is_numeric($value) ) || !is_string($key) )
            continue;

?>

        <?php echo htmlspecialchars( (string)$key ); ?>

<div class="article-meta">
   <a class="author" rel="author" title="author" href="/about" target="_blank"><?php echo "$author";?></a> 

   <time datetime="<?php echo "$datetime";?>" title="<?php echo "$datetime";?>"><?php echo "$datetime";?></time>

</div> 

<h1><?php echo "$header"; ?></h1>

<?php echo "$editor"; ?>

    <div class="tags">

      <span> <?php echo "$tag1"; ?> </span>
      <span> <?php echo "$tag2"; ?> </span>
      <span> <?php echo "$tag3"; ?> </span>

    </div>

    <?php
    }
}
?>

结果:

我已经度过了美好的一天,只是没有看到做错了什么。请注意,我的php 知识非常基础。

我知道使用db 是一种选择,但在我的脑海中,我只在过去一两年写过类似20+ 的文章。当我达到 100 篇文章时,我会考虑切换到数据库。

【问题讨论】:

    标签: javascript php html editor inline


    【解决方案1】:

    我不是 100% 确定你是什么;正在努力完成。我是你提到的那篇文章的作者,我很乐意提供帮助。

    我不确定您尝试使用的方法是否实用。这里最好的选择是建立一个数据库并将内容写入其中。

    使用 php 非常简单,我建议看一下 PDO,即数据库抽象层。还有一些其他的,但是这个可以为你做一些与注入攻击等有关的工作,所以这是一个很好的起点。

    听起来你需要一个相当简单的数据库表,很可能是这样的:

    create table articles (
      article_id INT NOT NULL AUTO INCREMENT,
      title VARCHAR(100) NOT NULL,
      content TEXT NOT NULL,
      article_date DATE
      PRIMARY KEY (tutorial_id)
    );
    

    当您从编辑器表单返回数据时,您可以执行以下操作:

    $dbh = new PDO('mysql:host=localhost;dbname=articles', 'user', 'pwd');
    $sth = $dbh->prepare('INSERT INTO articles (title, content, article_date) values (:title, :content, NOW())');
    $sth->bindParam(':title', $_POST['title']);
    $sth->bindParam(':content', $_POST['content']);
    $sth->execute();
    

    要从数据库中撤回,请使用以下内容: $dbh = new PDO('mysql:host=localhost;dbname=articles', 'user', 'pwd'); $sth = $dbh->query('SELECT * FROM article');

    除此之外还有更多内容,例如,您需要添加一个 try/catch 块以避免错误,并且您可能希望选择特定的文章(即 SELECT * FROM article where article_id=45)但是我希望这会为您指明正确的方向。

    我认为尽量避免使用数据库会导致大量额外工作,一旦您开始使用 MySQL 之类的东西,您很快就会启动并运行!

    【讨论】:

    • 所以将&lt;div id='editor'...的内容保存到x.php文件中。感谢您提供 Mozilla 文档的链接!看起来真的很有帮助!
    • @blade19899 抱歉,我重新阅读了这个问题并更新了我的答案,因为我觉得我原来的答案根本不正确。希望我添加的答案更完整,对您更有帮助。如果您需要更多帮助,请随时直接与我们联系 :)
    • 抱歉回复晚了(工作、考试、女性。)我没有那么多文章,所以现在 - 正如我的 A 中所述 - 我想保存编辑器的内容,在.php。我将来会研究数据库。
    • 可以做到,但让您的 Web 服务器能够写入 Web 根目录并不是很好的做法。... 攻击者可能会通过在那里写入恶意文件来危害服务器...
    • 仅用于教育目的!此外,我没有高知名度的网站。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多