form validation not working in CodeIgniter - php

I'm not able to run the form validation in CodeIgniter. It is not returning any errors or any submission to the database.
When i'm using
$this->form_validation->set_rules('subemail', 'Email', 'required|valid_email');
My code gets executed but not in below case. Why?
Controller name Form_sub :
class Form_sub extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->database();
$this->load->library('form_validation');
#loading model
$this->load->model('form_model');
$this->load->helper(array("form",'url'));
}#EOF CONSTRUCTOR
public function index(){
$this->load->view('form.php');
}//default page
public function subscribe(){
echo "hello1";
$config = array(
'field' => 'email',
'label' => 'email id',
'rules' => 'required|max_length[50]|min_length[5]|valid_email|trim|htmlspecialchars',
'errors'=>[
'required' => '* You must provide an %s',
'max_length' => '* %s should be of maximum 50 characters',
'min_length' => '* %s should be of minimum 5 characters',
'valid_email' => '* You must provide a valid %s'
]
)
$this->form_validation->set_rules($config);
echo "hello2";
if($this->form_validation->run() == false){
$this->load->view('form.php');
echo "hello3";
}
View name form.php :
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-8 col-lg-8">
<?php echo form_open('form_sub/subscribe'); ?>
<input name="email" id="" type="text" value="<?php echo set_value('email');?>" class="normal" placeholder="Email Address">
<?php echo form_error('email'); ?>
</div>
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4 button">
<!-- <input type="submit" id="subsubmit" name="send" class="button" value="Subscribe">-->
<input type="submit" class="button" value="Subscribe">
<?php echo form_close(); ?>
</div>
</div>
Model name form_model :
class form_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function subscribe_insert($data)
{
if($this->db->insert('subscribe',$data))
{
return true;
}
else
{
echo "something went wrong in the database";
}
}
}

Related

Codeigniter (Inserting category data into table)

I am new to CodeIgniter, I carried out an e-commerce project left by the former developer. The case is that the category data is not inserting into my table.
The code is very long in both controller and model but I cut it out and posted only the necessary part of it.
This is my controller.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Category extends Admin_Controller {
public function create()
{
/* Breadcrumbs */
$this->breadcrumbs->unshift(2, "New Category" , 'admin/category/create');
$this->data['breadcrumb'] = $this->breadcrumbs->show();
/* Variables */
$tables = $this->config->item('tables', 'ion_auth');
/* Validate form input */
$this->form_validation->set_rules('cat_name', 'Category Name', 'trim|required');
if ($this->form_validation->run() == TRUE)
{
$config['upload_path'] = './assets/uploads/category/';
//die(var_dump(is_dir($config['upload_path'])));
$config['allowed_types'] = 'png,jpeg';
$config['max_size'] = '1024';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$img = "icon";
if ( ! $this->upload->do_upload($img))
{
$this->session->set_flashdata('error', $this->upload->display_errors());
redirect('admin/category');
}
else
{
$data=$this->upload->data();
$file = array('file_name' => $data['file_name'] );
$data = array('upload_data' => $this->upload->data());
$photo = base_url().'assets/uploads/category/'.$file['file_name'];
$data = array(
'category_name' => $this->input->post('cat_name'),
'category_photo' => $photo,
'category_description' => $this->input->post('cat_desc')
);
$this->category_model->insertcategory($data);
//$this->ion_auth->messages()
$this->session->set_flashdata('message', "Successfully inserted!");
redirect('admin/category', 'refresh');
}
}
else
{
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
/* Load Template */
$this->template->admin_render('admin/category/create', $this->data);
}
}
This is my model.
class Category_model extends CI_Model
{
function insertcategory($data) {
$query = $this->db->insert('category', $data);
if ($query) {
return true;
} else {
return false;
}
}
This is my form.
<div class="box-body">
<span style="color:red"><?php echo $message;?></span>
<?php echo form_open_multipart(current_url(), array('class' => 'form-horizontal', 'id' => 'form-create_user')); ?>
<div class="form-group">
<span class="col-sm-2 control-label">Category Name</span>
<div class="col-sm-10">
<input type="text" class="form-control" id="cat_name" placeholder="Category Name" name="cat_name" required>
</div>
</div>
<div class="form-group">
<span class="col-sm-2 control-label">Category Description</span>
<div class="col-sm-10">
<input type="text" class="form-control" id="cat_desc" placeholder="Description" name="cat_desc" >
</div>
</div>
<div class="form-group">
<span class="col-sm-2 control-label">Category Icon</span>
<div class="col-sm-10">
<input class="input-file uniform_on" id="icon" name="icon" type="file">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="btn-group">
<?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-primary btn-flat', 'content' => lang('actions_submit'))); ?>
<?php echo form_button(array('type' => 'reset', 'class' => 'btn btn-warning btn-flat', 'content' => lang('actions_reset'))); ?>
<?php echo anchor('admin/category', lang('actions_cancel'), array('class' => 'btn btn-default btn-flat')); ?>
</div>
</div>
</div>
<?php echo form_close();?>
</div>
Could you please replace $img = "icon"; with $img = $this->input->post('icon');
Please check with the above data.
Also please post the error message you are getting.

Codeigniter Login Issues

I'm having difficulties in understanding what's going on here.
I've made a login page, setting it to go to admin page. Without any user data yet, just to check if it's working. And It doesn't go. I hade some problems loading the library form_validation. So I added the parent construct.
Controller
class Login Extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index() {
$this->load->view('login');
$this->load->helper('url');
}
public function login() {
$this->load->library('form_validation');
$this->load->helper('url');
$this->form_validation->set_rules('username','Username','trim|required');
$this->form_validation->set_rules('password','Password','trim|required|md5');
if($this->form_validation->run()==false){
$this->index();
}else{
$user_session=array(
'Username' => $this->input->post('username'),
'Password' => $this->input->post('password'),
'is_logged_in' => 1
);
$this->session->set_userdata($user_session);
redirect('login/admin');
}
}
public function admin() {
$this->load->view('admin');
}
}
My Login View
<section class="login_content">
<?php echo validation_errors(); ?>
<form action="<?php echo base_url().'login/login'; ?>" method="post">
<h1>Login no Sistema</h1>
<div>
<input type="text" name="username" class="form-control" placeholder="Username" required="" />
</div>
<div>
<input type="password" name="password" class="form-control" placeholder="Password" required="" />
</div>
<div>
<input type="submit" name="submit" value="Login" />
</div>
<div class="clearfix"></div>
<div class="separator">
<div class="clearfix"></div>
<br />
<div>
<h1> Sitio Monica e Marcia</h1>
<p>©2016 Todos os direitos reservados. Sitio Monica e Marcia.</p>
</div>
</div>
</form>
<!-- form -->
</section>
My Admin View
<div id="login" class="animate form">
<section class="login_content">
<h1>Bem vindo ao Admin</h1>
<?php
echo '<pre>';
print_r($this->session->all_userdata());
echo '<pre>';
?>
<!-- form -->
</section>
<!-- content -->
</div>
Here Is Your Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Description: Login controller class*/
class Login extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->library('session');
$this->load->model('login_model');
}
public function admin($msg = NULL){
// Load our view to be displayed
// to the user
$data['msg'] = $msg;
if($msg == NULL)
{
$this->load->view('login');
}
else
{
//print_r($data);
//die();
$this->load->view('login',$data);
}
}
public function process(){
// Load the model
// Validate the user can login
$result = $this->login_model->validate();
// Now we verify the result
if(! $result){
// If user did not validate, then show them login page again
$msg = 'Invalid username or password';
$this->admin($msg);
}else{
// If user did validate,
// Send them to members area
redirect('home/check_isvalidated');
}
}
public function doLogout(){
$this->session->sess_destroy();
redirect(base_url());
}
}
Here IS The Model You Can Use Validation instead of Security
public function validate(){
// grab user input
$username = $this->security->xss_clean($this->input->post('user_name'));
$password = $this->security->xss_clean(md5($this->input->post('password')));
// Prep the query
$this->db->where('user_name', $username);
$this->db->where('password', $password);
// Run the query
$query = $this->db->get('admin');
// Let's check if there are any results
if($query->num_rows == 1)
{
// If there is a user, then create session data
$row = $query->row();
$data = array(
'id' => $row->id,
'user_name' => $row->user_name,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
// If the previous process did not validate
// then return false.
else {
return false;
}
}
}
Try this
redirect(base_url('login/admin'));
I think, you should check your link and session. This is the code
login.php in controller
public function __construct() {
parent::__construct();
$this->load->helper('url');
}
public function index() {
//$this->load->view('login');
$this->load->view('login');
}
public function login() {
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper('url');
$this->form_validation->set_rules('username','Username','trim|required');
$this->form_validation->set_rules('password','Password','trim|required|md5');
if($this->form_validation->run()==false){
$this->index();
}else{
$user_session=array(
'Username' => $this->input->post('username'),
'Password' => $this->input->post('password'),
'is_logged_in' => 1
);
$this->session->set_userdata('userlogin',$user_session);
$this->admin();
}
}
public function admin() {
$this->load->view('admin');
}
login.php in view
<form class="form-horizontal" role="form" action="<?php echo 'http://localhost/answers/index.php/login/login'; ?>" method="post">
<div class="form-group">
<label class="control-label col-sm-2" for="username">Username:</label>
<div class="col-sm-10">
<input type="username" class="form-control" id="username" name="username" placeholder="Enter username">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="pwd">Password:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password" name="password" placeholder="Enter password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Login</button>
</div>
</div>
</form>
And admin.php in views
<div class="container" height=100%>
<h2>Welcome to the system,
<?php
$this->load->library('session');
$login_session = $this->session->userdata('userlogin');
//$username = $this->session->userdata('userlogin');
echo $login_session['Username'];
?>
</h2>
</div>
May you want to see the complete code like http://explicitphp.blogspot.co.id/2016/03/Codeigniter-Login-System-Tutorial-Without-Database-Connection.html
Hope answer your question. Have fun guys
first, i don't know if the Login.php file or the class Login extends CI_Controller is a reserved name, try changing it.
then you have a public function called login(), this can have problems with the constructor, because in php5 or above you call the constructor like the class name and you maybe override the constructor

Codeigniter: redirect page to user profile after update

I want to show an updated profile page of users on my site. The update on database is actually working but i want my userprofile page to show the updated datas once it clicked the submit button. What will I add here D:
MemberLoginController.php (controller)
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MemberLoginController extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('MemberLoginModel');
}
public function home(){
$this->load->view('pages/index');
}
public function userprofile(){
$this->load->view('member/userprofile');
}
public function useredit(){
$this->load->view('member/useredit');
}
public function memberlogin(){
$this->form_validation->set_error_delimiters('<p class=error>','</p>');
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_validate_credentials');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
if($this->form_validation->run()){
// Perform Actions after getting valid form inputs
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => 1
);
$this->session->set_userdata($data);
redirect('index.php/MemberLoginController/members');
}else
$this->load->view('pages/index');
}
public function members(){
if($this->session->userdata('is_logged_in')){
$vis = "hidden";
$id = $this->session->userdata('id');
$this->load->model('MemberLoginModel');
$memberinfo['memberinfo']=$this->MemberLoginModel->getMember($id);
$this->load->view('member/userprofile',$memberinfo);
}else{
redirect('index.php/HomeController/home');
}
}
public function edit($id){
$data = array(
"action" => base_url('/index.php/MemberLoginController/update/'.$id),
"data" => $this->db->get_where('member',array('id'=>$id))
);
$this->load->view('member/useredit', $data);
}
public function update($id){
$data = array(
'memberfname'=> $this->input->post('memberfname'),
'memberlname'=> $this->input->post('memberlname'),
'email'=>$this->input->post('email'),
);
$this->db->update('member',$data,array('id'=> $id));
redirect('index.php/MemberLoginController/getMember/'.$id);
}
public function getMember(){
$this->load->model('MemberLoginModel');
$memberinfo['memberinfo']=$this->MemberLoginModel->getMember();
$this->load->view('member/userprofile',$memberinfo);
}
public function validate_credentials(){
$this->load->model('MemberLoginModel');
if($this->MemberLoginModel->login()){
echo ("<SCRIPT LANGUAGE = 'JavaScript'>
window.alert('Login Successfully')
window.location.href='userprofile'
</SCRIPT>");
exit();
return true;
}else{
echo ("<SCRIPT LANGUAGE = 'JavaScript'>
window.alert('Invalid username or password. Please click LOGIN again.')
window.location.href='home'
</SCRIPT>");
exit();
//$this->form_validation- >set_message('validate_credentials','Incorrect Email/Password');
return false;
}
}
}
?>
MemberLoginModel.php
<?php class MemberLoginModel extends CI_Model{
public function __construct()
{
parent::__construct();
}
function login()
{
$this->db->where('email',$this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('member'); /*i added 'member' table on db new members*/
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
'id' => $rows->id,
'memberfname' => $rows->memberfname,
'memberlname' => $rows->memberlname,
'email' => $rows->email,
'logged_in' => TRUE
);
}
$this->session->set_userdata($newdata);
return true;
}else{
return false;
}
}
public function add_user()
{
$data = array(
'memberfname'=>$this->input->post('memberfname'),
'memberlname'=>$this->input->post('memberlname'),
'email'=>$this->input->post('email'),
'password'=>md5($this->input->post('password')),
);
$this->db->insert('member',$data);
}
public function getMember()
{
$query=$this->db->get('member');
return $query->result();
}
}
?>
Userprofile.php (view)
<body>
<div class="row rowpadding">
<div class="col-md-3 col-sm-3">
<div class="user-wrapper">
<div class="description">
<img height="200px" width="200px" src="<?php echo base_url();?>upload/no-avatar.jpg">
</div>
<br><br>
</div>
</div>
<div class="col-md-6 col-sm-6 user-wrapper">
<div class="description">
<br>
<h2 class="name">Hi, <?php echo $this->session->userdata('memberfname'); ?>!</h2>
<hr/>
<div class="colwrapper">
<div class="cont-5">
<div class="cont-6 name"><p class="para-2"><span class="font-3">First Name: <?php echo $this->session->userdata('memberfname'); ?>
</span></p></div>
</div><br>
<div class="cont-7">
<div class="cont-8 name"><p class="para-3"><span class="font-4">Last Name: <?php echo $this->session->userdata('memberlname'); ?></span></p></div>
</div><br>
<div class="cont-9">
<div class="cont-10"><p class="para-4"><span class="font-5">Email Address: <?php echo $this->session->userdata('email'); ?></span></p></div>
</div><br>
<div class="cont-11">
<div class="cont-12"><p class="para-5"><span class="font-6"></span></p></div>
</div><br>
</div>
<br><br>
</div>
</div>
<div class="col-md-3 col-sm-3 user-wrapper">
<div class="user-wrapper">
<br>
<div class="description">
<ul class="rightnavi"style="">
<li class="rightnavi">Add Profile Photo</li>
<li class="rightnavi">Update Your Profile</li>
</ul>
<hr/>
</div>
</div>
</div>
<!-- USER PROFILE ROW END-->
</div>
</body>
Useredit.php (View)
<form action="<?php echo $action;?>" method="POST" enctype="multipart/form-data">
<div class="container">
<div class="row rowpadding">
<div class="col-md-3 col-sm-3">
<div class="user-wrapper">
<div class="description">
<img height="200px" width="200px" src="<?php echo base_url();?>upload/no-avatar.jpg">
</div>
<br><br>
</div>
</div>
<div class="col-md-6 col-sm-6 user-wrapper">
<div class="description">
<br>
<h2 class="name">Hi, <?php echo $this->session->userdata('memberfname'); ?>!</h2>
<hr />
<p>
<div class="colwrapper">
<div class="cont-5">
<div class="cont-6 name"><p class="para-2"><span class="font-3">First Name:
<input type="text" name="memberfname" value="<?php echo $this->session->userdata('memberfname'); ?>" required />
</span></p></div>
</div><br>
<div class="cont-7">
<div class="cont-8 name"><p class="para-3"><span class="font-4">Last Name:
<input type="text" name="memberlname" value="<?php echo $this->session->userdata('memberlname'); ?>" required /></span></p></div>
</div><br>
<div class="cont-9">
<div class="cont-10"><p class="para-4"><span class="font-5">Email Address:
<input type="text" name="email" value="<?php echo $this->session->userdata('email'); ?>" required /></span></p></div>
</div><br>
<div class="cont-11">
<div>
<input type="hidden" name="hidden" value="<?php echo $this->session->userdata('id'); ?>"/>
<input type="submit" value="update">
</div>
</div>
<br><br>
</p>
</div>
</div>
</div>
</div>
</div>
</form>
You should combine form_validation object with your code.
public function update($id)
{
if ( (int)$id < 1)//$id is not an integer
{
redirect('memberlogincontroller/home', 'refresh');
}
else
{
$this->load->library('form_validation');//it's good to autoload it
$this->form_validation->set_rules('memberfname', 'First Name', 'trim|required');
$this->form_validation->set_rules('memberlname', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required');
if ($this->validation_form->run() === FALSE)
{
$this->load->view('userprofile');
}
else
{
$data = array(
'memberfname' => $this->input->post('memberfname'),
'memberlname' => $this->input->post('memberlname'),
'email' => $this->input->post('email'),
);
$this->db->update('member',$data,array('id'=> $id));
redirect('memberlogincontroller/getMember/' . $id, 'refresh');
}
}
}
In model you are not passing id of specific member. It should be like this
public function getMember($id)
{
$this->db->where('id', $id);
$query = $this->db->get('member');
if ($query->num_rows() !== 1)
{
return FALSE;
}
return $query->row();//since you need just single member data
}
Also, in controller method code you have to pass $id to model:
public function getMember($id)
{
if (int($id) < 1)
{
redirect('memberlogincontroller/home', 'refresh');//no valid uri segment
}
else
{
$this->load->model('MemberLoginModel');
$memberinfo['memberinfo'] = $this->MemberLoginModel->getMember($id);
if ( ! $memberinfo['memberinfo'])//returned FALSE from model
{
redirect('memberlogincontroller/home', 'refresh');//no $id in DB
}
else
{
$this->load->view('member/userprofile',$memberinfo);
}
}
}
Also, you should pay attention to naming convention. You shouldn't close file with closing php tag. My proposal is to read docs carefully since you could avoid lot of annoying bugs that way.
Redirect should be
redirect('MemberLoginController/members');
redirect('Controller/Method');
If method is not added, So it will call the index() method by default

set query string in admin login panel in codeigniter

I have created an admin login panel. here is my model, view, controller file. If login is unsuccessful, then i want to sent a querystring. After fetching the querystring value i want to set a message
Incorrect email/password.
I think we have to modify something in catalog_model.php or anything else??
my controller(D:\wamp\www\CodeIgniter\application\controllers\admin\catalog.php)
class Catalog extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->model('catalog_model');
}
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->view('admin/templates/header');
$this->form_validation->set_error_delimiters('<div class="error_admin">', '</div>');
$this->load->database();
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/catalog/catalog_view');
}
else
{
$this->catalog_model->admin_login();
}
$this->load->view('admin/templates/footer');
//$html_string = $this->load->view('admin/catalog/catalog_view');
}
function logout()
{
$this->session->unset_userdata('admin_login');
$this->session->unset_userdata('admin_email');
//print_r($this->session->all_userdata());
$this->load->view('admin/catalog/catalog_view');
}
}
my model(D:\wamp\www\CodeIgniter\application\models\cataalog_model)
<?php
class Catalog_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function admin_login()
{
$data = array(
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
);
//return $this->db->insert('school_registration', $data);
$query = $this->db->get_where('admin', $data);
$rowcount = $query->num_rows();
if($rowcount==1)
{
$this->session->set_userdata('admin_login', 'true');
$this->session->set_userdata('admin_email', $this->input->post('email'));
$this->load->view('admin/catalog/home_view');
}
else
{
$this->load->view('admin/catalog/catalog_view');
//parse_str($_SERVER['QUERY_STRING'],$_GET);
}
}
}
My view(D:\wamp\www\CodeIgniter\application\views\admin\catalog\catalog_view.php)
<?php if (!defined('BASEPATH')) exit(__('No direct script access allowed')); ?>
<div class="light_blue_back" align="center">
<div class="adminbox">
<?php echo form_open('admin/catalog'); ?>
<fieldset>
<legend><b>Admin Login</b></legend>
<dl>
<dt>
<label for="street_address1"> <span class="req">*</span>Email</label>
</dt>
<dd>
<input type="text" name="email" id="email" value="<?php echo set_value('email'); ?>"/><?php echo form_error('email'); ?>
</dd>
</dl>
<dl>
<dt>
<label for="street_address2">Password</label>
</dt>
<dd>
<input type="password" name="password" id="password" value="<?php echo set_value('password'); ?>"/><?php echo form_error('password'); ?>
</dd>
</dl>
<dl>
<dt>
</dt>
<dd>
<?php echo form_submit('reg_sub', 'Submit');?>
<?php echo form_reset('reg_reset', 'Reset');?>
</dd>
</dl>
</fieldset>
<?php //echo form_close('admin/catalog'); ?>
</form>
</div>
</div>
You can use set_flashdata for show your error message using this session function
the sample code is:
$this->load->library('session');
$this->session->set_flashdata('loginerror', 'Username and password mismatch');
You can show this loginerror flashdata using isset PHP function on you view page.
For more details visit this link:
http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

Error 500 after submitting the form in codeigniter

Using these MVC, I get Error 500. Any ideas?
Controller
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Download extends CI_Controller {
function __construct()
{
parent::__construct();
parent::__construct();
$this->load->library('form_validation');
$this->load->database();
$this->load->helper('form');
$this->load->helper('url');
$this->load->model('upload_model');
//$this->output->enable_profiler(TRUE);
}
function index()
{
$this->form_validation->set_rules('enter_product_name',
'Enter Product Name',
'required|max_length[200]');
$this->form_validation->set_rules('test_type',
'Test Type',
'required|max_length[200]');
$this->form_validation->set_rules('test_unit',
'Test Unit',
'required|max_length[200]');
$this->form_validation->set_rules('project_code',
'Project Code',
'required|max_length[200]');
$this->form_validation->set_error_delimiters('<br /><span class="error">',
'</span>');
if ($this->form_validation->run() == FALSE) // validation hasn't been passed
{
$data->page = 'download_form_view';
$this->load->view('container', $data);
// $this->load->view('upload_form_view');
}
else // passed validation proceed to post success logic
{
// build array for the model
$file_name_info = $this->input->post('enter_product_name')
.'_'. $this->input->post('test_type')
.'_'. $this->input->post('test_unit')
.'_'. $this->input->post('project_code');
$filename_data = array(
'download_name' => $file_name_info
);
// run insert model to write data to db
if ($this->download_model->DownloadName($filename_data) == TRUE)
{
//redirect('download/download');
$this->download();
}
else
{
echo 'An error occurred while saving your filename to database. Please contact Admin with Issue No.[1]';
// Or whatever error handling is necessary
}
}
}
function download()
{
$this->load->helper('download');
$this->db->select('download_name');
$this->db->where("id", "0");
$this->db->limit(1);
$query = $this->db->get('downloadname');
$download_save_name = $query->row()->download_name;
$data = file_get_contents("./uploads/$download_save_name.xlsx");
force_download("$download_save_name.xlsx", $data);
}
}
?>
View
<?php // Change the css classes to suit your needs
$attributes = array('class' => '', 'id' => ''); echo
form_open('download', $attributes); ?>
<p>
<label for="enter_product_name">Enter Product Name <span class="required">*</span></label>
<?php echo form_error('enter_product_name'); ?>
<br /><input id="enter_product_name" type="text" name="enter_product_name" value="<?php echo
set_value('enter_product_name'); ?>" /> </p>
<p>
<label for="test_type">Test Type <span class="required">*</span></label>
<?php echo form_error('test_type'); ?>
<?php // Change the values in this array to populate your dropdown as required ?>
<?php $options = array(
'' => 'Please Select',
'LongTerm' => 'Long Term Study',
'ShortTerm' => 'Short Term Study',
'Experimental' => 'Experimental Study',
); ?>
<br /><?php echo form_dropdown('test_type', $options, set_value('test_type'))?> </p>
<p>
<label for="test_unit">Test Unit <span class="required">*</span></label>
<?php echo form_error('test_unit'); ?>
<?php // Change the values in this array to populate your dropdown as required ?>
<?php $options = array(
'' => 'Please Select',
'Hyd' => 'Hyd Unit',
'Viz1' => 'Viz Unit-1',
'Viz2' => 'Viz Unit-2',
); ?>
<br /><?php echo form_dropdown('test_unit', $options, set_value('test_unit'))?> </p>
<p>
<label for="project_code">Project Code <span class="required">*</span></label>
<?php echo form_error('project_code'); ?>
<br /><input id="project_code" type="text" name="project_code" value="<?php echo set_value('project_code'); ?>" /> </p>
<p>
<?php echo form_submit( 'submit', 'Submit'); ?> </p>
<?php echo form_close(); ?>
Model
<?php
class Download_model extends CI_Model {
function __construct() { parent::__construct(); }
function DownloadName($filename_data)
{
$this->db->update('downloadname', $filename_data, "id = 0");
if ($this->db->affected_rows() == '1') { return TRUE; }
return FALSE; } } ?>
I think the problem is:
you loaded
$this->load->model('upload_model');
and in your model code you have given
class Download_model extends CI_Model {
change this to
class Upload_model extends CI_Model {

Categories