wysiwyg bootstrap save to database

jonx333 picture jonx333 · Dec 23, 2015 · Viewed 7.4k times · Source

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. 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?

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

Answer

LetsSeo picture LetsSeo · Dec 24, 2015

In order to get the contents of the richtext editor to submit to a PHP page:

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.

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.