简体   繁体   English

CodeIgniter:我无法从html-> ajax-> php的textarea获取值

[英]CodeIgniter: I can't get the value from a textarea from html -> ajax -> php

it seems I can't get the value of my textarea in php from ajax. 看来我无法从ajax的php中获取我的textarea的值。 When I try to get the value from html to javascript like so, var content = $('textarea[name=post_content]').val(); console.log(content); 当我尝试像这样从html到javascript获取值时, var content = $('textarea[name=post_content]').val(); console.log(content); var content = $('textarea[name=post_content]').val(); console.log(content); , it outputs the value of my textarea, but when I pass it to my controller using ajax, it outputs nothing. ,它输出textarea的值,但是当我使用ajax将其传递给控制器​​时,它什么也不输出。

Here is my html code: 这是我的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(); ?>

this is my ajax: 这是我的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');
        }
    }
});

}); });

and this is my controller: 这是我的控制器:

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();
    }

In my controller, I am trying to see the value of my textarea by using the error status so it would output the value as an error so i could check if I am getting the value of the textarea. 在我的控制器中,我试图通过使用错误状态来查看我的textarea的值,因此它将输出该值作为错误,以便我可以检查是否获得了textarea的值。 But its returning no value. 但是它没有回报。 I am also using ckeditor. 我也在用ckeditor。 The problem started when I used the text editor. 当我使用文本编辑器时,问题开始了。

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

Thanks for the help. 谢谢您的帮助。

try with serialize() 尝试使用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');
        }
    }
});
});

if that's doesn't work try to 1 by 1 post data 如果不行,请尝试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');
        }
    }
});

and then in your controller try to dump all POST data like this 然后在您的控制器中尝试像这样转储所有POST数据

var_dump($_POST);
die();

First remove these two: 首先删除这两个:

contentType : false,
processData : false,

Make you passing parameters like below: 让您传递如下参数:

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

and add it like: 并添加如下:

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

your passing parameters will be like eg 您传递的参数将类似于

post_content=post_content&cat=catValue

I hope this will help 我希望这个能帮上忙

I found a solution to my problem. 我找到了解决问题的方法。 I used CKEditor GetData to get the value of the textarea and I apppended it to formData as post_content2 like so, 我使用CKEditor GetData来获取textarea的值,并将其作为post_content2附加到formData,就像这样,

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

I found my solution here: how-can-i-get-content-of-ckeditor-using-jquery 我在这里找到我的解决方案:我如何获得使用jQuery的ckeditor的内容

Thank you for all the replies :) 谢谢你的所有答复:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM