CodeIgniter:我无法从 html -> ajax -> php 的文本区域获取值
CodeIgniter: I can't get the value from a textarea from html -> ajax -> php
我似乎无法从 ajax 中获取我在 php 中的文本区域的值。当我尝试像这样从 html 获取 javascript 的值时,var content = $('textarea[name=post_content]').val(); console.log(content);
,它输出我的文本区域的值,但是当我使用 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 post 数据
$('#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 来获取文本区域的值,并将其作为 post_content2 附加到 formData 中,
var content = CKEDITOR.instances['post_content'].getData();
var formData = new FormData(this);
formData.append('post_content2', content);
我在这里找到了我的解决方案:how-can-i-get-content-of-ckeditor-using-jquery
谢谢大家的回复:)
我似乎无法从 ajax 中获取我在 php 中的文本区域的值。当我尝试像这样从 html 获取 javascript 的值时,var content = $('textarea[name=post_content]').val(); console.log(content);
,它输出我的文本区域的值,但是当我使用 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 post 数据
$('#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 来获取文本区域的值,并将其作为 post_content2 附加到 formData 中,
var content = CKEDITOR.instances['post_content'].getData();
var formData = new FormData(this);
formData.append('post_content2', content);
我在这里找到了我的解决方案:how-can-i-get-content-of-ckeditor-using-jquery
谢谢大家的回复:)