1

当从 div html 提交值时,我在 codeigneter 中使用表单时遇到问题,

示例视图

<label for="title">Title:</label>
<input type="text" class="form_field" id="title" name="title" size="30" value="<?php echo set_value('title', isset($default['title']) ? $default['title'] : ''); ?>" />
  <div class="form_field" name="contentbox" id="contentbox" contenteditable="true"> 
   <?php echo set_value('contentbox', isset($default['contentbox']) ? $default['contentbox'] : ''); ?></div>

在 div id="contentbox" 中设置 contenteditable="true",我将获得动态值,

控制器

....

$data = array( 'title' => $this->input->post('title'),
           'content' => $this->input->post('contentbox'));
$this->my_model->add($data);

....

但是在控制器中我无法获得值 div id="contentbox" ,我在输入数据库时​​遇到问题,当我在 div id="contentbox" 中键入“sometext”时,值始终为“0”

4

1 回答 1

1

您可以使用 jquery cookie 插件来获取 CI csrf 令牌并将其包含在此函数之前

jQuery

 (function($){

        var do_ajax = function(data, scope){

            // depending on your version of CI csrf token name will differ
            // newer versions of CI use the name in your application/config file

            data = {content : data, csrf_token : $.cookie('csrf_token')}

            $.ajax({
                url : BASE_PATH + 'some_class/some_method',
                data: data,
                context : scope,
                type : 'POST',
                dataType : 'json',
                success : function(callback){
                   //check the status first!

                   var $this = $(this);
                   $this.find(".msg").fadeIn();
                   $this.find("#contentbox").html(data);
                }
            });
        }

        var contents = {
            init: function(){
                if($("#editme"))
                   this.contentEditable();
            },
            contentEditable : function(){
                var scope    = $("#editme"),
                    eicon    = $(".icon-editable").hide(),
                    emessage = $(".msg").hide(),
                    econtent = $("#contentbox");

            econtent.hover(function(){
               eicon.show();
            }, function(){
               eicon.hide();
            });

            econtent.blur(function(){
               do_ajax($(this).html(), scope);
            });

            }        
        }

        $(function(){
            contents.init();
        });

    })(jQuery);

HTML

<div id="editme" class="editme">
    <span class="icon-editable">✎&lt;/span>
    <span class="msg">Content updated!</span>
    <h4>Some relevant title</h4>
    <div id="contentbox" class="contentbox" contenteditable="true">
        This is editable...
    </div>
</div>

CSS

div.editme{
    position:relative;
    background:#ffffff;
    padding:30px 8px 8px 8px;
}
span.icon-editable, span.msg{
    position:absolute;
    top:0;
}
span.icon-editable{
    left:0;
    border:1px solid #d1d1d1;
    border-top:0;
    border-left:0;
    font-size:1em;
    line-height:1.2em;
}
span.msg{
    right:0;
    font-size:0.8em;
    line-height:1.2em;
    color:#fafafa;
    background:green;
    padding:3px;
}

PHP

class some_class extends Controller{

    public function some_method(){
        if($this->input->is_ajax_request())
        {
           $data = array(
               'content'  => $this->input->post('content')
           );

           if($this->my_model->add($data))
           {
              $responce = array(
                  'status' => 'ok'
              );
           }
           else
           {
              $responce = array(
                  'status' => 'notok'
              );
           }
           echo json_encode($responce);  
        }
        else
        {
           show_404();
        }
    }
}

没有ajax请求 的测试版本点击这里

于 2012-03-22T10:55:53.480 回答