php - How to display uploaded picture immediately inside submition form (CodeIgniter)? -
i want create form users can add products database. have upload picture , choose couple of options select boxes. far have controller , view working fine - validate both upload , select boxes, set values select boxes if error accoured.
however, want users able see uploaded picture , manipulations if necessary (rotate, crop - pretty sure know how not question here) before submitting hole form.
the desired steps this:
- there button "upload picture" or picture gets uploaded automatically. (if there button have change validation settings upload)
- picture dynamically displayed user ( have ideas how can not test them because of missing first step)
- user manipulation
- if user exits before submitting delete uploaded picture (pretty sure can handle one)
here code far controller:
public function create() { $this->load->helper(array('form', 'url')); $this->load->library('form_validation'); $this->form_validation->set_rules('health', 'health', 'required'); $this->form_validation->set_rules('manufacturer', 'manufacturer', 'required'); if (empty($_files['product']['name'])) { $this->form_validation->set_rules('product', 'document', 'required'); } $data['manufacturer'] = $this->input->get('manufacturer'); if ($this->form_validation->run() === false) { $data['head'] = $this->load->view('templates/head', null, true); $data['navmenu'] = $this->load->view('templates/navmenu', null, true); $this->load->view('products/create', $data); } else { $user_id = $this->tank_auth->get_user_id(); $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $config['file_name'] = $user_id; $config['max_size'] = 100; $config['max_width'] = 1024; $config['max_height'] = 768; $this->load->library('upload', $config); if ( ! $this->upload->do_upload('product')) { $error = array('error' => $this->upload->display_errors()); $this->load->view('products/create', $error); } else { $this->products_model->set_products(); $id = $this->db->insert_id(); $image_data = $this->upload->data(); $image['image_url'] = $image_data['file_name']; $this->db->where ('product_id', $id); $this->db->update('products', $image); $this->load->view('products/success'); } } }
and view:
<html> <head> <?=$head?> </head> <body> <?=$navmenu?> <?php echo form_open_multipart('products/create'); ?> <input type="file" name="product" size="20" /> <?php echo form_error('product'); ?><br /> <br /><br /> <label for="health">health</label> <select name="health"> <option disabled selected value> -- select health option -- </option> <option value="new">new</option> <option value="used">used</option> </select> <?php echo form_error('health'); ?><br /> <label for="manufacturer">manufacturer</label> <select name="manufacturer" > <option disabled selected value> -- select manufacturer -- </option> <option value="manufacturer1" <?php echo set_select('manufacturer','manufacturer1', ( !empty($manufacturer) && $manufacturer == "manufacturer1" ? true : false )); ?> >manufacturer1</option> <option value="manufacturer2" <?php echo set_select('manufacturer','manufacturer2', ( !empty($manufacturer) && $manufacturer == "manufacturer2" ? true : false )); ?> >manufacturer2</option> <option value="manufacturer3" <?php echo set_select('manufacturer','manufacturer3', ( !empty($manufacturer) && $manufacturer == "manufacturer3" ? true : false )); ?> >manufacturer3</option> <option value="manufacturer4" <?php echo set_select('manufacturer','manufacturer4', ( !empty($manufacturer) && $manufacturer == "manufacturer4" ? true : false )); ?> >manufacturer4</option> </select> <?php echo form_error('manufacturer'); ?><br /> <input type="submit" name="submit" value="create product item" /> </form>
sorry if code unclean in places - it`s in development proceses. have tried split form 2 lost in controllers code. general idea, pseidocode - me on right track great!
use jquery this
function showimage(input) { if (input.files && input.files[0]) { var reader = new filereader(); reader.onload = function (e) { $('#imgpreview').attr('src', e.target.result); } reader.readasdataurl(input.files[0]); } } $("#imgupload").change(function(){ showimage(this); });
html
<form> <input type='file' id="imgupload" /> <img id="imgpreview" src="#" alt="your image" /> </form>
Comments
Post a Comment