1

查看代码

<form id="form1" method="post" enctype="multipart/form-data" action='<?php echo base_url();?>index.php/setup_con/upload'>

<input type="file" name="userfile" id="userfile" required="required"/>
</form>

<form id="form2" method="post"  enctype="multipart/form-data" action='<?php echo base_url();?>index.php/setup_con/register'>
<input type="text" name="name" id="name"/> 
<input type="text" name="city" id="city"/> 
<input type="text" name="mobile" id="mobile"/> 
<input type="submit" value="Submit" />
</form>

控制器代码

function upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '800';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload())
    {
        echo "File is Not valid/File does not select";
        $error = array('error' => $this->upload->display_errors());

    }
    else
    {

        $data = array('upload_data' => $this->upload->data());

        $img_name =  $data ['upload_data']['file_name'];

            echo "'<img src='".base_url()."uploads/".$img_name."' class='preview'>";

    }
}
    function register()
    {
        $this->form_validation->set_rules('name','Name', 'trim|required');
        $this->form_validation->set_rules('city','city', 'trim|required');
        $this->form_validation->set_rules('mobile','mobile', 'trim|required');
        if($this->form_validation->run() == FALSE)
    {
        }
        else
        {
           $data = $this->register_model->register();
        }
    }

我已经在codeigniter中使用ajax从第一个表单上传图像,控制器正在上传,这里将只上传图像这个控制器,但我想图像名称发送寄存器控制器,我将在电阻表中插入图像名称、名称、城市和monbile。

请帮我,

我怎样才能成功地做到这一点

4

2 回答 2

0

我认为 ajaxform 提交无法正常上传文件。您必须使用 Formdata() 对象,请检查 如何在 jQuery 中使用 Ajax 请求发送 FormData 对象?

于 2014-03-11T11:48:05.730 回答
0

只需从上传函数调用注册函数并发送图像名称。

function upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '800';
$config['max_width']  = '1024';
$config['max_height']  = '768';

$this->load->library('upload', $config);

if (!$this->upload->do_upload())
{
    echo "File is Not valid/File does not select";
    $error = array('error' => $this->upload->display_errors());

}
else
{

    $data = array('upload_data' => $this->upload->data());

    $img_name =  $data ['upload_data']['file_name'];

        echo "'<img src='".base_url()."uploads/".$img_name."' class='preview'>";
        //Send image name to register function
        register($img_name);

}
}
function register($img_name)
{
    $this->form_validation->set_rules('name','Name', 'trim|required');
    $this->form_validation->set_rules('city','city', 'trim|required');
    $this->form_validation->set_rules('mobile','mobile', 'trim|required');
    if($this->form_validation->run() == FALSE)
{
    }
    else
    {
       // Send image name and form data to the model
       $data = $this->register_model->register($img_name,$form_data);
    }
}
于 2012-12-08T08:34:39.050 回答