I have a form for insert pages on the database,the trouble is that I can't detected in form if a URL is duplicated in my database,because I should to configure a callback function.The error is only on the callback function because I can insert,delete and create pages with this form.
This is my Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_Pages extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
/*$this->load->library('pagination');*/
$this->load->model('Create_Pages_Model');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
}
else
{
$this->load->view('view_error');
}
}
public function add_new_page()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$this->load->view('view_admin_content_panel', $data);
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Create_Pages_Model');
$data_pages = array(
'TitlePage' => $this->input->post('TitlePage'),
'SectionPage' => $this->input->post('SectionPage'),
'CategoryPage' => $this->input->post('CategoryPage'),
'NamePage' => $this->input->post('NamePage'),
'DescriptionPage' => $this->input->post('DescriptionPage'),
'BodyPage' => $this->input->post('BodyPage')
);
if($this->input->post())
{
$this->form_validation->set_rules('TitlePage','Title of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('SectionPage','Section of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('CategoryPage','Category of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('NamePage','Name of the Page','required|min_length[5]|callback_check_namepage[$str]|xss_clean|url_title');
$this->form_validation->set_rules('DescriptionPage','Description of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('BodyPage','Content of the Page','required|min_length[5]');
if($this->form_validation->run() == TRUE)
{
$page_inserted = $this->Create_Pages_Model->addPage();
redirect('dashboard', 'refresh');
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
function check_namepage($str)
{
$PageName = $this->db->where('NamePage', $NamePage);
$query = $this->db->select('pages', array('NamePage' => $NamePage));
if($query == $PageName){
$this->form_validation->set_message('check_namepage', '%Should be Unique Name Page because we can\'t duplicate the URL of the page');
return FALSE;
}
elseif($NamePage != $query)
{
return TRUE;
}
}
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
}
else
{
$this->load->view('view_error');
}
}
}
This is my model code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_Pages_Model extends CI_Model {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('view_error');
}
public function addPage(){
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$this->load->database();
$add_page = $this->input->post();
unset($add_page['insert']);
$this->db->insert('pages',$add_page);
return $this->db->insert_id();
}
else
{
$this->load->view('view_error');
}
}
}
And this this is my my view page,for insert the pages into the database:
<body>
<div id="main">
<div class="container">
<div class="page-header">
<h1>Create a New Page</h1>
</div>
<div class="row">
<div class="col-sm-12">
<p>Please enter your details below to form.</p>
<?php
$title_page = array(
'name' => 'TitlePage',
'id' => 'TitlePage',
'maxlength' => '490',
'size' => '90',
'type' => 'text',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('TitlePage'),
);
$section_page = array(
'name' => 'SectionPage',
'id' => 'SectionPage',
'maxlength' => '390',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('SectionPage'),
);
$category_page = array(
'name' => 'CategoryPage',
'id' => 'CategoryPage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('CategoryPage'),
);
$name_page = array(
'name' => 'NamePage',
'id' => 'NamePage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('NamePage'),
);
$description_page = array(
'name' => 'DescriptionPage',
'id' => 'DescriptionPage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:40px;width:100%;',
'value' => set_value('DescriptionPage'),
);
$body_page = array(
'name' => 'BodyPage',
'id' => 'BodyPage',
'maxlength' => '290',
'type' => 'text',
'size' => '90',
'style' => 'position:relative;min-height:320px;width:100%;',
'value' => set_value('BodyPage'),
'rows' => '15',
'cols' => '80',
);
?><?php echo form_open();?>
<?php echo form_label('Title of the Page:','lbl_titlepage');?><br/>
<?php echo form_input($title_page);?><?php echo form_error('TitlePage'); ?><br/>
<?php echo form_label('Section of the Page:','lbl_sectionpage');?><br/>
<?php echo form_input($section_page);?><?php echo form_error('SectionPage'); ?><br/>
<?php echo form_label('Category of the Page:','lbl_categorypage');?><br/>
<?php echo form_input($category_page);?><?php echo form_error('CategoryPage'); ?><br/>
<?php echo form_label('Name of the Page (URL):','lbl_namepage');?><br/>
<?php echo form_input($name_page);?><?php echo form_error('NamePage'); ?><br/>
<?php echo form_label('Description of the Page:','lbl_descriptionpage');?><br/>
<?php echo form_input($description_page);?><?php echo form_error('DescriptionPage'); ?><br/>
<?php echo form_label('Contentent of the Page:','lbl_BodyPage');?><br/>
<?php echo form_textarea($body_page);?><?php echo form_error('BodyPage'); ?><br/><br/>
<?php echo form_submit('insert','Create Page')?><br/>
<?php echo form_close();?>
</div>
</div>
</div>
</div>
</body>
</html>
And this is my database:
CREATE TABLE `pages` (
`ID_Page` int(11) NOT NULL,
`TitlePage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`SectionPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`CategoryPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`NamePage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`BodyPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL,
`DescriptionPage` varchar(255) COLLATE utf8_spanish_ci NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
ALTER TABLE `pages`
ADD PRIMARY KEY (`ID_Page`);
ALTER TABLE `pages`
MODIFY `ID_Page` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=10;
Move check_namepage() method outside of add_new_page() method.
and you do not need to pass $str in callback_check_namepage[$str]. It should be just callback_check_namepage because CI will automatically pass the user provided field data to the callback function. So, in the callback check_namepage($str), $str will be available automatically.
So, your Controller will be like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Create_Pages extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
/*$this->load->library('pagination');*/
$this->load->model('Create_Pages_Model');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
}
else
{
$this->load->view('view_error');
}
}
public function add_new_page()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['Username'] = $session_data['Username'];
$this->load->view('view_admin_content_panel', $data);
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Create_Pages_Model');
$data_pages = array(
'TitlePage' => $this->input->post('TitlePage'),
'SectionPage' => $this->input->post('SectionPage'),
'CategoryPage' => $this->input->post('CategoryPage'),
'NamePage' => $this->input->post('NamePage'),
'DescriptionPage' => $this->input->post('DescriptionPage'),
'BodyPage' => $this->input->post('BodyPage')
);
if($this->input->post())
{
$this->form_validation->set_rules('TitlePage','Title of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('SectionPage','Section of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('CategoryPage','Category of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('NamePage','Name of the Page','required|min_length[5]|callback_check_namepage|xss_clean|url_title'); // [$str] is removed
$this->form_validation->set_rules('DescriptionPage','Description of the Page','required|min_length[5]|xss_clean');
$this->form_validation->set_rules('BodyPage','Content of the Page','required|min_length[5]');
if($this->form_validation->run() == TRUE)
{
// moved below
$page_inserted = $this->Create_Pages_Model->addPage();
redirect('dashboard', 'refresh');
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
}
else
{
$this->load->view('admin/user/add_new_page_view');
}
}
else
{
$this->load->view('view_error');
}
}
private function check_namepage($str)
{
// you have error here, solve it
$PageName = $this->db->where('NamePage', $NamePage);
$query = $this->db->select('pages', array('NamePage' => $NamePage));
if($query == $PageName){
$this->form_validation->set_message('check_namepage', '%Should be Unique Name Page because we can\'t duplicate the URL of the page');
return FALSE;
}
elseif($NamePage != $query)
{
return TRUE;
}
}
}
Related
public function set_news($id = 0)
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$name=$_FILES["myimage"]["name"];
$folder="./uploads/";
$imageFileType = strtolower(pathinfo($name,PATHINFO_EXTENSION));
$extensions_arr = array("jpg","jpeg","png");
if( in_array($imageFileType, $extensions_arr) )
{
$data = array(
'title' => $this->input->post('title'), // $this->db->escape($this->input->post('title'))
'date' => $this->input->post('date'),
'slug' => $slug,
'text' => $this->input->post('text'),
'myimage' => $name,
'user_id' => $this->input->post('user_id'),
);
move_uploaded_file($_FILES["myimage"]["tmp_name"], "$folder".$_FILES["myimage"]["name"]);
}
// else
// {
// echo "<script>alert('Image Error');window.history.back(); </script>";
// }
if ($id == 0) {
//$this->db->query('YOUR QUERY HERE');
return $this->db->insert('news', $data);
} else {
$this->db->where('id', $id);
return $this->db->update('news', $data);
}
}
this is my controller file
on uploading image it showing error as follows
A PHP Error was encountered
Severity: Notice
Message: Undefined index: myimage
Filename: models/News_model.php
Line Number: 48
Backtrace:
File: C:\xampp\htdocs\web\codeigniter\application\models\News_model.php
Line: 48
Function: _error_handler
File: C:\xampp\htdocs\web\codeigniter\application\controllers\News.php
Line: 123
Function: set_news
File: C:\xampp\htdocs\web\codeigniter\index.php
Line: 315
Function: require_once
Seems you missed to set the input name as "myimage". Here is the complete codeigniter image upload code(As Mention on their Documentation).
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
Controller Code:
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$uploadData= array(
'title' => TITLE,
'date' => date("Y-m-d H:i:s"),
'slug' => SLUG,
'text' => TEXT,
'myimage' => FILE_NAME,
'user_id' => USER_ID,
);
$this->db->insert('mytable', $uploadData);
$this->load->view('upload_success', $data);
}
}
}
Controller I have form_ctrl code is below
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class form_ctrl extends CI_Controller {
public function index()
{
//$this->load->view('welcome_message');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//$this->form_validation->set_rules('name', 'Username', 'required');
$this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobile', 'Mobile', 'required');
$this->form_validation->set_rules('address', 'Address','required|min_length[5]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('table');
}
else
{
$this->load->view('results');
$name=$this->input->post('name');
$pass=$this->input->post('pass');
$email=$this->input->post('email');
$mobile=$this->input->post('mobile');
$address=$this->input->post('address');
$data = array(
'name' =>$name ,
'pass' => $pass,
'email' => $email,
'mobile' => $mobile,
'address' => $address
);
$this->db->insert('form', $data);
}
}
}
View I have result.php code
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open(); ?>
<table >
<tr>
<td colspan=2 align="center"><h3>User Details</h3></td>
</tr>
<tr>
<td>
<?php echo form_label('Name'); ?>
</td>
<td>
<?php echo form_input(array('id' => 'name', 'name' => 'name')); ?>
</td>
</tr>
<tr>
<td>
<?php echo form_label('Pass'); ?>
</td>
<td>
<?php echo form_password(array('id' => 'pass', 'name' => 'pass')); ?>
</td>
</tr>
<tr>
<td><?php echo form_label('Email'); ?>
</td>
<td><?php echo form_input(array('id' => 'email', 'name' => 'email')); ?></td>
</tr>
<tr>
<td><?php echo form_label('Mobile'); ?>
</td>
<td><?php echo form_input(array('id' => 'mobile', 'name' => 'mobile')); ?>
</td>
</tr>
<tr>
<td><?php echo form_label('Address'); ?>
</td>
<td><?php echo form_input(array('id' => 'address', 'name' => 'address')); ?>
</td>
</tr>
<tr>
<td colspan="2" align="center"><?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
</td>
</tr>
<?php echo form_close(); ?>
</table>
</body>
</html>
In this code I want to include model to insert the data instead of controller.
The code is working properly for insert data into database but I want this through model not from controller. I tried so many times but I didn't get the desirable result.
commonModel.php
class CommonModel extends CI_Model {
function __construct() {
parent::__construct ();
}
public function insert($tableName,$data){
return $this->db->insert($tableName, $data);
}
}
replace your controller code like this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class form_ctrl extends CI_Controller {
public function index()
{
//$this->load->view('welcome_message');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('commonModel');
//$this->form_validation->set_rules('name', 'Username', 'required');
$this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobile', 'Mobile', 'required');
$this->form_validation->set_rules('address', 'Address','required|min_length[5]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('table');
}
else
{
$this->load->view('results');
$name=$this->input->post('name');
$pass=$this->input->post('pass');
$email=$this->input->post('email');
$mobile=$this->input->post('mobile');
$address=$this->input->post('address');
$data = array(
'name' =>$name ,
'pass' => $pass,
'email' => $email,
'mobile' => $mobile,
'address' => $address
);
$this->commonModel->insert('form', $data);
}
}
}
class form_ctrl extends CI_Controller {
public function index()
{
//$this->load->view('welcome_message');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//$this->form_validation->set_rules('name', 'Username', 'required');
$this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobile', 'Mobile', 'required');
$this->form_validation->set_rules('address', 'Address','required|min_length[5]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('table');
}
else
{
$this->load->view('results');
$name=$this->input->post('name');
$pass=$this->input->post('pass');
$email=$this->input->post('email');
$mobile=$this->input->post('mobile');
$address=$this->input->post('address');
$data = array(
'name' =>$name ,
'pass' => $pass,
'email' => $email,
'mobile' => $mobile,
'address' => $address
);
$this->load->model ( 'user_model' );
$this->user_model->insert('form', $data);
}
}
}
model
class User_model extends CI_Model
{
public function insert($table,$data)
{
$this->db->insert ( $table, $data );
}
}
class form_ctrl extends CI_Controller {
public function index(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobile', 'Mobile', 'required');
$this->form_validation->set_rules('address', 'Address','required|min_length[5]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('table');
}
else
{
$data = $this->input->post();
$this->load->view('results',$data);
$this->load->model ( 'user_model' );
$this->user_model->insert('form', $this->input->post());
}
}
}
and your model looks like below.
public function insert($table, $data) {
$param = array(
'name' => $data['name'],
'pass' => $data['pass'],
'email' => $data['email'],
'mobile' => $data['mobile'],
'address' => $data['address']
);
$this->db->insert($table, $param);
}
It's always best practices that your controller part will be light weight and have less code.
**controller**
$postData = $_POST;
$result = $this->batch->addBatch($postData);
**Batch Model**
class Batch_model extends MY_Model {
public function __construct() {
parent::__construct();
}
function addBatch($postData) {
$this->_table = TBL_BATCH;
$result = $this->add($postData);
return $result;
}
}
**My Model**
public $_table;
public $_fields;
public $_where;
protected $_except_fields = array();
protected $soft_delete = TRUE;
function add($PostData) {
$postArray = $this->getDatabseFields($PostData);
$query = $this->db->insert($this->_table, $postArray);
if ($this->db->affected_rows() > 0)
return $this->db->insert_id();
else
return '';
}
protected function getDatabseFields($postData, $tableName = '') {
if (empty($tableName))
$tableName = $this->_table;
$table_fields = $this->getFields($tableName);
$final = array_intersect_key($postData, $table_fields);
return $final;
}
Controller I have form_ctrl code is below
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class form_ctrl extends CI_Controller {
public function index()
{
//$this->load->view('welcome_message');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//$this->form_validation->set_rules('name', 'Username', 'required');
$this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('mobile', 'Mobile', 'required');
$this->form_validation->set_rules('address', 'Address','required|min_length[5]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('table');
}
else
{
$this->load->view('results');
$name=$this->input->post('name');
$pass=$this->input->post('pass');
$email=$this->input->post('email');
$mobile=$this->input->post('mobile');
$address=$this->input->post('address');
$data = array(
'name' =>$name ,
'pass' => $pass,
'email' => $email,
'mobile' => $mobile,
'address' => $address
);
$this->your_model->insert_data($data)
}
}
}
Here is your model your_model..........
class your_model extends CI_Model
{
function insert_data($data) {
$this->db->insert('your_table', $data);
}
}
** You must load your_model to controller or in autoload
I have a same field in foreach loop like below
foreach ( $subCategoryData as $k => $val) {
<?= $form->field($model, 'sub_category', ['template' => '{input}'])->textInput(['maxlength' => 255, 'class' => 'form-control required section_name', 'name' => "Category[sub_category][$k][name]"]) ?>
} ?>
I have ajax validation with custom method it is working fine.
But it is Working with only first input. Because it has same ID.
But when I changed it with 'inputOptions' => ['id' => 'myCustomId'] and make it unique with below and my ajax validation is not called.
foreach ( $subCategoryData as $k => $val) {
<?= $form->field($model, 'sub_category', ['template' => '{input}','inputOptions' => ['id' => "category-sub_category_".$k]])->textInput(['maxlength' => 255, 'class' => 'form-control required section_name', 'name' => "Category[sub_category][$k][name]"]) ?>
}
I have seen this solution here
https://github.com/yiisoft/yii2/issues/7627
and also seen this https://stackoverflow.com/a/28460442/2286537
But nothing work
can anyone help me ?
Your question is different from the posts you introduced.
You should use loadMultiple.
Example:
if (\Yii::$app->request->isAjax) {
if (\yii\base\Model::loadMultiple($model,\Yii::$app->request->post())) {
\Yii::$app->response->format = Response::FORMAT_JSON;
echo json_encode(ActiveForm::validateMultiple($model));
\Yii::$app->end();
}
}
if ( \yii\base\Model::loadMultiple($model, Yii::$app->request->post()) && \yii\base\Model::validateMultiple($model)) {
foreach ($model as $models) {
$models->save(false);
}
in view:
<?php $form = ActiveForm::begin([
'enableAjaxValidation' => true,
]);
i create image module and i edit image more then 1mb then can not show errormsg.
i used codigniter fremwork.
controller:
public function edit($id) {
$this->edit_status_check($id);
$this->form_validation->set_rules('agent_name', 'Agent Name', 'required');
$this->form_validation->set_rules('mobile', 'Mobile No.', 'required');
$this->form_validation->set_rules('agent_vehicle', 'Agent Vehicle', 'required');
if ($this->form_validation->run() == FALSE) {
$data = array(
'page_title' => 'Edit Agent',
'page_name' => 'agent/edit',
'result' => $this->agent_model->select_id($id),
'result_vehicle' => $this->vehicle_model->list_all(),
'error' => validation_errors(),
'id' => $id
);
$this->load->view('template', $data);
} else {
$config['upload_path'] = '../uploads/agent/';
$config['allowed_types'] = 'jpg|jpeg';
$config['encrypt_name'] = TRUE;
$config['max_size'] = 1000; // 1 mb
$this->load->library('upload', $config);
if (!empty($_FILES['agent_image']['name'])) {
if ($this->upload->do_upload('agent_image')) {
$_POST['agent_img_url'] = 'uploads/agent/' . $this->upload->data('file_name');
} else {
$data = array(
'page_title' => 'Edit Agent',
'page_name' => 'agent/edit',
'result' => $this->agent_model->select_id($id),
'result_vehicle' => $this->vehicle_model->list_all(),
'error' => $this->upload->display_errors(),
'id' => $id
);
$this->load->view('template', $data);
}
}
$this->agent_model->update($_POST, $id);
alert('Update', $_POST['agent_name']);
redirect('agent');
}
}
Model:
public function update($data, $id) {
$updatedata = array(
'name' => $data['agent_name'],
'mobile' => $data['mobile'],
'password' => sha1($data['password']),
'vehicle' => $data['agent_vehicle'],
'address' => $data['agent_address'],
'category' => $data['category'],
'created_on' => date('Y-m-d h:i:sa')
);
if (!empty($data['agent_img_url'])) {
$updatedata['img_url'] = $data['agent_img_url'];
}
$this->db->where('id', $id);
$this->db->update('agent', $updatedata);
}
View:
<div class="form-group">
<img src="/<?= $result['img_url']; ?>" class="img-responsive" name="old_agent_image" width="133" height="100">
</div>
<div class="form-group">
<label>Agent Image</label>
<input type="file" name="agent_image">
</div>
MY question: I edit image for particular user then image uploaded,but if image size more then 1mb ,then image can not upload and display error message.
so my question how to show errormsg.
$uploaded = $this->upload->do_upload('file'); //'file' is input field name
if($uploaded) {
$upload_data = $this->upload->data();
// do database stuff
} else {
$data['errors'] = array("error" => $this->upload->display_errors());
}
I’ve a problem with implementing recaptcha in a CodeIgniter application.
The problem is that the recapctha_challenge_field and recaptcha_response_field do not get posted, however, the recapctcha (and those fields) is visible on the page (within the form).
The the recapctha_challenge_field and recaptcha_response_field are appearing in the HTML of the page, but not in the header when I post the form.
I’ve downloaded de recaptcha library and added it as an helper in CI.
Within the form in my view I echo the recaptcha_get_html($publickey) (with the public key set).
In my controller, I load the recaptchalib_helper and add set a form validation rule for the recapctha_challenge_field.
This is my view:
<h1>Register</h1>
<fieldset>
<legend>Personal information</legend>
<?php
echo form_open('login/create_user');
echo form_label('First name:', 'first_name');
echo form_input(
array(
'name' => 'first_name',
'id' => 'first_name',
'value' => set_value('first_name')
)
);
echo form_label('Last name:', 'last_name');
echo form_input(
array(
'name' => 'last_name',
'id' => 'last_name',
'value' => set_value('last_name')
)
);
echo form_label('Birth date:', 'birth_date');
echo form_input(
array(
'name' => 'birth_date',
'id' => 'birth_date',
'value' => set_value('birth_date')
)
);
echo form_label('E-mail:', 'email');
echo form_input(
array(
'name' => 'email',
'id' => 'email',
'value' => set_value('email')
)
);
?>
</fieldset>
<fieldset>
<legend>Login information</legend>
<?php
echo form_label('Username:', 'username');
echo form_input(
array(
'name' => 'username',
'id' => 'username',
'value' => set_value('username')
)
);
echo form_label('Password:', 'password1');
echo form_password(
array(
'name' => 'password1',
'id' => 'password1',
'value' => set_value('password1')
)
);
echo form_label('Confirm password:', 'password2');
echo form_password(
array(
'name' => 'password2',
'id' => 'password2',
'value' => set_value('password2')
)
);
$publickey = "mypublickey"; // here I entered my public key
echo recaptcha_get_html($publickey);
echo form_label(nbs(1), 'submit');
echo form_submit(
array(
'name' => 'submit',
'id' => 'submit',
'value' => 'Registreren'
)
);
echo form_close();
?>
<?php echo validation_errors('<p class="error">'); ?>
</fieldset>
</div>
and this is a part of my controller:
function create_user() {
print_r($_POST);//for debugging
$this->load->library('form_validation');
$this->load->model('user');
$this->form_validation->set_rules('recaptcha_challenge_field', 'Captcha', 'callback_validate_captcha');
$this->form_validation->set_rules('first_name', 'First name', 'trim|xss_clean|required');
$this->form_validation->set_rules('last_name', 'Last name', 'trim|xss_clean|required');
$this->form_validation->set_rules('email', 'E-mail', 'trim|xss_clean|valid_email|callback_is_email_available|required');
$this->form_validation->set_rules('username', 'Username', 'trim|xss_clean|min_length[5]|callback_is_username_available|required');
$this->form_validation->set_rules('password1', 'Password', 'trim|xss_clean|min_length[8]|max_length[32]|required');
$this->form_validation->set_rules('password2', 'Confirm password', 'trim|xss_clean|matches[password1]|required');
if ($this->form_validation->run() == FALSE) {
$this->signup();
} else {
if ($this->user->create_user($this->input->post('username'),$this->input->post('password1'),$this->input->post('email'),$this->input->post('first_name'),$this->input->post('last_name'),$this->input->post('birth_date'))) {
$data['main_content'] = 'login/signup_successful';
$this->load->view('includes/template', $data);
} else {
$this->load->view('login/signup_form');
}
}
}
public function validate_captcha($recaptcha_challenge_field) {
$this->load->helper('recaptchalib');
$privatekey = "myprivatekey";//this is et to my private key
$resp = recaptcha_check_answer ($privatekey,
$this->input->ip_address(),
$this->input->post("recaptcha_challenge_field"),
$this->input->post("recaptcha_response_field"));
if (!$resp->is_valid) {
$this->form_validation->set_message('validate_captcha', 'Invalid Capctha code entered.');
return FALSE;
} else {
return TRUE;
}
}
The capctha fields are not set in the HTTP headers:
Form data:
csrf_test_name:8cc3f2391784867df2d46f193a65a317
first_name:Myfirstname
last_name:Mylastname
birth_date:10-12-2012
email:myemail#adres.com
username:username
password1:password
password2:password
submit:Register
What am I doing wrong?
Your sincerely,
Alwin
Does the form tag not begin within a table, tbody or tr?