I am trying to validate a form through config. When i submit blank input field,it just shoes same page. I needs errors displayed in add_article.php Help....
config/form_validation.php
<?php
$config = [
'add_article_rules' => [
[
'field' => 'title',
'label' => 'Article Title',
'rules' => 'required|alphadash'
],
[
'field' => 'body',
'label' => 'Article Body',
'rules' => 'required'
]
]
];
My Controller (admin.php)
<?php
class Admin extends MY_Controller{
public function __construct(){
parent::__construct();
if(! $this->session->userdata('user_id')){
return redirect('login');
}
}
public function dashboard(){
$this->load->model('articlesmodel','articles');
$articles= $this->articles->article_list();
$this->load->view('admin/dashboard',['articles'=>$articles]);
}
public function add_article(){
$this->load->view('admin/add_article');
}
public function store_article(){
$this->load->library('form_validation');
if($this->form_validation->run('add_article_rules')){
//if sucesss
}else{
return redirect('admin/add_article');
}
}
public function edit_select(){}
public function edit_article(){}
public function delete_article(){}
}
View file (add_article.php)
<?php require_once('admin_header.php'); ?>
<div class="container">
<?php echo validation_errors(); ?>
<div class="row">
<?php echo form_open('admin/store_article',['class'=>'form-horizontal']); ?>
<?php echo form_hidden('user_id', $this->session->userdata('user_id'));?>
<fieldset>
<legend>Add Article</legend>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="inputEmail" class="col-lg-4 control-label">Title</label>
<div class="col-lg-8">
<?php
$data = array(
'name' => 'title',
'class' => 'form-control',
'value' => set_value('title'),
'placeholder' => 'Username'
);
echo form_input($data);
?>
</div>
</div>
</div>
<div class="col-sm-6">
<?php echo form_error('title'); ?>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for="inputPassword" class="col-lg-4 control-label">Article Body</label>
<div class="col-lg-8">
<?php
$data = array(
'name' => 'body',
'value' => set_value('body'),
'class' => 'form-control',
'placeholder' => 'Article Body',
);
echo form_textarea($data);
?>
</div>
</div>
</div>
<div class="col-sm-6">
<?php echo form_error('body'); ?>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<div class="col-lg-8 col-lg-offset-4">
<?php
$data = array(
'name' => 'reset',
'class' => 'btn btn-default',
'value' => 'Reset',
);
echo form_reset($data);
?>
<?php
$data = array(
'name' => 'submit',
'class' => 'btn btn-primary',
'value' => 'Submit',
);
echo form_submit($data);
?>
</div>
</div>
</div>
</div>
</fieldset>
</form>
</div>
</div>
<?php require_once('admin_footer.php'); ?>
// Just Change Admin Controller method and view file
// View File in add_article.php
<?php echo form_open(); ?>
//Controller File
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
}
public function add_article()
{
if ($this->form_validation->run('add_article_rules')) {
echo "Success";
} else {
$this->load->view('admin/add_article');
}
}
public function edit_select()
{
}
public function edit_article()
{
}
public function delete_article()
{
}
}
Related
I want to add a File Upload field in an ActiveForm in Yii2 framework, but it's not working, the file is not uploaded nor the name of the file is stored in the database column 'company_cover'.
If someone could help me find my mistake.
Thanks in advance.
The VIEW
<div class="block-body collapse" id="company-block">
<?php $form = ActiveForm::begin([
'id' => 'form-change-company',
'method' => 'post',
'action' => ['account/update-company'],
'enableAjaxValidation' => false,
'options' => ['enctype' => 'multipart/form-data'],
]);
?>
<div class="row">
<div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
<?= $form->field($modelAbout, 'group_id', [
'template' => '{input} {error}',
])->dropDownList([2 => t('app','Enable'), 1 => t('app','Disable')])->label(false);
?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'store_name', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Store Name'), 'class' => ''])->label(false); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_name', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company Name'), 'class' => ''])->label(false); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_mail', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company E-Mail'), 'class' => ''])->label(false); ?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_no', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company No'), 'class' => ''])->label(false); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'vat', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','VAT'), 'class' => ''])->label(false); ?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_lat', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company Latitude'), 'class' => ''])->label(false); ?>
</div>
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'company_lng', [
'template' => '{input} {error}',
])->textInput([ 'placeholder' => t('app','Company Longitude'), 'class' => ''])->label(false); ?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<?= $form->field($modelCompany, 'file')->fileInput([ 'placeholder' => t('app','Company Cover'), 'class' => '']) ?>
</div>
</div>
<div class="row">
<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
<button type="submit" id="submit-company-info" class="btn-as" value="Submit"><?=t('app','Submit');?></button>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
The MODEL
class CustomerStore extends \app\models\auto\CustomerStore
{
// when inactive model
const STATUS_INACTIVE = 'inactive';
// when active model
const STATUS_ACTIVE = 'active';
// when deactivated
const STATUS_DEACTIVATED = 'deactivated';
public $file;
public function rules()
{
return [
[['store_name', 'company_name', 'company_mail', 'company_lat', 'company_lng', 'company_no', 'vat'], 'trim'],
[['store_name', 'company_name'], 'required'],
[['file'], 'file'],
[['store_name', 'company_name'], 'string', 'max' => 30],
[['company_no', 'vat'], 'string', 'max' => 20],
[['status'], 'safe']
];
}
/**
* #inheritdoc
*/
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors[] = [
'class' => SluggableBehavior::className(),
'value' => [$this, 'getSlug'] //https://github.com/yiisoft/yii2/issues/7773
];
return $behaviors;
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return ArrayHelper::merge(parent::attributeLabels(), [
'store_id' => t('app', 'Store ID'),
'customer_id' => t('app', 'Customer'),
'store_name' => t('app', 'Store Name'),
'company_name' => t('app', 'Company Name'),
'company_mail' => t('app', 'Company E-Mail'),
'company_no' => t('app', 'Company No'),
'vat' => t('app', 'VAT'),
'status' => t('app', 'Status'),
'company_lat' => t('app', 'Company Latitude'),
'company_lng' => t('app', 'Company Longitude'),
'company_cover' => t('app', 'Company Cover'),
'created_at' => t('app', 'Created At'),
'updated_at' => t('app', 'Updated At'),
]);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getCustomer()
{
return $this->hasOne(Customer::className(), ['customer_id' => 'customer_id']);
}
/**
* #return bool
*/
public function deactivate()
{
$this->status = self::STATUS_INACTIVE;
$this->save(false);
return true;
}
/**
* #return bool
*/
public function activate()
{
if($this->status == self::STATUS_DEACTIVATED || $this->status == self::STATUS_INACTIVE) {
$this->status = self::STATUS_ACTIVE;
$this->save(false);
}
return true;
}
/**
* #param $slug
* #return array|null|\yii\db\ActiveRecord
*/
public function findBySlug($slug)
{
return $this->find()->where(array(
'slug' => $slug,
))->one();
}
/**
* #param $event
* #return string
* //https://github.com/yiisoft/yii2/issues/7773
*/
public function getSlug($event)
{
if(!empty($event->sender->slug)) {
return $event->sender->slug;
}
return Inflector::slug($event->sender->store_name);
}
}
The CONTROLLER (the controller is quite long so i just paste the part for the file upload field)
public function actionUpload()
{
$modelCompany = new CustomerStore();
if (Yii::$app->request->isPost) {
$modelCompany->file = UploadedFile::getInstance($modelCompany, 'file');
if ($modelCompany->validate()) {
$modelCompany->file->saveAs('/' . $modelCompany->file->baseName . '.' . $modelCompany->file->extension);
}
}
return $this->render('upload', ['model' => $modelCompany]);
}
I think you must to load post data to the model. You can try do like this
if ($modelCompany->load(Yii::$app->request->post())) {
$modelCompany->file = UploadedFile::getInstance($modelCompany, 'file');
if ($modelCompany->validate()) {
$modelCompany->file->saveAs('/' . $modelCompany->file->baseName . '.' . $modelCompany->file->extension);
}
}
I got stuck in a a problem in which I need to update some info from database in the same page that is shown.
On this case I'm fetching some "global settings" from a website in an index page which comes with a form in it. Here is a picture of it just to make it more clear to understand what I mean.
As you can see I created the button and I made it possible to see the values from the database, the problem is that I can not figure it out how to update it from there. Can somebody suggest an idea?
Here is my controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Settings extends Admin_Controller {
public function index()
{
$this->form_validation->set_rules('website_favicon', 'Favicon', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_logo', 'Logon', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_name', 'Website Name', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_credits', 'Credits', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_credits_link', 'Credits Link', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_copyright', 'Copyright', 'trim|required|min_length[4]');
if($this->form_validation->run() == FALSE){
// Get Current Subject
$data['item'] = $this->Settings_model->get_website_data();
//Load View Into Template
$this->template->load('admin', 'default', 'settings/index', $data);
} else {
// Create website settings
$data = array(
'website_favicon' => $this->input->post('website_favicon'),
'website_logo' => $this->input->post('website_logo'),
'website_name' => $this->input->post('webiste_name'),
'website_credits' => $this->input->post('website_credits'),
'website_credits_link' => $this->input->post('website_credits_link'),
'website_copyright' => $this->input->post('website_copyright'),
);
// Update User
$this->Settings_model->update($id, $data);
// Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'website settings',
'action' => 'updated',
'user_id' => $this->session->userdata('user_id'),
'message' => 'User (' . $data["username"] . ') updated the website settings'
);
// Add Activity
$this->Activity_model->add($data);
//Create Message
$this->session->set_flashdata('success', 'Website setting has been updated');
//Redirect to Users
redirect('admin/settings');
}
}
}
Here is my model:
<?php
class Settings_model extends CI_MODEL
{
function __construct()
{
parent::__construct();
$this->table = 'website_settings';
}
public function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update($this->table, $data);
}
public function get_website_data()
{
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('id', 1);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->row();
} else {
return false;
}
}
}
and here is my view(index.php) with the form:
<h2 class="page-header">Website Settings</h2>
<?php if($this->session->flashdata('success')) : ?>
<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-check"></i> Alert!</h4>
<?php echo $this->session->flashdata('success') ?></div>
<?php endif; ?>
<?php if($this->session->flashdata('error')) : ?>
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-ban"></i> Alert!</h4>
<?php echo $this->session->flashdata('error') ?></div>
<?php endif; ?>
<?php echo validation_errors('<p class="alert alert-danger">'); ?>
<?php echo form_open('admin/settings/index/'.$item->id); ?>
<!-- Website Favicon -->
<div class="form-group">
<?php echo form_label('Website Favicon', 'title'); ?>
<?php
$data = array(
'name' => 'website_favicon',
'id' => 'website_favicon',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_favicon
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Logo -->
<div class="form-group">
<?php echo form_label('Website Logo', 'website_logo'); ?>
<?php
$data = array(
'name' => 'website_logo',
'id' => 'website_logo',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_logo
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Name -->
<div class="form-group">
<?php echo form_label('Website Name', 'website_name'); ?>
<?php
$data = array(
'name' => 'website_name',
'id' => 'website_name',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_name
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Credits -->
<div class="form-group">
<?php echo form_label('Website Credits to', 'website_credits'); ?>
<?php
$data = array(
'name' => 'website_credits',
'id' => 'website_credits',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_credits
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Credits Link -->
<div class="form-group">
<?php echo form_label('Website Credits to Link', 'website_credits_link'); ?>
<?php
$data = array(
'name' => 'website_credits_link',
'id' => 'website_credits_link',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_credits_link
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Copyright -->
<div class="form-group">
<?php echo form_label('Copyrights', 'website_copyright'); ?>
<?php
$data = array(
'name' => 'website_copyright',
'id' => 'website_copyright',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_copyright
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website First Ad -->
<div class="form-group">
<?php echo form_label('Ad One', 'website_first_ad'); ?>
<?php
$data = array(
'name' => 'website_first_ad',
'id' => 'website_first_ad',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_first_ad
);
?>
<?php echo form_textarea($data); ?>
</div>
<!-- Website Second Ad -->
<div class="form-group">
<?php echo form_label('Ad Two', 'website_second_ad'); ?>
<?php
$data = array(
'name' => 'website_second_ad',
'id' => 'website_second_ad',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_second_ad
);
?>
<?php echo form_textarea($data); ?>
</div>
<!-- Website Third Ad -->
<div class="form-group">
<?php echo form_label('Ad Three', 'website_third_ad'); ?>
<?php
$data = array(
'name' => 'website_third_ad',
'id' => 'website_third_ad',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_third_ad
);
?>
<?php echo form_textarea($data); ?>
</div>
<?php echo form_submit('mysubmit', 'Update Website', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>
Thanks for helping.
Check if the data is posted thru the controller. then use set_value to your input fields to retain the values after submit
CONTROLLER
public function index(){
if($this->input->post()){
//set rules for form validation
if($this->form_validation->run() !== FALSE){
//then update
}
}
//your views, data or any other things you do
}
VIEW
echo form_input('name', set_value('name'));
On click of the button you can bind the ajax call to submit the data to the update action of the controller and you can handle response to show relevant message on the same page.
Sample ajax call
$.ajax({
url:'settings/update',//controller action
type:'POST',
dataType:'JSON',
data:{'data':data,'id':id},//form data you need to upate with the id
success:function(response) {
//show success message here
},
error:function(response) {
//show error message here
}
});
Hope this helps.
With Yii2, I'm trying to make a dropdownlist from a database (already done) but show the fields from the selected value.
Example :
Table :
Immeuble
Immeuble_id
Immeuble_name
Immeuble_propetary
Immeuble_color
This is my code :
Model :
class Imm extends ActiveRecord{
public static function tableName()
{
return 'i10';
}
public function attributeLabels(){
return array(
'N_NUME_IMME' => 'N_NUME_IMME',
);
}
}
Controller :
public function actionImm($id)
{
$model = new Imm(); // envoyer le model sur imm.php
$query = Imm::find(); // trouver le model
$model = $this->loadModel($id);
$imm = Imm::find()->where(['N_NUME_IMME' => $id])->one();
$items = ArrayHelper::map(Imm::find()->all(), 'N_NUME_IMME', 'L_NOM1_COMP');
return $this->render('imm',['imm'=>$model,'imm'=>$imm, 'items'=>$items]);
}
views :
<h1>Immeuble : </h1>
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'layout' => 'horizontal',
'fieldConfig' => [
'template' => "{label}\n<div class=\"col-lg-6\">{input}</div>\n<div class=\"col-lg-6\">{error}</div>",
'labelOptions' => ['class' => 'col-lg-1 control-label'],
],
]); ?>
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?= Html::activeDropDownList($imm, 'N_NUME_IMME',$items, ['class' => 'form-control', 'name' => 'liste_immeubles'])?>
</div>
</div>
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<?= Html::submitButton('Chaufferie', ['class' => 'btn btn-primary', 'name' => 'bouton_']) ?>
<?= Html::submitButton('Locaux communs', ['class' => 'btn btn-primary', 'name' => 'bouton_d']) ?>
<?= Html::submitButton('Parking/Amenagements Ext', ['class' => 'btn btn-primary', 'name' => 'bouton_donnees_generales']) ?>
<?= Html::submitButton('Facades/Toitures', ['class' => 'btn btn-primary', 'name' => 'bouton_don']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
<h2>Donnees Generales</h2>
<div class="col-lg-6">
<?= $form->field($imm, 'C_MODE_CHAU')->textInput(['autofocus' => false]) ?>
</div>
<div class="col-lg-6">
I want to create the custompasswordhasher for my cakephp project. the cakephp version is 2.5. I have follow the cakephp cook book and create the following custom class in directory Controller/Auth/CustomPasswordHasher.php
App::uses('AbstractPasswordHasher', 'Controller/Component/Auth');
class CustomPasswordHasher extends AbstractPasswordHasher {
public function hash($password) {
$hasher = md5(Configure::read('Security.salt') . $password . Configure::read('Security.cipherSeed'));
return $hasher;
}
public function check($password, $hashedPassword) {
//debug('PHPassHasher'); die('Using custom hasher'); //<--THIS NEVER HAPPENS!
$password = md5(Configure::read('Security.salt') . $password . Configure::read('Security.cipherSeed'));
echo $password."==".$hashedPassword;exit;
return password_verify($password, $hashedPassword);
}
}
and here is my login function in the controller
public function admin_login() {
if ($this->Auth->loggedIn()) {
return $this->redirect($this->Auth->redirect());
}
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Username or password is incorrect', 'error');
}
}
}
and in appController.php I config function
public function beforeFilter() {
if ($this->request->prefix == 'admin') {
$this->layout = 'admin';
AuthComponent::$sessionKey = 'Auth.User';
$this->Auth->loginAction = array('controller' => 'administrators', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'administrators', 'action' => 'dashboard');
$this->Auth->logoutRedirect = array('controller' => 'administrators', 'action' => 'login');
$this->Auth->authenticate = array(
'all' => array(
'scope' => array(
'User.is_active' => 1
)
),
'Form' => array(
'userModel' => 'User',
'passwordHasher' => array(
'className' => 'Auth/CustomPasswordHasher'
)
)
);
$this->Auth->allow('login');
} else {
/* do another stuff for user authentication */
}
}
And here is my login form.
<div class="login-box">
<div class="login-logo">Admin Login</div>
<div class="login-box-body">
<p class="login-box-msg">Sign in to start your session</p>
<?php echo $this->Session->flash(); ?>
<?php echo $this->Form->create(); ?>
<div class="form-group has-feedback">
<?php
echo $this->Form->input('User.username',
array(
'label' => false,
'class' => 'form-control',
'placeholder' => 'Username',
'autocomplete' => 'off',
'autofocus' => true,
'value' => #$username
)
);
?>
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<?php
echo $this->Form->input('User.password',
array(
'type' => 'password',
'label' => false,
'class' => 'form-control',
'placeholder' => 'Password',
'value' => #$password
)
);
?>
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck ">
<label>
<input type="checkbox" name="data[Admin][remember_me]"> Remember Me </label>
</div>
</div>
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button>
</div>
</div>
<?php echo $this->Form->end(); ?>
I forgot my password<br>
</div>
</div>
how every when submit form. I got the stack trace
So everyone can you help me with this?
I call wrong className in my controller . it should be "Custom" not "CustompasswordHasher". Cake will automatic add that.
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