簡體   English   中英

CodeIgniter:我無法從html-> ajax-> php的textarea獲取值

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

看來我無法從ajax的php中獲取我的textarea的值。 當我嘗試像這樣從html到javascript獲取值時, var content = $('textarea[name=post_content]').val(); console.log(content); var content = $('textarea[name=post_content]').val(); console.log(content); ,它輸出textarea的值,但是當我使用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();
    }

在我的控制器中,我試圖通過使用錯誤狀態來查看我的textarea的值,因此它將輸出該值作為錯誤,以便我可以檢查是否獲得了textarea的值。 但是它沒有回報。 我也在用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');
        }
    }
});

然后在您的控制器中嘗試像這樣轉儲所有POST數據

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附加到formData,就像這樣,

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

我在這里找到我的解決方案:我如何獲得使用jQuery的ckeditor的內容

謝謝你的所有答復:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM