简体   繁体   English

所见即所得引导程序保存到数据库

[英]wysiwyg bootstrap save to database

I am unable to save content written inside the editor to my database.我无法将编辑器中写入的内容保存到我的数据库中。

I have noticed that if i change the div to textarea, it saves just fine to the database.我注意到,如果我将 div 更改为 textarea,它会很好地保存到数据库中。 But that 'solution' removes the functionality of the wysiwyg and is unable to save in rich data format, which I need.但是该“解决方案”删除了所见即所得的功能,并且无法以我需要的丰富数据格式保存。

So how do I save to my database with divs?那么如何使用 div 保存到我的数据库中呢?

Heres my code:这是我的代码:

<form method="post" action="index.php">
      <div id="editor" name="test">
        <?

  $sql = "SELECT blaabog FROM brugere WHERE id='1'";
  $result = $conn->query($sql);

      while($row = $result->fetch_assoc()) {
          echo $row["blaabog"];
      }

  ?>
</div>
      <button class="btn btn-default" type="submit">Send Post</button>
    </form>

If I change my code to this, it saves to the database, but as described above ruins other functionality..如果我将代码更改为此,它会保存到数据库中,但如上所述会破坏其他功能..

<form method="post" action="index.php">
      <textarea id="editor" name="test">
        <?

  $sql = "SELECT blaabog FROM brugere WHERE id='1'";
  $result = $conn->query($sql);

      while($row = $result->fetch_assoc()) {
          echo $row["blaabog"];
      }

  ?>
</textarea>
      <button class="btn btn-default" type="submit">Send Post</button>
    </form>

How do I fix this so I am able to save to my database in rich data format?我该如何解决这个问题,以便我能够以丰富的数据格式保存到我的数据库中?

Thanks谢谢

In order to get the contents of the richtext editor to submit to a PHP page:为了让富文本编辑器的内容提交到一个 PHP 页面:

Create a hidden text input on the same page as the richtext editor, in the same form.在与富文本编辑器相同的页面上以相同的形式创建隐藏文本输入。

Then add an onSubmit function to your form that copies the contents of the div to the hidden text input.然后向您的表单添加一个 onSubmit 函数,该函数将 div 的内容复制到隐藏的文本输入。

It would look something like:它看起来像:

<form name="my_form" method="post" onSubmit="document.my_form.editor_contents.value = $('#editor').html()" action="index.php">

<textarea name="editor_contents" style="display:none;"></textarea> 

      <div id="editor" name="test">
        <?

  $sql = "SELECT blaabog FROM brugere WHERE id='1'";
  $result = $conn->query($sql);

      while($row = $result->fetch_assoc()) {
          echo $row["blaabog"];
      }

  ?>
</div>
      <button class="btn btn-default" type="submit">Send Post</button>
    </form>

note Don't forget this time you must save the editor_contents to database.注意不要忘记这次您必须将editor_contents保存到数据库中。

just add the onclick event something like this只需添加这样的 onclick 事件

<button onclick=" $('#txtEditor').val($('.Editor-editor').html());" class="btn btn-default" type="submit">Send Post</button>

note the #txteditor is the id of your form once this is done you will get the functionality of the wysiwyg.请注意#txteditor是表单的 ID,一旦完成,您将获得所见即所得的功能。 to play around with it you can change the .html to .text and it will save to database but without the the functionality of the wysiwyg.要使用它,您可以将.html更改为.text ,它将保存到数据库,但没有所见即所得的功能。

If someone has the same problem I have a solution:如果有人有同样的问题,我有一个解决方案:

I changed my wysiwyg editor to TINYmce, which stores the information in the database just fine with the following code:我将我的所见即所得编辑器更改为 TINYmce,它可以使用以下代码将信息存储在数据库中:

Editor code:编辑器代码:

 <script src="//cdn.tinymce.com/4/tinymce.min.js"></script>

PHP: PHP:

  // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


if(isset($_POST['text'])) {


  $text = $_POST['text'];




  $sql = "UPDATE users SET blaabog = '" . $text . "' WHERE id = 1";

  if ($conn->query($sql) === TRUE) {
  echo "Record updated successfully";
  } else {
  echo "Error updating record: " . $conn->error;

} } } }

?> ?>

And some more PHP:还有一些PHP:

<form method="post">
            <textarea name="text">
                                      <?

            $sql = "SELECT * FROM users WHERE id=1";
            $result = $conn->query($sql);

                while($row = $result->fetch_assoc()) {
                    echo $row["blaabog"];
                }

            ?>
          </textarea>
        </div>
        <div class="widget-container">
          <div class="widget-content">
              <div class="form-content">
                <div id="response"></div>
              </div>
              <div class="form-footer">
              <input type="submit" class="btn btn-success primary-btn" value="Gem blå bog">
              </div>
            </form>

I am not sure how but the code above works.我不确定上面的代码是如何工作的。 I bought a solution from a professional - so sorry if my explaination is bad.我从专业人士那里买了一个解决方案 - 如果我的解释不好,很抱歉。

Anyway, if you have the same problem as me, the code above should do trick!无论如何,如果你和我有同样的问题,上面的代码应该可以解决问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM