簡體   English   中英

如何將textarea值插入數據庫?

[英]How to insert textarea value into database?

我正在使用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);
?>
var data = CKEDITOR.instances['description-down1'].getData();

您需要記住在表單提交或以一定間隔更新該字段之前設置隱藏的輸入值。

感謝Bart的幫助。 我在這里找到答案。 首先運行代碼,然后提交。

上一個代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM