gpt4 book ai didi

php - 使用ajax使用codeigniter表单上传图像

转载 作者:行者123 更新时间:2023-12-01 07:55:07 25 4
gpt4 key购买 nike

我需要提交带有表单数据的图像,我已经为此搜索了更好的方法,但不幸的是我找不到更好的方法来实现我的目的,之前使用 this.serialize() 方法完成。但它不适用于图像

这是我的代码

查看

 <?php
$this->load->helper('form');
$attributes = array('method'=>'post','name'=>'create_company','id'=>'create_company');
echo form_open_multipart('',$attributes);?>
<label>Code : </label> <?php echo form_input('code');?><br/><br/>
<label>Name : </label> <?php echo form_input('name');?><br/><br/>
<label>Logo : </label><input type="file" name="userfile"/><br/><br/>
<label>URL : </label> <?php echo form_input('url');?><br/><br/>
<label>Description : </label> <textarea name="description" rows="4" cols="50"> </textarea><br/><br/>
<input type="submit" name="submit" value="Save"/>
<?php echo form_close(); ?>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>//no need to specify the language
$(document).ready(function() {

$('#create_company').on("submit",function(e) {

e.preventDefault();

$.ajax({
type: "POST",
url: "<?php echo site_url('site/upload'); ?>",
data: $(this).serialize(),
success: function(data){
var site_url = "<?php echo site_url('site/create_branch_form'); ?>";
var json = $.parseJSON(data);
site_url = site_url +"/" + json.results[0].id ;
alert(site_url);
$("#content").load(site_url);
alert(data);
}
});
});
});
</script>

Controller

public function upload(){

//insert company details
$this->load->model('company_model');
$data['results'] = $this->company_model->insert_company();
$this->output->set_output(json_encode($data));
}

型号

function insert_company(){
//user details
$username = $this->session->userdata('username');
//$query = $this->db->get_where('userdetails', array('username' => $username));

$query = $this->db->get_where('userdetails',array('username'=>$username));

foreach ($query->result() as $function_info)
{
$this->userid = $function_info->id;
}

$new_company_insert_data = array(

'code' => $this->input->post('code'),
'name' => $this->input->post('name'),
'logo' => $imgpath['file_name'],
'url' => $this->input->post('url'),
'description' => $this->input->post('description'),
'createdat' => date('Y-m-d H:i:s',strtotime('')),
'status' => 0,
'userid' => $this->userid
);

$this->db->insert('companydetails',$new_company_insert_data);
//get id of the last inserted row
$this->load->database();
$query = $this->db->query("SELECT MAX(id) AS id FROM companydetails");
return $query->result();
}

最佳答案

尝试使用 FormData 对象。你的 JS 代码中应该有这样的内容:

 $(document).ready(function() {

$('#create_company').on("submit",function(e) {

e.preventDefault();
var formData = new FormData(this);

$.ajax({
type: "POST",
url: "<?php echo site_url('site/upload'); ?>",
data: formData,
cache: false,
contentType: false,
processData: false
success: function(data){
//function success
},
error: function(data){
//error function
}
});
});
});

关于php - 使用ajax使用codeigniter表单上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25242093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com