a_simple_traveler
a_simple_traveler

Reputation: 11

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. When I try to get the value from html to javascript like so, 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.

Here is my html code:

<?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:

$('#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. But its returning no value. I am also using ckeditor. The problem started when I used the text editor.

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

Thanks for the help.

Upvotes: 1

Views: 1008

Answers (3)

a_simple_traveler
a_simple_traveler

Reputation: 11

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,

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

Thank you for all the replies :)

Upvotes: 0

slon
slon

Reputation: 1030

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 e.g.

post_content=post_content&cat=catValue

I hope this will help

Upvotes: 0

Roby Firnando Yusuf
Roby Firnando Yusuf

Reputation: 449

try with 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

$('#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

var_dump($_POST);
die();

Upvotes: 3

Related Questions