【发布时间】:2015-10-21 06:16:20
【问题描述】:
我正在使用 codeigniter,但是当我使用 ajax 提交表单时,我无法将字段数据从视图获取到控制器。从过去的两天开始,我正在研究它,但找不到解决方案。当我打印字段值时,它显示为空白,即没有值。 我只想用ajax提交数据,没有正常发帖。请帮助解决我的问题。
这是我的代码:
查看
$(document.body).on('click', '.postbtn' ,function(e){
e.preventDefault();
$('#posting_comment').submit();
});
<script type="text/javascript">
function sendCareerData()
{
var fdata = new FormData(document.getElementById("posting_comment"));
jQuery.ajax({
type: "POST",
url: "<?php echo base_url();?>"+"dashboard/do_upload",
data: fdata,
mimeType:"multipart/form-data",
contentType: 'text',
cache: false,
processData:false,
dataType: 'html',
success: function (data) {
alert("d"+data);
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log( errorThrown );
}
});
return false;
}
</script>
<form name="posting_comment" id="posting_comment" method="post" enctype="multipart/form-data" onsubmit="return sendCareerData()">
<input name="comment_topic" id="comment" type="text" class=" postinputox" placeholder="Enter Topic..."/>
<input id="file_upload" name="attachment_file" class="file_upload_icon" type="file"/>
<input type="button" class="post postbtn" style="border: none;outline:none;" value="Post"/>
</form>
控制器:
public function do_upload()
{
$comment_topic=$_POST['comment_topic'];
$attachment_file=$_POST['attachment_file'];
$config['upload_path'] ='./uploads/';
$config['allowed_types'] = 'gif|jpg|png||jpeg';
$config['max_width'] = 1000;
$config['max_height'] = 1000;
$config['max_size'] = 20000000;
$config['encrypt_name'] = FALSE;
$this->load->library('upload', $config);
$this->upload->do_upload($file_names);
}
$comment_topic 和 $attachment_file 都包含空白值。
【问题讨论】:
-
var_dump($_POST)的输出是什么? -
@Martin 我已经检查过这是 $_POST 的输出:- 'Array()1'
-
@Martin 它是空数组
标签: php jquery ajax codeigniter codeigniter-2