开发者

struggling with a ajax upload and PHP

I need some help please,

I am upload loading an image with the help of a jquery uploader so that the user can get a preview of the image and I can jCrop on it, my problem is that I then need to save the file name, but I cannot get to the variable I create in the upload function,

The process that users goes throug is as follows,

  1. User fills in their details
  2. User selects and image to up load and hits the upload button
  3. Upload function is run, and the image is returned to the view
  4. User selects an area of the image that will be cropped and saved
  5. User carries on filling form out
  6. Save the items including the filename

Below is my PHP code, which I believe is where the variable goes missing and cannot be used in the add function,

Add() - The function that the form submits too

public function add()
{
    $this->form_validation->set_rules('title','title', 'required|trim|min_length[2]');
    $this->form_validation->set_rules('firstname', 'firstname', 'required|trim|alpha');
    $this->form_validation->set_rules('surname', 'surname', 'required|trim|alpha');
    $this->form_validation->set_rules('email', 'email address', 'required|trim|valid_email');
    $this->form_validation->set_rules('postcode', 'postcode', 'required|trim|min_length[6]|max_length[9]');
    $this->form_validation->set_rules('company_name', 'company_name', 'required|trim');
    $this->form_validation->set_rules('company_summary', 'company_summary', 'required|trim|max_length[3000]');
    $this->form_validation->set_rules('alternative_ads', 'alternative ads', 'required|trim|prep_url');
    $this->form_validation->set_rules('facebook_url', 'Facebook URL', 'required|trim|prep_url');
    $this->form_validation->set_rules('twitter_url', 'Twitter URL', 'required|trim|prep_url');

    if($this->form_validation->run() == FALSE)
    {
        $this->template->build('admin/users/add');
    }
    else
    {
        //group the post data together soe that we can save the data easily.
        $user = array(
            'firstname' => $this->input->post('firstname'),
            'surname' => $this->input->post('surname'),
            'email' => $this->input->post('email'),
            'postcode' => $this->input->post('postcode'),
            'date_registered' => date("d-m-y h:i:s", time())
        );

        if(!$this->users_model->insert($this->input->xss_clean($user)))
        {
            $data['error'] = "We could not save you details, please try again";
            $this->template->build('/users/admin/add', $data);
        }

        $employer = array(
            'company_name' => $this->input->post('company_name'),
            'company_summary' => $this->input->post('company_summary'),
            'logo' => $this->file['file_name'],
            'alternative_ads' => $this->input->post('alternative_ads'),
            'facebook_url' => $this->input->post('facebook_url'),
            'twitter_url' => $this->input->post('twitter_url'),
            'user_id' => $this->db->insert_id()
        );

        if(!$this->employer_model->insert($this->input->xss_clean($employer)))
        {
            $data['error'] = "We could not save you details, please try again";
            $this->template->build('/users/admin/add', $data);              
        }
        else
        {
            die(print_r($this->file));
            $this->load->library('image_lib');

            $config['image_library'] = 'gd2';
            $config['source_image'] = '/media/uploads/users/' . $this->file['file_name'];
            $config['thumb_marker'] = TRUE;
            $config['x_axis'] = $this->input->post('x');
            $config['y_axis'] = $this->input->post('y');

            $this->image_lib->initialize($config);

            if ( ! $this->image_lib->crop())
            {
                $data['error'] = "We could not crop you image, please try again. If the problem persists please contact 开发者_如何学运维us";
                $this->template->build('/admin/users/add', $data);
            }

            $this->session->set_flashdata('success', 'You have successfully added an employer');
            redirect('/admin/users/manage');
        }
    }
}

And the upload function that the jquery uploader calls

private function upload()
{
    $config['upload_path'] = "./media/uploads/users";
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']    = '1000';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['encrypt_name']  = TRUE;

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


    if ( ! $this->upload->do_upload('userfile'))
    {
        $error = array('error' => $this->upload->display_errors());
        die(print_r($error));
    }
    else
    {
        $this->file = $this->upload->data();
        $msg = $this->file;
        echo json_encode($msg);
    }
}

To recap I am setting $this->file in upload() and trying to use it in the add() function.


You'll have to return something from the upload() function in order to use it in the add() function. Depending on your app and it's set up this may or may not break your ajax.

An alternative would be to set session, for the bits you need.


$this->file will only be in the scope of the upload. So like Ross mentioned have the upload echo the image in the json and then do something with it in the form. Like add it to a hidden input that is then sent with the post data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜