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();
        }
    }

コントローラーがアップロードされているコードイグナイターでajaxを使用して最初のフォームから画像をアップロードしました。ここでは、このコントローラーの画像のみをアップロードしますが、レジスターテーブルにimage_name、name、city、およびmonbileを挿入する登録コントローラーを送信するために画像名を希望します。

私を助けてください、

どうすればうまくいくでしょうか

4

2 に答える 2

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 に答える
0

ajaxformの投稿は通常のファイルアップロードはできないと思います。Formdata() オブジェクトを使用する必要があります。確認してください How to send FormData objects with Ajax-requests in jQuery?

于 2014-03-11T11:48:05.730 に答える