0

I'am using CKEditor and i have problem with upload into database. First of all I want take the value from textarea id (I dont want use textarea name but the id) and give the value in the hidden input.

HTML & Jquery (test.php)

<!DOCTYPE html>
<html>
<head>
    <!-- CKEditor full package v4.7.3 -->
    <script type="text/javascript" src="ckeditor/ckeditor.js"></script>
    <!-- Jquery -->
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
    <form action="test2.php" method="POST" target="_blank">
        <textarea id="description-down1"></textarea>
        <script type="text/javascript">CKEDITOR.replace("description-down1")</script>
        <br><br>
        <input type="submit" name="save-button" value="Insert">
        <!-- #3 take the value via name into php -->
        <input type="hidden" name="insert-variable-value-name" id="insert-variable-value-id">
    </form>
    <script type="text/javascript">

        $(document).ready(function(){

            //#1 take value from textarea "id"
            var data = // what code write here?

            //#2 put the value of textarea into hidden input
            document.getElementById('insert-variable-value-id').value = data;

        });

    </script>
</body>
</html>

PHP (test2.php)

<?php 
    //connection
    $conn = new mysqli("localhost", "root", "", "test_engine");

    // call the value from the hidden input
    $description = $_POST['insert-variable-value-name'];

    // Insert data
    $insert_data = "INSERT INTO test (description)
                  VALUES('$description')";
    $conn->query($insert_data);
?>
madhead
  • 31,729
  • 16
  • 153
  • 201
Andrew
  • 5
  • 5
  • 2
    Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour) and edit your question to show both the incorrect results, and the results you want with some sample data. – CJ Dennis Nov 02 '17 at 00:22

2 Answers2

0
var data = CKEDITOR.instances['description-down1'].getData();

You need to remeber to set hidden input value before form submit or update that field in some interval.

Bart
  • 1,268
  • 2
  • 12
  • 14
0

Thanks Bart for the help. I find here the answer. First runs the code, then submit.

Previus Code:

<body>
    <form action="test2.php" method="POST" target="_blank">
        <textarea id="description-down1"></textarea>
        <script type="text/javascript">CKEDITOR.replace("description-down1")</script>
        <br><br>
        <input type="submit" name="save-button" value="Insert">
        <!-- #3 take the value via name into php -->
        <input type="hidden" name="insert-variable-value-name" id="insert-variable-value-id">
    </form>
    <script type="text/javascript">

        $(document).ready(function(){

            //#1 take value from textarea "id"
            var data = // what code write here?

            //#2 put the value of textarea into hidden input
            document.getElementById('insert-variable-value-id').value = data;

        });

    </script>
</body>

Edited Code:

<body>
    <!-- Create form id='form-id' -->
    <form action="test2.php" method="POST" target="_blank" id='form-id'>
        <textarea id="description-down1"></textarea>
        <script type="text/javascript">CKEDITOR.replace("description-down1")</script>
        <br><br>
        <!-- Create submit id='save-button' -->
        <input type="submit" name="save-button" value="Insert" id='save-button'>
        <!-- #3 take the value via name into php -->
        <input type="hidden" name="insert-variable-value-name" id="insert-variable-value-id">
    </form>
    <script type="text/javascript">

        $(document).ready(function(){

            $('#save-button').click(function(e){

                //Stop
                e.preventDefault();

                //The code

                //#1 take value from textarea "id"
                var data = CKEDITOR.instances['description-down1'].getData();

                //#2 put the value of textarea into hidden input
                document.getElementById('insert-variable-value-id').value = data;

                //Proceed the submit
                $('#form-id').submit();

             });

        });

    </script>
</body>
Andrew
  • 5
  • 5