I want to add upload image code to my form - php

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

Related

Yii2 Dynamic Data is not loading on all DepDrop in dynamic field

I'm trying to create a dynamic form for one of my projects. I used wbraganca's Dynamic From to achieve this. In my dynamic form I have a DepDrop to show subcategories list from a parent category.
My _form.php
<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>
<div class="row">
<div class="col-md-6">
<?= $form->field($model, 'rq_job_no')->textInput() ?>
</div>
<div class="col-md-6">
<?= $form->field($model, 'rq_req_date')->widget(\yii\jui\DatePicker::class, [
//'language' => 'ru',
'dateFormat' => 'php:Y-m-d',
'clientOptions' => [
'changeMonth' => true,
'changeYear' => true,
'showButtonPanel' => true,
'yearRange' => '1990:2030',
'minDate' => date('Y-m-d'),
],
'options' => ['class' => 'form-control', 'readOnly' => true, 'placeholder' => 'Enter the Item Required Date'],
]) ?>
</div>
<div class="col-md-6">
<?= $form->field($model, 'rq_priority_type')->dropDownList(['urgent' => 'Urgent', 'normal' => 'Normal'], ['prompt' => '--Select Priority Type--']) ?>
</div>
<div class="col-md-6">
</div>
<div class="col-md-6">
<?= $form->field($model, 'rq_approval_type')->dropDownList([1 => ' Approved Vendor Needed', 2 => 'Approved Submitted Needed', 3 => 'Warranty Certificate to be Collected', 4 => 'Long Lead Material',], ['prompt' => '--Select Type of Approval--']) ?>
<?php echo $form->field($model, 'rq_cat_type')->widget(Select2::class, [
'data' => $catData,
'options' => ['placeholder' => '--Select Request Type--', 'class' => 'form-control'],
'pluginOptions' => [
'allowClear' => true
],
]);
?>
</div>
<div class="col-md-12">
<?= $form->field($model, 'rq_remarks')->textarea(['rows' => 6]) ?>
</div>
</div>
<!-- code for dynamic form -->
<div class="panel panel-default">
<div class="panel-heading">
<h4><i class="glyphicon glyphicon-envelope"></i> Request Items</h4>
</div>
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: css class
'limit' => 10, // the maximum times, an element can be cloned (default 999)
'min' => 1, // 0 or 1 (default 1)
'insertButton' => '.add-item', // css class
'deleteButton' => '.remove-item', // css class
'model' => $modelsAddress[0],
'formId' => 'dynamic-form',
'formFields' => [
'item_name',
'item_qty',
'item_unit',
],
]); ?>
<div class="container-items">
<!-- widgetContainer -->
<?php foreach ($modelsAddress as $i => $modelAddress) : ?>
<div class="item panel panel-default">
<!-- widgetBody -->
<div class="panel-heading">
<h4 class="panel-title pull-left">Items</h4>
<div class="pull-right">
<button type="button" class="add-item btn btn-success btn-xs"><i class="fa fa-plus"></i></button>
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (!$modelAddress->isNewRecord) {
echo Html::activeHiddenInput($modelAddress, "[{$i}]rt_id");
}
?>
<?php //$form->field($modelAddress, "[{$i}]rt_item")->textInput(['maxlength' => true]) ?>
<?= $form->field($modelAddress, "[{$i}]rt_item")->widget(DepDrop::class, [
'options' => ['id' => 'reqitems-'.$i.'-rt_item'],
'pluginOptions' => [
'depends' => ['requests-rq_cat_type'],
'placeholder' => '--Select Location--',
'url' => Url::to(['/requests/subcat'])
]
]); ?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($modelAddress, "[{$i}]rt_qty")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-6">
<?= $form->field($modelAddress, "[{$i}]rt_unit")->textInput(['maxlength' => true]) ?>
</div>
</div><!-- .row -->
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
My Controller action to load the subcategory
public function actionSubcat()
{
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = self::getSubCatList($cat_id);
return ['output' => $out, 'selected' => ''];
}
}
return ['output' => 'No Category Available', 'selected' => ''];
}
public function getSubCatList($cat_id)
{
$output = [];
$category = Item::find()->where(['item_category' => $cat_id])->orderBy('item_title')->all();
if (empty($category)) {
$output[] = ['id' => '0', 'name' => 'No Category Available'];
} else {
foreach ($category as $cat) {
$output[] = ['id' => $cat->item_id, 'name' => $cat->item_title];
}
}
return $output;
}
My problem is that DepDrop is only loading the subcategory for the first loop of the dynamic form and leaves the rest loop blank. Attached screenshot for your reference.
Can anyone help by pointing what I am missing or how can I achieve my requirement.

How to update optional fields if not empty in CodeIgniter?

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

Bootstrap datepicker does not show date on popup

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>

save message is not displayed in yii form

I have 2 forms, the save message is displayed for 1st, but don't display on another.
1st view give confirmation message but the 2nd don't, the data is saved in tables.
gist for codes
in the above gist, you can see the controller and views for both.
You can used this code for you solution. Check set Message code in below.
view.php
<div class="ibox">
<div class="ibox-heading">
<div class="ibox-title">
<h3><?PHP echo(($model->isNewRecord) ? 'Create Fee Structure' : 'Update Fee Structure'); ?></h3>
</div>
</div>
<!-- /.box-header -->
<!-- Set Message start -->
<?php if(Yii::app()->user->hasFlash('confirm')):?>
<div class="info">
<?php echo Yii::app()->user->getFlash('confirm'); ?>
</div>
<?php endif; ?>
<!-- Message end -->
<!-- form start -->
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'fee_structure_form',
'enableAjaxValidation' => true,
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
'role' => 'form'
),
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => true,
'validateOnType' => false,
),
)); ?>
<div class="ibox-content">
<div class="row">
<div class="col-md-6 col-xs-12">
<div class="form-group">
<?PHP
echo $form->label($model, 'class_id');
echo $form->dropDownList($model, 'class_id', $studentClasses,array('placeholder' => 'Class', 'class' => 'form-control'));
echo $form->error($model,'class_id');
?>
</div>
<div class="form-group">
<?PHP
echo $form->label($model, 'fee_type');
echo $form->dropDownList($model, 'fee_type', $feeTypes, array('placeholder' => 'Fee Type', 'class' => 'form-control'));
echo $form->error($model,'fee_type');
?>
</div>
<div class="form-group">
<?PHP
echo $form->label($model, 'amount');
echo $form->textField($model, 'amount', array('placeholder' => 'Amount', 'class' => 'form-control'));
echo $form->error($model,'amount');
?>
</div>
<div class="form-group">
<?PHP
echo $form->label($model, 'remarks');
echo $form->textField($model, 'remarks', array('placeholder' => 'Remarks', 'class' => 'form-control'));
echo $form->error($model,'remarks');
?>
</div>
</div>
</div>
</div>
<div class="ibox-footer col-md-12 col-xs-12" style="margin-bottom: 60px;">
<button class="btn col-md-3 col-xs-12 btn-primary pull-left" type="submit">
<?PHP echo Definations::SAVEFORM; ?>
</button>
</div>
<?PHP
$this->endWidget();
?>
</div>

Upload photo with thumbnail preview, using codeigniter

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

Categories