douchuxun4162
2015-07-01 13:15
浏览 62
已采纳

Codeigniter - <textarea> </ textarea> POST更改内容

I have some problem with posting data in codeigniter. I want to make an editor for .php files and when i want to post a string like this

<?php echo $my_var; ?><p>sample text</p>

the obtained result in

$_POST['element_name']

is

<!--<?php echo $my_var; ?>--><p>sample text</p>

Why happen this? How i can obtain the original text?

Here is part of my code

HTML

<form role="form" accept-charset="utf-8" method="post" action="">
    <textarea id="editor_original"><?php echo $editor_original;?></textarea>
    <textarea name="editor" id="editor"></textarea>
    <input type="submit" value="Save" />
</form>

JAVASCRIPT

    var editor = CodeMirror.fromTextArea(document.getElementById('editor_original'), {
        lineNumbers: true,
        extraKeys: {"Ctrl-Space": "autocomplete"},
        keyMap: "sublime",
        autoCloseBrackets: true,
        matchBrackets: true,
        mode: "application/x-httpd-php",
        showCursorWhenSelecting: true,
        theme: "monokai",
        onBlur: function () { 
             editor.save();             
        }

      });
   $("#editor").val(editor.getValue());

PHP

$data = $this->page_m->array_from_post(array('editor'));
echo $data; // here output is <!--<?php echo $my_var; ?>--><p>sample text</p>


public function array_from_post($fields)
{
    $data = array();
    foreach($fields as $field)
    {
        $data[$field] = $this->input->post($field);
    }
    return $data;
}

图片转代码服务由CSDN问答提供 功能建议

我在codeigniter中发布数据时遇到了一些问题。 我想为.php文件创建一个编辑器,当我 想要发布这样的字符串

&lt;?php echo $ my_var; ?&gt;&lt; p&gt;示例文本&lt; / p&gt;

获取的结果

$ _ POST [' element_name']

&lt;! - &lt;?php echo $ my_var; ?&gt; - &gt;&lt; p&gt;示例文字&lt; / p&gt;

为什么会这样? 如何获取原始文本?

以下是我的代码的一部分

HTML

 &lt; form role =“form”accept-charset =“utf-8”method =“post”action =“”&gt; 
&lt; textarea id =“editor_original”&gt;&lt;  ?php echo $ editor_original;?&gt;&lt; / textarea&gt; 
&lt; textarea name =“editor”id =“editor”&gt;&lt; / textarea&gt; 
&lt; input type =“submit”value =“Save  “/&gt; 
&lt; / form&gt; 
   
 
 

JAVASCRIPT

  var  editor = CodeMirror.fromTextArea(document.getElementById('editor_original'),{
 lineNumbers:true,
 extraKeys:{“Ctrl-Space”:“autocomplete”},
 keyMap:“sublime”,
 autoCloseBrackets:  true,
 matchBrackets:true,
 mode:“application / x-httpd-php”,
 showCursorWhenSelecting:true,
 theme:“monokai”,
 onBlur:function(){
 editor.save(  ); 
} 
 
}); 
 $(“#editor”)。val(editor.getValue()); 
   
 
 

PHP

  $ data = $ this-&gt; page_m-&gt; array_from_post(array('editor')  )); 
echo $ data;  //这里的输出是&lt;! - &lt;?php echo $ my_var;  ?&gt;  - &gt;&lt; p&gt;示例文本&lt; / p&gt; 
 
 
公共函数array_from_post($ fields)
 {
 $ data = array(); 
 foreach($ fields as $ field  )
 {
 $ data [$ field] = $ this-&gt; input-&gt; post($ field); 
} 
返回$ data; 
} 
    
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongsong73032 2015-07-01 14:17
    已采纳

    You get the right data, but, because of echo , the output is modified. Use echo htmlentities($this->input->post('editor')); to see the posted content.

    If you want to save the content, use something like this:

    write_file('out_file.php', stripslashes($this->input->post('editor')));
    

    whre write_file is

    function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE)
        {
            if ( ! $fp = @fopen($path, $mode))
            {
                return FALSE;
            }
    
            flock($fp, LOCK_EX);
            fwrite($fp, $data);
            flock($fp, LOCK_UN);
            fclose($fp);
    
            return TRUE;
        }
    

    I hope this will help you

    点赞 评论

相关推荐 更多相似问题