使用php和java将数据从iframe / textarea插入到db中

时间:2017-04-28 21:00:56

标签: javascript php mysqli

我无法插入已在iframe上设计/编写的数据。我的代码如下。

我尝试将iframe更改为textarea。这似乎有效,但我无法添加粗体,斜体,不同字体等格式。

请帮助纠正我的代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <!-- <script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> <script type="text/javascript">
            bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });
        </script>-->
    </head>
    <style>
        .image{
            border-radius:50%;
        }
    </style>

    <script>
        function enableEditMode(){
            TextField.document.designMode="On";
        }
        //function ya buttun zote 
        function boldCmd(command){
            TextField.document.execCommand(command, false, null)
        }
        //function ya font name and fontsize
        function execCommandWithArg(command, arg){
            TextField.document.execCommand(command, false, arg)
        }
    </script>

    <body onload="enableEditMode();">
        <form action="upload.php" method="post" enctype="multipart/form-data" > 
            <input type="file" name="image" />
            <div>
                <button name="al" onclick="boldCmd('bold');" type="button"><i class="fa fa-bold"></i></button>
                <button onclick="boldCmd('italic');" type="button"><i class="fa fa-italic"></i></button>
                <button onclick="boldCmd('justifyCenter');" type="button"><i class="fa fa-align-center"></i></button>
                <button onclick="boldCmd('justifyLeft');" type="button"><i class="fa fa-align-left"></i></button>
                <button onclick="boldCmd('justifyRight');" type="button"><i class="fa fa-align-right"></i></button>
                <button onclick="boldCmd('justifyFull');" type="button"><i class="fa fa-align-justify"></i></button>
                <button onclick="boldCmd('cut');" type="button"><i class="fa fa-cut"></i></button>
                <button onclick="boldCmd('copy');" type="button"><i class="fa fa-copy"></i></button>
                <button onclick="boldCmd('undo');" type="button"><i class="fa fa-undo"></i></button>
                <button onclick="boldCmd('redo');" type="button"><i class="fa fa-repeat"></i></button>
                <button onclick="boldCmd('underline');" type="button"><i class="fa fa-underline"></i></button>
                <br />

                <select onchange="execCommandWithArg('fontName', this.value);">
                    <option value="Arial">Arial</option>
                    <option value="Times new Roman">Times New Roman</option>
                    <option value="Agency FB">Agency FB</option>
                </select>

                <select onchange="execCommandWithArg('fontSize', this.value);">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                </select>
            </div>
            <iframe   name="TextField" style="width:500px; height:300px;"></iframe>
            <input type="submit" name="submit" />
        </form>
        <?php
        if(isset($_POST["submit"])){
            $con = mysqli_connect("localhost","root","","image") or die("not");   
            $TextField1=$_POST['TextField'];
            $target_dr = "upload/";
            $target_file = $target_dr . basename($_FILES["image"]["name"]);
            if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) 
            {
                echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
            $image=basename( $_FILES["image"]["tmp_name"]); 
            $sql="INSERT INTO picha(image,file,title) 
            VALUES('$image','$target_file','$TextField1')";
            mysqli_query($con,$sql);
        }
        $sql2="SELECT * FROM picha ORDER BY id DESC";
        $result= mysqli_query($con,$sql2);
        if($result-> num_rows > 0){
            echo "<table>";
            while($row=$result->fetch_assoc()){
                echo"<tr>";
                echo "<td><img class='image' src='".$row["file"]."' height='190' width='190'/></td><td>".$row["title"]."</td>[enter image description here][1] </tr>";
            }
            echo "</table>"; 
        }
        mysqli_close($con);
        ?>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

现在我明白了,你的问题是什么。所以:

  1. 你不能使用iframe而不是textarea,你永远不会在表单提交上收到它的价值。
  2. 如果你想在textarea中添加样式到内容,你必须使用某种WYSIWYG编辑器。与TinyMCE(或其他选择)一样。