Javascript 代码点火器:我能';t从html中的文本区域获取值->;ajax->;php
似乎我无法从ajax中获得php中textarea的值。当我尝试将值从html转换为javascript时,就像这样,Javascript 代码点火器:我能';t从html中的文本区域获取值->;ajax->;php,javascript,php,html,ajax,ckeditor,Javascript,Php,Html,Ajax,Ckeditor,似乎我无法从ajax中获得php中textarea的值。当我尝试将值从html转换为javascript时,就像这样,var content=$('textarea[name=post_content]').val();控制台日志(内容),它输出我的textarea的值,但当我使用ajax将其传递给控制器时,它什么也不输出 以下是我的html代码: <?php $attributes = array('id' => 'create_post_form'); ?>
var content=$('textarea[name=post_content]').val();控制台日志(内容)代码>,它输出我的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(); ?>
}))
这是我的控制器:
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' );
CKEDITOR.replace('post_content');
谢谢您的帮助。尝试使用serialize()
如果这不起作用,请尝试逐个发布数据
$('#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
我希望这对我找到解决问题的办法有所帮助。我曾经获取textarea的值,然后将其作为post_content2应用到formData中,就像这样
var content = CKEDITOR.instances['post_content'].getData();
var formData = new FormData(this);
formData.append('post_content2', content);
我在这里找到了解决方案:
感谢您的回复:)很抱歉,我们在这次长途旅行中没有得到足够的信息。我的建议是,首先尝试从每个断点进行日志记录,这样您就可以知道在哪一行丢失了内容
post_content=post_content&cat=catValue
var content = CKEDITOR.instances['post_content'].getData();
var formData = new FormData(this);
formData.append('post_content2', content);