如何将textarea值插入数据库?

时间:2017-11-01 23:30:08

标签: php jquery html mysql ckeditor4.x

我正在使用CKEditor,我上传到数据库时遇到问题。首先,我想从textarea id获取值(我不想使用textarea name但是id)并在隐藏输入中给出值。

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);
?>

2 个答案:

答案 0 :(得分:0)

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

您需要记住在表单提交之前设置隐藏的输入值或在某个时间间隔内更新该字段。

答案 1 :(得分:0)

感谢Bart提供帮助。我找到here答案。首先运行代码,然后提交。

Previus代码:

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

编辑代码:

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