CodeIgniter:我无法从 html -> ajax -> php 的文本区域中获取值



看来我无法从Ajax中获得我的php中的文本中心的值。当我尝试将值从html到javaScript之类的 var content = $('textarea[name=post_content]').val(); console.log(content);像javascript一样,它会输出我的文本方面的值,但是当我使用ajax将其传递给控制器时,它不会输出。

这是我的HTML代码:

<?php $attributes = array('id' => 'create_post_form'); ?>
        <?php echo form_open_multipart('', $attributes); ?>
            <label>Title:</label>
            <input type="text" name="title" placeholder="Enter Title" class="form-control" required>
            <label>Category:</label>
            <select name="category" class="form-control" required>
                <option value="">--------</option>
                <?php foreach ($categories as $category): ?>
                    <option value="<?php echo $category['id']; ?>"><?php echo $category['name']; ?></option>
                <?php endforeach ?>
            </select>
            <label>Upload Image:</label>
            <input type="file" name="userfile" placeholder="Upload Image" class="form-control">
            <label>Post Content:</label>
            <textarea class="form-control" rows="15" name="post_content" placeholder="Enter Content of Post" required></textarea>
            <button type="submit" class="btn btn-secondary submit-button form-control">Save</button>
        <?php echo form_close(); ?>

这是我的ajax:

$('#create_post_form').submit(function(e) {
e.preventDefault();
$.ajax({
    data : new FormData(this),
    method : 'post',
    dataType : 'json',
    url : base_url + 'posts/create',
    async : false,
    cache : false,
    contentType : false,
    processData : false,
    success : function(data) {
        if(data['status'] == 'success')
        {
            $('#main_container').load(data['redirect_url']);
        }
        else
        {
            $('.error_container').html(data['message']);
            $('.error_container').addClass('error-border');
        }
    }
});

});

这是我的控制器:

public function create()
    {
        $result['status'] = 'error';
        $result['message'] = $this->input->post('post_content');
        $this->output->set_content_type('application/json');
        $this->output->set_output(json_encode($result));
        $string = $this->output->get_output();
        echo $string;
        exit();
    }

在我的控制器中,我试图通过使用错误状态来查看文本方面的值,以便将值作为错误输出,以便我可以检查是否获得了文本方面的值。但是它没有任何价值。我也在使用ckeditor。当我使用文本编辑器时,问题始于。

<script type="text/javascript">
CKEDITOR.replace( 'post_content' );

感谢您的帮助。

尝试使用serialize()

$('#create_post_form').submit(function(e) {
e.preventDefault();
$.ajax({
    data :  $('#create_post_form').serialize(),
    method : 'post',
    dataType : 'json',
    url : base_url + 'posts/create',
    async : false,
    cache : false,
    contentType : false,
    processData : false,
    success : function(data) {
        if(data['status'] == 'success')
        {
            $('#main_container').load(data['redirect_url']);
        }
        else
        {
            $('.error_container').html(data['message']);
            $('.error_container').addClass('error-border');
        }
    }
});
});

如果那是行不通的,请尝试使用1乘1个帖子数据

$('#create_post_form').submit(function(e) {
e.preventDefault();
$.ajax({
    data : {
      post_content : $(":input[name='post_content']").val(),
      cat : $("select[name='category']").val(),
      /* and etc post the data what u need*/
},
    method : 'post',
    dataType : 'json',
    url : base_url + 'posts/create',
    async : false,
    cache : false,
    contentType : false,
    processData : false,
    success : function(data) {
        if(data['status'] == 'success')
        {
            $('#main_container').load(data['redirect_url']);
        }
        else
        {
            $('.error_container').html(data['message']);
            $('.error_container').addClass('error-border');
        }
    }
});

然后在您的控制器中尝试将所有发布数据丢弃,例如

var_dump($_POST);
die();

首先删除这两个:

contentType : false,
processData : false,

使您传递参数如下:

var dataObj = {
      post_content : $(":input[name='post_content']").val(),
      cat : $("select[name='category']").val()
};

并将其添加为:

data: jQuery.param(dataObj), 
contentType: "application/x-www-form-urlencoded; charset=utf-8",

您的传递参数将就像例如

post_content=post_content&cat=catValue

我希望这会有所帮助

我找到了解决问题的解决方案。我使用ckeditor getData获取Textarea的价值,然后将其应用于post_content2的形式,

var content = CKEDITOR.instances['post_content'].getData();
var formData = new FormData(this);
formData.append('post_content2', content);

我在这里找到了我的解决方案:ckeditor-using-unding-jquery

谢谢您的所有答复:)

最新更新