1

View code

<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>

controller code

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

I have upload image from first form using ajax in codeigniter which controller is upload,here will be upload only image this controller but i would like to image name to send register controller which i will insert image_name, name, city and monbile in resister table.

Please help me,

How can i do it successfully

MD. ABDUL Halim
  • 712
  • 3
  • 11
  • 32
  • register controller or method? – Bhuvan Rikka Dec 08 '12 at 07:14
  • 1
    I think you are using register method... Why using two functions? Just keep the register code in the upload function – Bhuvan Rikka Dec 08 '12 at 07:21
  • I have upload image using Ajax and have done display instantly, so i can't use only resister controller because same time image can't display and insert data to MySQL database using only resister controller. At first, I want to upload image and instantly display ,then i want to fill input field and to submit. Now all data will be insert and also image_name in resister table. Please will you give me any suggestion . – MD. ABDUL Halim Dec 08 '12 at 08:27

2 Answers2

0

Just call the register function from upload function and send the image name.

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);
    }
}
Bhuvan Rikka
  • 2,683
  • 1
  • 17
  • 27
  • Please see my code again(i have edit some), In first form,After upload image using Ajax, in second form when fill input field and click submit button then it go to do hit to register controller. Now, how i will send image name upload controller to resister controller and i will insert all data with image name in resister table. according to You suggestion, if i use your code ,then it is seen below Severity: Warning Message: Missing argument 1 for Setup_con::resister() Filename: controllers/setup_con.php Line Number: 257 – MD. ABDUL Halim Dec 08 '12 at 09:04
  • 1) Open the image upload form 2) Get the image into upload controller 3) Call register function from upload function and send the image 4) Now you are in register function with the image.Call the second form now and get the data.. Now you'll have both image and form data – Bhuvan Rikka Dec 08 '12 at 09:08
0

I think normal file uploading is not possible for the ajaxform submission. You must use Formdata() Object please check How to send FormData objects with Ajax-requests in jQuery?

Community
  • 1
  • 1