从codeigniter </div>中的<div> html值提交表单

时间:2012-03-21 09:26:56

标签: codeigniter codeigniter-form-helper

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

示例视图

<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”,我将获得dinamic值,

控制器

....

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

1 个答案:

答案 0 :(得分:0)

您可以使用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">✎</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请求 CLICK HERE