How can i make a registration form where i upload an image for my profile picture, and before clicking register, there's a preview of the image that i want to set as the profile picture using CI??
here's my registration form code
register_model.php
<?php
class Register_model extends CI_Model
{
function create_user($photo_name)
{
$new_user_insert_data = array(
'id' => '',
'email' => $this->input->post('email'),
'password' => md5($this->input->post('password')),
'name' => $this->input->post('name'),
'major' => $this->input->post('user_major'),
'year' => $this->input->post('user_year'),
'bio' => $this->input->post('bio'),
'profpic' => $photo_name,
'insta' => $this->input->post('instagram'),
'twitter' => $this->input->post('twitter'),
'facebook' => $this->input->post('facebook'),
'vote_count' => '10',
'vote' => '0',
'gender' => $this->input->post('gender'),
);
$insert = $this->db->insert('user', $new_user_insert_data);
return $insert;
}
}
?>
Here's the view
register.php
<div class="margin-top-87">
<div class="container">
<h1>Register</h1>
<hr>
<div class="row">
<!-- left column -->
<div class="col-md-3">
<div class="text-center">
<img src="//placehold.it/100" class="avatar img-circle" alt="avatar">
<?php
if(!empty($_FILES['userfile'])){
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key => $value)
for ($s=0; $s<=$count-1; $s++)
{
//Original Image Upload - Start
$_FILES['userfile']['name'] = $value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = './public/images/campaign-images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = '10000';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$CI->load->library('upload', $config);
$CI->upload->do_upload();
$data = $CI->upload->data();
//Original Image Upload - End
//Thumbnail Image Upload - Start
$config['image_library'] = 'gd2';
$config['source_image'] = './public/images/campaign-images/'. $value['name'][$s];
$config['new_image'] = './public/images/campaign-images/thumbs/'.$value['name'][$s];
$config['width'] = 350;
$config['height'] = 250;
//load resize library
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//Thumbnail Image Upload - End
$name_array[] = $data['file_name'];
}
return $name_array;
}
?>
<h6>Please use real photo, not an avatar</h6>
<?php
$data= array
(
'name' => 'userfile',
'type' => 'file',
'class' => 'form-control',
'id' => 'form_register'
);
echo form_upload($data);
?>
</div>
</div>
<!-- edit form column -->
<div class="col-md-9 personal-info">
<div class="alert alert-info fade in">
<strong>Info!</strong> Please fill with real information of yours.
</div>
<?php
if(validation_errors('<p class="error">'!=""))
{?>
<div class="alert alert-danger fade in">
×
<strong>Attention!</strong> <?php echo validation_errors('<p class="error">') ?>
</div><?php
}?>
<h3>Personal info</h3>
<?php $attributes = array('class' => 'form-horizontal', 'role' => 'form', 'id' => 'form_register'); echo form_open('index/create_user',$attributes); ?>
<div class="form-group">
<label class="col-lg-3 control-label">UC Student email :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'email',
'placeholder' => 'Ciputra student email. Example : email#student.ciputra.ac.id',
'type' => 'email',
'class' => 'form-control',
'required' => 'required'
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Name :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'name',
'placeholder' => 'Full name. Example : Front Middle Last',
'type' => 'text',
'class' => 'form-control',
'required' => 'required'
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Gender :</label>
<div class="col-lg-8">
<div class="ui-select">
<?php
$options = array
(
'' => 'Gender',
'Man' => 'Man',
'Woman' => 'Woman'
);
echo form_dropdown('gender', $options, '', 'class="form-control"');
?>
</div>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Major :</label>
<div class="col-lg-8">
<div class="ui-select">
<?php
$options = array
(
'' => 'Insert your major',
'CB' => 'CB',
'FDB' => 'FDB',
'IBM-IC' => 'IBM-IC',
'IBM-RC' => 'IBM-RC',
'IBA' => 'IBA',
'IHTB' => 'IHTB',
'IMT' => 'IMT',
'INA' => 'INA',
'MCM' => 'MCM',
'MIS' => 'MIS',
'PSY' => 'PSY',
'VCD' => 'VCD'
);
echo form_dropdown('user_major', $options, '', 'class="form-control"');
?>
</div>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Year :</label>
<div class="col-lg-8">
<div class="ui-select">
<?php
$options = array('' => 'Year joined UC');
for ($i = date('Y'); $i >= 2006; $i--)
{
$options[$i] = $i;
}
echo form_dropdown('user_year', $options, '', 'class="form-control"');
?>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Password :</label>
<div class="col-md-8">
<?php
$data= array
(
'name' => 'password',
'placeholder' => 'Password',
'type' => 'password',
'class' => 'form-control',
'required' => 'required',
'maxlength' => '15'
);
echo form_password($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Confirm password :</label>
<div class="col-md-8">
<?php
$data= array
(
'name' => 'cpassword',
'placeholder' => 'Confirm Password',
'type' => 'password',
'class' => 'form-control',
'required' => 'required',
'maxlength' => '15'
);
echo form_password($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Short Bio :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'bio',
'placeholder' => 'Short bio (max 128 character)',
'type' => 'text',
'class' => 'form-control',
'maxlength' => '128',
'rows' => '3'
);
echo form_textarea($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Facebook :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'facebook',
'placeholder' => 'Facebook Profile link. Example : facebook.com/name',
'type' => 'url',
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Twitter :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'twitter',
'placeholder' => 'Twitter Profile link. Example : #name',
'type' => 'url',
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Instagram :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'instagram',
'placeholder' => 'Instagram Profile link. Example : #name',
'type' => 'url',
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<?php
$data= array
(
'name' => 'register',
'value' => 'Register',
'type' => 'submit',
'class' => 'btn btn-primary',
);
echo form_submit($data);
?>
<span></span>
<?php
$data= array
(
'name' => 'reset',
'value' => 'Clear',
'type' => 'reset',
'class' => 'btn btn-default',
);
echo form_reset($data);
?>
</div>
</div>
</form>
<?php echo form_close(); ?>
</div>
</div>
</div>
<hr>
</div>
The "preview before uploading" thing is a javascript trick that loads the local file into the <img> source. The rest is simple file upload where you check the file types, size, resolution etc.
Here is your answer: Preview an image before it is uploaded
Related
Anyone pls help ...
This is my Controller code (Actions.php)
public function add_admin()
{
$data['employee'] = $this->employee_model->get_new();
$data['department'] = $this->employee_model->dpdown_admins();
$data['gender'] = $this->employee_model->dpdown_gender();
$data['status'] = $this->employee_model->dpdown_status();
$data['attainment'] = $this->employee_model->dpdown_attainment();
$rules = $this->employee_model->rules['insert'];
$this->form_validation->set_rules($rules);
if ($this->form_validation->run() == TRUE)
{
$post = $this->employee_model->array_from_post(array(
'ID_number',
'employee_no',
'department_id',
'department_position',
'tin_number',
'sss_number',
'ph_number',
'first_name',
'middle_name',
'last_name',
'suffix_name',
'street',
'barangay',
'city',
'zipcode',
'birthday',
'age',
'gender_id',
'nationality',
'religion',
'status_id',
'telephone_number',
'cellphone_number',
'email',
'educational_att_id',
'diploma_title',
'school_name',
'year_graduated',
'company1_name',
'position1_title',
'inclusive_dates1',
'company2_name',
'position2_title',
'inclusive_dates2',
'company3_name',
'position3_title',
'inclusive_dates3',
'date_employed'
));
$data = $this->employee_model->save($post);
return self::redirect_to('human_resource/admin', '<b>Success</b><br>New Admin Employee has been successfully Registered.');
}
$data['error'] = validation_errors('<li class="error">', '</li>');
$data['content'] = 'human_resource/actions/add_admin';
return $this->load->view('templates', $data);
}
and then this is how my model looks like (employee_model.php)
these are for my rules
public $rules = array('insert' => array(
'department_position' => array(
'field' => 'department_position',
'label' => 'Employee Position',
'rules' => 'trim|required',
'errors' => array('required' => 'This Field is Required')),
'ID_number' => array(
'field' => 'ID_number',
'label' => 'ID Number',
'rules' => 'trim|required',
'errors' => array('required' => 'This Field is Required')),
'employee_no' => array(
'field' => 'employee_no',
'label' => 'Employee Number',
'rules' => 'trim|required',
'errors' => array('required' => 'This Field is Required')),
'tin_number' => array(
'field' => 'tin_number',
'label' => 'Tin Number',
'rules' => 'trim|required',
'errors' => array('required' => 'This Field is Required')),
'sss_number' => array(
'field' => 'sss_number',
'label' => 'SSS Number',
'rules' => 'trim|required',
'errors' => array('required' => 'This Field is Required')),
'ph_number' => array(
'field' => 'ph_number',
'label' => 'Phil Health Number',
'rules' => 'trim|required',
'errors' => array('required' => 'This Field is Required')),));
then this is my save function still inside the same model
public function save($post, $employee_id = NULL)
{
if($this->timestamp == TRUE)
{
$now = date('Y-m-d H:i:s');
$employee_id || $post['created_at'] = $now;
$post['updated_at'] = $now;
}
if($employee_id === NULL)
{
!isset($post[$this->primary_key]) || $post[$this->primary_key] = NULL;
$this->db->insert($this->table_name, $post);
$employee_id = $this->db->insert_id();
}else{
$filter = $this->filter;
$this->db->where($this->primary_key, $filter($employee_id))
->update($this->table_name, $post);
}
}
then this is my view form
i did not put all in here just the 1/4 of my view code
<div class="main-content">
<div class="main-content-inner">
<div class="breadcrumbs ace-save-state" id="breadcrumbs">
<ul class="breadcrumb">
<li>
<i class="ace-icon fa fa-tachometer home-icon"></i>
Dashboard
</li>
<li>
<i class="ace-icon fa fa-user home-icon"></i>
Administration Table
</li>
<li>
New Admin Form
</li>
<li class="active">Application Form</li>
</ul>
</div>
<!-- /end of breadcrumb -->
<div class="page-content">
<div class="page-header">
<h1>
<i class="fa fa-user-plus"></i>
New Admin Form
<small>
<i class="ace-icon fa fa-angle-double-right"></i>
Application Form
</small>
</h1>
</div>
<!-- end of page header -->
<?php echo form_open(NULL, array('class' => 'form-horizontal', 'role' => 'form'));?>
<div class="row">
<div class="col-xs-12">
<!-- table content -->
<div class="widget-box">
<div class="widget-header widget-header-blue widget-header-flat">
<h4 class="widget-title lighter">Form Wizard</h4>
</div>
<!-- end of widget header -->
<!-- R-1 -->
<div class="widget-body">
<div class="widget-main" style="padding-bottom: 0;">
<h4 class="lighter block green">Employee General Information
<span class="help-button" style="background-color: red;" data-rel="popover" data-trigger="hover" data-placement="right" data-content="If a field is not applicable please type N/A" title="Notice!">
<i class="fa fa-exclamation"></i>
</span>
</h4>
<div class="row">
<!-- B-1 col -->
<div class="col-xs-12 col-sm-4">
<div class="widget-box">
<div class="widget-header">
<h5 class="widget-title">General Information</h5>
</div>
<!-- end of widget header -->
<div class="widget-body">
<div class="widget-main">
<!-- employee information B-1-->
<!-- first name -->
<?php echo form_label('First Name', 'first_name', array('class' => 'control-label'));?>
<span style="font-weight: bolder; color: red;">*</span>
<div class="form-group <?php if (form_error('first_name')==TRUE){echo 'has-error';} ?>" style="margin-bottom: 0;">
<div class="col-sm-12">
<?php echo form_input('first_name', set_value('first_name', $employee->first_name),
array( 'type' => 'text',
'name' => 'first_name',
'id' => 'firstname',
'class' => 'form-control')); ?>
</div>
</div>
<!-- validation -->
<?php if (!empty($error)): ?>
<div class="has-error" style="margin-bottom: 0;">
<div class="help-block col-xs-12 col-sm-reset inline"
style="margin-bottom: 0; font-size: 12px;">
<?php echo form_error('first_name'); ?>
</div>
</div>
<?php endif ?>
<!-- last name -->
<?php echo form_label('Last Name', 'last_name', array('class' => 'control-label'));?>
<span style="font-weight: bolder; color: red;">*</span>
<div class="form-group <?php if (form_error('last_name')==TRUE){echo 'has-error';} ?>" style="margin-bottom: 0;">
<div class="col-sm-12">
<?php echo form_input('last_name', set_value('last_name', $employee->last_name),
array( 'type' => 'text',
'name' => 'last_name',
'id' => 'lastname',
'class' => 'form-control'));?>
</div>
</div>
<!-- validation -->
<?php if (!empty($error)): ?>
<div class="has-error" style="margin-bottom: 0;">
<div class="help-block col-xs-12 col-sm-reset inline"
style="margin-bottom: 0; font-size: 12px;">
<?php echo form_error('last_name'); ?>
</div>
</div>
<?php endif ?>
<!-- middle name -->
<?php echo form_label('Middle Name', 'middle_name', array('class' => 'control-label'));?>
<span style="font-weight: bolder; color: red;">*</span>
<div class="form-group <?php if (form_error('middle_name')==TRUE){echo 'has-error';} ?>" style="margin-bottom: 0;">
<div class="col-sm-12">
<?php echo form_input('middle_name', set_value('middle_name', $employee->middle_name),
array( 'type' => 'text',
'name' => 'middle_name',
'id' => 'middlename',
'class' => 'form-control'));?>
</div>
</div>
<!-- validation -->
<?php if (!empty($error)): ?>
<div class="has-error" style="margin-bottom: 0;">
<div class="help-block col-xs-12 col-sm-reset inline"
style="margin-bottom: 0; font-size: 12px;">
<?php echo form_error('middle_name'); ?>
</div>
</div>
<?php endif ?>
<!-- suffix name -->
<?php echo form_label('Suffix', 'suffix_name', array('class' => 'control-label'));?>
<div class="form-group" style="margin-bottom: 0;">
<div class="col-sm-6">
<?php echo form_input('suffix_name', set_value('suffix_name', $employee->suffix_name),
array( 'type' => 'text',
'name' => 'suffix_name',
'id' => 'suffixname',
'class' => 'form-control'));?>
</div>
</div>
<hr>
</div>
</div>
</div>
</div>
<!-- end of B-1 col -->
please anyone i really need your help
You need to use form_open_multipart
This function is absolutely identical to form_open() ... , except that
it adds a multipart attribute, which is necessary if you would like to
use the form to upload files with.
Ref: https://www.codeigniter.com/user_guide/helpers/form_helper.html
So I have this form in which a user can update his information:
The problem comes when some of these inputs are optional. Let's say a user updates his email and password, then the update updates but when a user only edits his email and leave the password inputs blank the password in the database should not be updated....it currently changes the password as well even when they're empty.
Here is my HTML:
<?php echo validation_errors('<p class="alert alert-dismissable alert-danger">'); ?>
<?php echo form_open('users/edit/'.$item->id); ?>
<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
<li class="active">Basics</li>
<li class="">About Me</li>
</ul>
<br>
<div class="tab-content">
<!-- Basics -->
<div class="tab-pane active" id="basics">
<!-- Email -->
<div class="form-group">
<?php echo form_label('Email', 'email'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-envelope" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'email',
'id' => 'email',
'maxlength' => '150',
'class' => 'form-control',
'value' => $item->email,
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Avatar Image -->
<div class="form-group">
<?php echo form_label('Avatar Image URL', 'avatar_img'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-id-card-o" aria-hidden="true"></i></i></div>
<?php
$data = array(
'name' => 'avatar_img',
'id' => 'avatar_img',
'class' => 'form-control',
'placeholder' => '96x96 Pixels',
'value' => $item->avatar_img
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Cover Image -->
<div class="form-group">
<?php echo form_label('Cover Img URL', 'cover_img'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-id-card-o" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'cover_img',
'id' => 'cover_img',
'class' => 'form-control',
'value' => $item->cover_img
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Occupation -->
<div class="form-group">
<?php echo form_label('Occupation', 'occupation'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-briefcase" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'occupation',
'id' => 'occupation',
'class' => 'form-control',
'value' => $item->occupation
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Website -->
<div class="form-group">
<?php echo form_label('Website', 'website'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-link" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'website',
'id' => 'website',
'class' => 'form-control',
'value' => $item->website
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Password -->
<div class="form-group">
<?php echo form_label('Password', 'password'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-lock" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'password',
'id' => 'password',
'class' => 'form-control',
'value' => set_value('password'),
);
?>
<?php echo form_password($data); ?>
</div>
</div>
<!-- Password2 -->
<div class="form-group">
<?php echo form_label('Confirm Password', 'password2'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-lock" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'password2',
'id' => 'password2',
'class' => 'form-control',
'value' => set_value('password2'),
);
?>
<?php echo form_password($data); ?>
</div>
</div>
</div>
</div>
</div>
<?php echo form_submit('mysubmit', 'Update User', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>
and here is what I have tried:
<?php
$data = array(
'email' => $this->input->post('email'),
'avatar_img' => $this->input->post('avatar_img'),
'cover_img' => $this->input->post('cover_img'),
'occupation' => $this->input->post('occupation'),
'website' => $this->input->post('website'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'password2' => password_hash($this->input->post('password2'), PASSWORD_DEFAULT),
);
if($this->input->post('password') != ''){
$data['password'] = ($this->input->post('password') && !empty($this->input->post('password'))) ? $this->input->post('password') : NULL;
}
if($this->input->post('password2') != ''){
$data['password2'] = ($this->input->post('password2') && !empty($this->input->post('password2'))) ? $this->input->post('password2') : NULL;
}
// Update User
$this->User_model->update($id, $data);
?>
but it just don't work, so I tried making more simple:
<?php
$data = array(
'email' => $this->input->post('email'),
'avatar_img' => $this->input->post('avatar_img'),
'cover_img' => $this->input->post('cover_img'),
'occupation' => $this->input->post('occupation'),
'website' => $this->input->post('website'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'password2' => password_hash($this->input->post('password2'), PASSWORD_DEFAULT),
);
if($this->input->post('password') != ''){
$data['password2'] = password_hash($this->input->post('password2'), PASSWORD_DEFAULT);
}
if($this->input->post('password2') != ''){
$data['password2'] = password_hash($this->input->post('password2'), PASSWORD_DEFAULT);
}
// Update User
$this->User_model->update($id, $data);
?>
Update method in Userr model:
public function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update($this->table, $data);
}
Thanks in advance.
If you want the password(s) to be updated on database only if the password field(s) are NOT empty, do it like below:
<?php
$data = array(
'email' => $this->input->post('email'),
'avatar_img' => $this->input->post('avatar_img'),
'cover_img' => $this->input->post('cover_img'),
'occupation' => $this->input->post('occupation'),
'website' => $this->input->post('website')
);
if(trim($this->input->post('password')) != ''){
$data['password'] = password_hash(trim($this->input->post('password')), PASSWORD_DEFAULT);
}
if(trim($this->input->post('password2')) != ''){
$data['password2'] = password_hash(trim($this->input->post('password2')), PASSWORD_DEFAULT);
}
// Update User
$this->User_model->update($id, $data);
?>
I removed "password" and "password2" from first array list and kept in condition check. I added trim in case the fields have white-spaces in it.
I hope this will work!
edit this code.
$data = array(
'email' => $this->input->post('email'),
'avatar_img' => $this->input->post('avatar_img'),
'cover_img' => $this->input->post('cover_img'),
'occupation' => $this->input->post('occupation'),
'website' => $this->input->post('website'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'password2' => password_hash($this->input->post('password2'), PASSWORD_DEFAULT),
);
to
if(!empty($this->input->post('email'))){
$data['email'] = $this->input->post('email');
}
if(!empty($this->input->post('avatar_img'))){
$data['avatar_img'] = $this->input->post('avatar_img');
}
if(!empty($this->input->post('cover_img'))){
$data['cover_img'] = $this->input->post('cover_img');
}
if(!empty($this->input->post('occupation'))){
$data['occupation'] => $this->input->post('occupation');
}
if(!empty($this->input->post('website'))){
$data['website'] = $this->input->post('website');
}
if(!empty($this->input->post('password'))){
$data['password'] = password_hash($this->input->post('password'), PASSWORD_DEFAULT);
}
if(!empty($this->input->post('password2'))){
$data['password2'] = password_hash($this->input->post('password2'), PASSWORD_DEFAULT);
}
I have a datepicker form using bootstrap, but when I click on it, it does not show the date on the popup (the popup is truncated).
Code of the script :
// Date Picker
jQuery('#datepicker').datepicker();
jQuery('#datepicker-autoclose').datepicker({
autoclose: true,
todayHighlight: true
});
and then the result of click comes out as below:
I use codeigniter and this is my controller :
function save()
{
$this->template->set_title('Pickup');
$this->breadcrumbs->push('Pickup', 'pickup');
$this->breadcrumbs->push('Create Pickup', '/pickup/list');
$this->breadcrumbs->push('Save', '/pickup/add');
// JS Init
$js_init = "";
// Include CSS & JS
$this->template->add_init($js_init);
$this->template->add_includes('css', 'assets/plugins/bootstrap-datepicker/css/bootstrap-datepicker.min.css');
$this->template->add_includes('css', 'assets/plugins/clockpicker/css/bootstrap-clockpicker.min.css');
$this->template->add_includes('js', 'assets/plugins/moment/moment.js');
$this->template->add_includes('js', 'assets/plugins/timepicker/bootstrap-timepicker.js');
$this->template->add_includes('js', 'assets/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js');
$this->template->add_includes('js', 'assets/plugins/clockpicker/js/bootstrap-clockpicker.min.js');
$this->template->add_includes('js', 'assets/plugins/clockpicker/js/bootstrap-clockpicker.min.js');
$this->template->add_includes('js', 'assets/js/popper.min.js');
$this->template->add_includes('js', 'assets/pages/jquery.form-pickers.init.js');
$this->template->add_includes('js', 'assets/plugins/validator/validator.min.js');
if (!$this->ion_auth->logged_in())
{
redirect('auth', 'refresh');
}
// validate form input
$this->form_validation->set_rules('pickup_code', 'Pickup Code', 'required');
$this->form_validation->set_rules('pickup_date','Date', 'required');
$this->form_validation->set_rules('pickup_address', 'Address', 'required');
// serial number untuk pickup
$serial = $this->MY_Model->generate_number($this->tables,$this->pk,'10','PCK');
if ($this->form_validation->run() == TRUE)
{
$data = array(
'pickup_code' =>$this->input->post('pickup_code'),
'pickup_date' =>$this->input->post('pickup_date'),
'pickup_address' =>$this->input->post('pickup_address')
);
$new_supplier = $this->MY_Model->insert($this->tables,$data);
redirect("pickup/listing", 'refresh');
}
else
{
$this->data['pickup_code'] = array(
'name' => 'pickup_code',
'id' => 'pickup_code',
'type' => 'text',
'data-error' => 'Please enter pickup code.',
'class' => 'form-control',
'required' => 'required',
'value' => $this->form_validation->set_value('pickup_code',$serial),
'readonly' => 'readonly',
);
$this->data['pickup_date'] = array(
'name' => 'pickup_date',
'id' => 'datepicker',
'type' => 'text',
'data-error' => 'Please enter pickup date.',
'class' => 'form-control',
'required' => 'required',
'value' => $this->form_validation->set_value('pickup_date'),
);
$this->data['pickup_note'] = array(
'name' => 'pickup_note',
'id' => 'pickup_note',
'type' => 'text',
'data-error' => 'Please enter note.',
'class' => 'form-control',
'value' => $this->form_validation->set_value('pickup_note'),
);
$this->data['pickup_address'] = array(
'name' => 'pickup_address',
'id' => 'pickup_address',
'type' => 'text',
'data-error' => 'Please enter address.',
'class' => 'form-control',
'required' => 'required',
'value' => $this->form_validation->set_value('pickup_address'),
);
$this->template->load('templates/Template',$this->folder.'/add',$this->data);
}
}
and this is my view :
<div class="row">
<div class="col-sm-12">
<div class="card-box">
<?php echo form_open("pickup/save_pickup","data-toggle='validator'");?>
<h4 class="m-t-0 header-title"><b>Pickup Save</b></h4>
<p class="text-muted m-b-30 font-13">
<?php echo lang('create_group_subheading');?>
</p>
<?php if ($this->session->flashdata('message')) {?>
<div class='alert alert-danger alert-dismissable'>
<?php echo $this->session->flashdata('message'); ?>
</div>
<?php }?>
<div class="row">
<div class="col-md-6">
<div class="p-20">
<div class="form-group">
<label for="group_name"> Pickup Code
<?php ?>
<span class="text-danger">*</span></label>
<?php echo form_input($pickup_code);?>
<p class="help-block with-errors text-danger"></p>
</div>
<div class="form-group">
<label for="description"> Date
</label>
<span class="text-danger">*</span></label>
<?php echo form_input($pickup_date);?>
<div class="help-block with-errors text-danger"></div>
</div>
<div class="form-group">
<label for="description"> Note
</label>
<?php echo form_textarea($pickup_note);?>
<div class="help-block with-errors text-danger"></div>
</div>
<div class="form-group">
<label for="description"> Address
</label>
<span class="text-danger">*</span></label>
<?php echo form_textarea($pickup_address);?>
<div class="help-block with-errors text-danger"></div>
</div>
</div>
</div>
</div>
<div class="form-group text-right m-b-0">
<button class="btn btn-primary waves-effect waves-light" type="submit">
<i class="fa fa-save m-r-5"></i> <span>Save</span>
</button>
<a href="<?php echo base_url('pickup'); ?>" class="btn btn-secondary waves-effect m-l-5">
<i class="fa fa-reply m-r-5"></i> <span>Cancel</span>
</a>
<?php echo form_close();?>
</div>
</div>
</div>
I am new to Ion Auth codeigniter library. Now I am making a custom bootstrap form to add a user using modal but I always get errors. Any help would be much appreciated. The modal is inside the userList.php view that displays all registered users.
Controller: Users.php
function create_user()
{
$this->data['title'] = "Create User";
if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
$tables = $this->config->item('tables','ion_auth');
$identity_column = $this->config->item('identity','ion_auth');
$this->data['identity_column'] = $identity_column;
// validate form input
$this->form_validation->set_rules('first_name', $this->lang->line('create_user_validation_fname_label'), 'required');
$this->form_validation->set_rules('last_name', $this->lang->line('create_user_validation_lname_label'), 'required');
if($identity_column!=='email')
{
$this->form_validation->set_rules('identity',$this->lang->line('create_user_validation_identity_label'),'required|is_unique['.$tables['users'].'.'.$identity_column.']');
$this->form_validation->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'required|valid_email');
}
else
{
$this->form_validation->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'required|valid_email|is_unique[' . $tables['users'] . '.email]');
}
$this->form_validation->set_rules('phone', $this->lang->line('create_user_validation_phone_label'), 'trim');
$this->form_validation->set_rules('company', $this->lang->line('create_user_validation_company_label'), 'trim');
$this->form_validation->set_rules('password', $this->lang->line('create_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', $this->lang->line('create_user_validation_password_confirm_label'), 'required');
if ($this->form_validation->run() == true)
{
$email = strtolower($this->input->post('email'));
$identity = ($identity_column==='email') ? $email : $this->input->post('identity');
$password = $this->input->post('password');
$additional_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone'),
);
var_dump($first_name);
}
if ($this->form_validation->run() == true && $this->ion_auth->register($identity, $password, $email, $additional_data))
{
// check to see if we are creating the user
// redirect them back to the admin page
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect("auth", 'refresh');
}
else
{
// display the create user form
// set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
$this->data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name'),
);
$this->data['last_name'] = array(
'name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name'),
);
$this->data['identity'] = array(
'name' => 'identity',
'id' => 'identity',
'type' => 'text',
'value' => $this->form_validation->set_value('identity'),
);
$this->data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$this->data['company'] = array(
'name' => 'company',
'id' => 'company',
'type' => 'text',
'value' => $this->form_validation->set_value('company'),
);
$this->data['phone'] = array(
'name' => 'phone',
'id' => 'phone',
'type' => 'text',
'value' => $this->form_validation->set_value('phone'),
);
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password',
'value' => $this->form_validation->set_value('password'),
);
$this->data['password_confirm'] = array(
'name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password',
'value' => $this->form_validation->set_value('password_confirm'),
);
$this->_render_page('userList', $this->data);
}
}
View: userList.php
<div class="modal fade" id="add-user" role="dialog">
<div class="modal-dialog modal-default">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 id="modal-user-profile" class="modal-title">Add User</h4>
</div>
<div class="modal-body">
<?php echo form_open('users/create_user', ['class' => 'form-horizontal', 'role' => 'form']); ?>
<div class="form-group">
<label for="first_name" class="col-sm-2 control-label">First Name</label>
<div class="col-sm-10">
<?php echo form_input($first_name);?>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<?php echo form_password(['name' => 'password', 'id' => 'password', 'class' => 'form-control', 'placeholder' => 'Password']); ?>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Confirm Password</label>
<div class="col-sm-10">
<?php echo form_password(['name' => 'password_confirm', 'id' => 'password_confirm', 'class' => 'form-control', 'value' => set_value('password_confirm'), 'placeholder' => 'Confirm Password']); ?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<?php echo form_input(['name' => 'email', 'id' => 'email', 'class' => 'form-control', 'value' => set_value('email'), 'placeholder' => 'Email']); ?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Contact No</label>
<div class="col-sm-10">
<?php echo form_input(['name' => 'contact', 'id' => 'contact', 'class' => 'form-control', 'value' => set_value('contact'), 'placeholder' => 'Contact']); ?>
</div>
</div>
<div class="form-group">
<label for="status" class="col-sm-2 control-label">Group</label>
<div class="col-sm-10">
<select class="form-control" id="group">
<option>--</option>
<option>Admin</option>
<option>Client</option>
<option>BIR</option>
</select>
</div>
</div>
<div class="form-group">
<label for="status" class="col-sm-2 control-label">Status</label>
<div class="col-sm-10">
<select class="form-control" id="status">
<option>--</option>
<option>Active</option>
<option>Inactive</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default create-user">Create</button>
<button type="button" class="btn btn-danger create">Cancel</button>
</div>
</div>
<?php echo form_close(); ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
What errors are you getting??
In your controller, Use this to verify your results
After query;
echo $this->db->last_query();
var_dump($_POST);exit;
First check all the post data and the query and tell the errors you're getting
I'm probably missing a real simple point here but would one of you Codeigniter gurus' be able to help me with my issue. I have a userform which stores information for a sales page, which holds both text and images saved to a database.
I am able to save one image to the database fine, but multiple i'm struggling. Currently all the images are being transferred to the correct folder but I need them to store in the MySQL table.
Form:
<div class="form-group">
<label class="col-lg-4 control-label">Avonics:</label>
<div class="col-lg-6">
<?php
$avonics6 = array(
'name' => 'avonics6',
'class' => 'form-control',
'placeholder' => 'Avonics',
'value' => set_value('avonics6')
);
echo form_input($avonics6);
?>
</div>
</div>
</fieldset>
<fieldset class="fieldset_class">
<legend><strong><font color="black">Aircraft Condition</font></strong></legend>
<div class="form-group">
<label class="col-lg-4 control-label">Interior:</label>
<div class="col-lg-6">
<?php
$interior = array(
'name' => 'interior',
'class' => 'form-control',
'placeholder' => 'Interior 0-100%',
'value' => set_value('interior')
);
echo form_input($interior);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Exterior:</label>
<div class="col-lg-6">
<?php
$exterior = array(
'name' => 'exterior',
'class' => 'form-control',
'placeholder' => 'Exterior 0-100%',
'value' => set_value('exterior')
);
echo form_input($exterior);
?>
</div>
</div>
</fieldset>
<fieldset class="fieldset_class">
<legend><strong><font color="black">Upload Images</font></strong></legend>
<div class="form-group">
<label class="col-lg-4 control-label">Add Image (1)</label>
<div class="col-lg-6">
<?php
$aircraft1 = array(
'name' => 'userfile[]',
'class' => 'form-control',
'id' => 'userfile',
'type' => 'file',
'multiple' => ''
);
echo form_upload($aircraft1);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Add Image (2)</label>
<div class="col-lg-6">
<?php
$aircraft2 = array(
'name' => 'userfile[]',
'class' => 'form-control',
'id' => 'userfile',
'type' => 'file',
'multiple' => 'true'
);
echo form_upload($aircraft2);
?>
</div>
</div>
</fieldset>
<div class="form-group">
<label class="col-lg-4 control-label"></label>
<div class="col-lg-8">
<?php echo form_submit('submit', 'Add Aircraft', 'class="btn btn-danger"');?>
</div>
</div>
<?php
echo form_close();
?>
</div>
</div><div class="clearfix"></div>
Controller:
function add_aircraft()
{
$this->load->library('form_validation');
/* handle form data then send to model */
$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('price', 'Price', 'trim|required');
$this->form_validation->set_rules('year', 'Year', 'trim|required');
$this->form_validation->set_rules('annual', 'Annual', 'trim|required');
$this->form_validation->set_rules('serial_number', 'Serial number', 'trim|required');
$this->form_validation->set_rules('airframe_hours', 'Airframe hours', 'trim|required');
$this->form_validation->set_rules('engine_type', 'Engine type', 'trim|required');
$this->form_validation->set_rules('engine_hours', 'Engine hours', 'trim|required');
$this->form_validation->set_rules('propeller_type', 'Propeller type', 'trim|required');
$this->form_validation->set_rules('notes', 'Sales Pitch', 'trim|required');
$this->form_validation->set_rules('avonics', 'Avonics', 'trim');
$this->form_validation->set_rules('avonics1', 'Avonics', 'trim');
$this->form_validation->set_rules('avonics2', 'Avonics', 'trim');
$this->form_validation->set_rules('avonics3', 'Avonics', 'trim');
$this->form_validation->set_rules('avonics4', 'Avonics', 'trim');
$this->form_validation->set_rules('avonics5', 'Avonics', 'trim');
$this->form_validation->set_rules('avonics6', 'Avonics', 'trim');
$this->form_validation->set_rules('interior', 'Interior', 'trim|required');
$this->form_validation->set_rules('exterior', 'Exterior', 'trim|required');
$this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
if($this->form_validation->run() == FALSE)
{
$data['main_content'] = 'control/aircraftsales/add';
$this->load->view('control/includes/template_simple_header_footer', $data);
$name_array[] = $data['aircraft1'];
}
else
{
$this->load->model('control/Aircraftsales_model');
if($this->Aircraftsales_model->add_aircraft()){
$data['main_content'] = 'control/aircraftsales/add';
$this->load->view('control/includes/template_simple_header_footer', $data);
}
}
}
Model (this seems to be the issue??):
function add_aircraft()
{
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key=>$value)
for($s=0; $s<=$count-1; $s++)
{
$_FILES['userfile']['name']=$value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
/* config image */
$config['upload_path'] = './upload/aircraftsales';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
$names= implode(',', $name_array);
}
$aircraft = array(
'title' => $this->input->post('title'),
'price' => $this->input->post('price'),
'year' => $this->input->post('year'),
'annual' => $this->input->post('annual'),
'serial_number' => $this->input->post('serial_number'),
'airframe_hours' => $this->input->post('airframe_hours'),
'engine_type' => $this->input->post('engine_type'),
'engine_hours' => $this->input->post('engine_hours'),
'propeller_type' => $this->input->post('propeller_type'),
'notes' => $this->input->post('notes'),
'avonics' => $this->input->post('avonics'),
'avonics1' => $this->input->post('avonics1'),
'avonics2' => $this->input->post('avonics2'),
'avonics3' => $this->input->post('avonics3'),
'avonics4' => $this->input->post('avonics4'),
'avonics5' => $this->input->post('avonics5'),
'avonics6' => $this->input->post('avonics6'),
'interior' => $this->input->post('interior'),
'exterior' => md5($this->input->post('exterior')),
);
$insert = $this->db->insert('aircraftsales', $aircraft);
return $insert;
}
}
In your code you use return $insert;.
return act as exit. So when its reach it will stop the foreach and return value imidiatly to controller.
And you no need to use for() loop inside foreach loop