POST方法无效<code> tag

时间:2017-11-11 13:34:50

标签: php html post

<form name="submit_form" action="./uploads/submit.inc.php" method="POST">
        <div class="wrapper">
             <code id="ace-editorid" name="code_editor">Your Code Here</code>
        </div>  
        <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
        <script src='http://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js'></script>
        <script  src="./css/script.js"></script>
        Type the Problem ID: <input type="text" name="probId">
        <input type="submit" name="b_code_submit" class="submit_query">
</form>

When user enters the code in the code editor and presses the submit_query button, then when i'm trying to extract the code in another php file using $code = $_POST["code_editor"] , then i'm getting nothing in the $code variable. But the code works fine when i use <textarea> tag instead of <code> tag.

1 个答案:

答案 0 :(得分:0)

我个人只会将代码标记更改为textarea标记。但是,如果您必须使用代码标记,则需要创建另一个伪文本区域,该代码标记隐藏并在检测到的代码标记正在修改时始终隐藏代码标记中的值,因为HTML页面无法传输没有JavaScript的帮助下代码标记值到服务器端(例如使用JavaScript首先获取代码标记值然后使用XMLHttpRequest在后台传输它,如果这样做,您可以保留HTML页面并动态更新它没有重新加载页面)。我将添加&#34; contenteditable&#34;属性为代码标记以使其可编辑。另外,请记住首先编辑代码标记,否则将没有值传输到服务器端。

HTML code:

<form name="submit_form" action="./uploads/submit.inc.php" method="POST">
    <div class="wrapper">
         <code id="ace-editorid" name="code_editor" contenteditable>Your Code Here</code>
         <textarea id="new_textarea" name="new_textarea" style="display: none;"></textarea>
    </div>
    <script>
        document.getElementById("ace-editorid").oninput = function() {
            document.getElementById("new_textarea").innerText = document.getElementById("ace-editorid").innerHTML;
        //update fake textarea every time there is a change in the code tag.
        }
    </script>
    <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
    <script src='http://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js'></script>
    <script  src="./css/script.js"></script>
    Type the Problem ID: <input type="text" name="probId">
    <input type="submit" name="b_code_submit" class="submit_query">
</form>

PHP代码:

<?php
    echo "Code tag value: <br>".$_POST["new_textarea"]."<br>";
    echo "Problem ID: ".$_POST["probId"];
?>