Ceejay
Ceejay

Reputation: 7267

How can i save the tinyMCE editor content to mysql

I am using tinyMCE editor to create posts in my app,once i format text and text alignments, create post or update post i need to click save. and when i click save, the tinyMCE content should get saved to mysql database. how can i do that with php?

  tinymce.init({
    entity_encoding : "raw",
    selector: '#mytextarea',
    plugins: "table print textcolor colorpicker anchor link searchreplace media emoticons lists visualblocks preview wordcount hr contextmenu",
    mediaembed_service_url: 'SERVICE_URL',
    mediaembed_max_width: 450,
    toolbar: "save print forecolor backcolor anchor link searchreplace emoticons visualblocks preview",
  });

HTML code

<form method="post">
            <label name="post-title" for="post-title">Post title</label>
            <input type="text" name="post-title" id="post-title"><br>                   
            <label name="post-content" for="post-title">Post Content</label>
            <textarea id="mytextarea"></textarea>
            <br><br>            
            <!-- <input type="submit" class="btn btn-beautuful-blue" name="submit" value="CREATE POST"> -->
        </form>

Upvotes: 0

Views: 17778

Answers (2)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Make following changes:

<textarea id="mytextarea"></textarea>

to 

<textarea id="mytextarea" name="mytextarea"></textarea>

// provide a name so that we can get its content by using its name

And to get its value

$content = $_REQUEST['mytextarea'];

Now save the content of variable $content to database

Upvotes: 3

N. Hamelink
N. Hamelink

Reputation: 603

You have to add an action to your form element, let's say you want to post it to the file "post.php" you'll get <form method="post" action="post.php">. After that you'll need a name attached in your textarea field, I am calling it "mytextarea" since you are using the same name in your ID (<textarea id="mytextarea"></textarea>). In the file "post.php" you need to have an MySQL connection, I always make them with the PDO object, there are other possibilities. Besides that you'll need a check or the post content is set.

post.php

// Checking or there is a post request, and checking or the "mytextarea" field is set
if($_SERVER['request_method'] == 'POST'
&& !empty($_POST['mytextarea']))
{
    // Creating the database connection
    $dsn = 'mysql:dbname=databasename;host=127.0.0.1';
    $user = 'username';
    $password = 'password';

    try {
        $dbh = new PDO($dsn, $user, $password);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }

    // Getting the variables from the form
    $title = isset($_POST['post-title']) : $_POST['post-title'] ? '';
    $content = $_POST['mytextarea'];

    // Executing the query
    $query = $dbh->prepare('INSERT INTO tablename (item_title, item_content) VALUES (:item_title, :item_content)');
    $query->execute([':item_title' => $title, ':item_content' => $content]);
}

Links

http://php.net/manual/en/pdo.construct.php

http://php.net/manual/en/pdo.prepare.php

http://php.net/manual/en/reserved.variables.server.php

Upvotes: 2

Related Questions