Codeigniter Form Won't Insert Data Into Database - php

I have a web app built on Codeigniter but I don't know which version it is. My job is just adding a simple form for this web app. The form should look like this:
And then here's my VIEW:
<div class="box">
<h4 class="white">ADD CIT Details</h4>
<div class="box-container">
<div class="modal-body form">
<form action="#" id="formcit" class="form-horizontal">
<?php echo form_open('insert_cit_ctrl'); ?>
<div class="form-body">
<?php if (isset($message)) { ?>
<CENTER><h3 style="color:green;">Data inserted successfully</h3></CENTER><br>
<?php } ?>
<div class="form-group">
<label class="control-label col-md-3">Terminal ID</label> <?php echo form_error('dterminalid'); ?>
<div class="col-md-9">
<?php echo form_input(array('id' => 'dterminalid', 'name' => 'dterminalid')); ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">CIT</label>
<div class="col-md-9">
<select class="form-control" id="selectcit" >
<option value="none">-- Select CIT --</option>
<option value="CMS">1. CMS</option>
<option value="ALPHA">2. ALPHA</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Branch</label> <?php echo form_error('dbranch'); ?>
<div class="col-md-9">
<?php echo form_input(array('id' => 'dbranch', 'name' => 'dbranch')); ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">COMM</label> <?php echo form_error('dcomm'); ?>
<div class="col-md-9">
<?php echo form_input(array('id' => 'dcomm', 'name' => 'dcomm')); ?>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Machine Type</label> <?php echo form_error('dmachinetype'); ?>
<div class="col-md-9">
<?php echo form_input(array('id' => 'dmachinetype', 'name' => 'dmachinetype')); ?>
</div>
</div>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
</div>
<?php echo form_close(); ?>
</form>
</div>
</div><!-- end of div.box-container -->
</div> <!-- end of div.box -->
And then this is my controller:
function insert_cit_ctrl() {
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$this->form_validation->set_rules('dterminalid', 'Terminal ID', 'required');
$this->form_validation->set_rules('dcit', 'CIT', 'required');
$this->form_validation->set_rules('dbranch', 'Branch', 'required');
$this->form_validation->set_rules('dcomm', 'COMM', 'required');
$this->form_validation->set_rules('dmachinetype', 'Machine Type', 'required');
if ($this->form_validation->run() == FALSE) {
echo "<script>alert('Something wrong with your input');</script>";
} else {
//Setting values for tabel columns
$data = array(
'terminal_id' => $this->input->post('dterminalid'),
'cit' => $this->input->post('dcit'),
'branch' => $this->input->post('dbranch'),
'comm' => $this->input->post('dcomm'),
'machine_type' => $this->input->post('dmachinetype')
);
//Transfering data to Model
$this->cit_model->modeladdcit($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('template', $data);
}
}
And here's my model:
<?php
class Cit_model extends CI_Model{
function __construct() {
parent::__construct();
}
function modeladdcit($data){
$this->load->database('database_office', 'TRUE');
$this->db->insert('tbl_cit', $data);
}
}
?>
And the result? Here's my database:
There's no error at all. It's just that after I submit the data, it won't appeared on my database, not a single data. Meaning, that data is not even INSERT INTO the database.
I don't understand. What is wrong with my code?
UPDATE:
Ok, so I followed your tips guys, like this:
<form action="<?php echo base_url("evangelion/insert_cit_ctrl"); ?>" id="formcit" method="POST" class="form-horizontal">
What actually happened was... NOTHING. I mean, the data still not inserted into the database. And worse, the page ruined, it called only the function, not the whole page like it used to.

add the action and method to your form
<form action="<?php echo base_url("controller_name/function_name"); ?>" id="formcit" method="POST" class="form-horizontal">

First try to display errors like this
you can find where you are wrong..
where you call controller in codeigniter you must call controller
action="controllername/actionname"
<?php
echo ini_get('display_errors');
if (!ini_get('display_errors')) {
ini_set('display_errors', '1');
}
echo ini_get('display_errors');
?>
i thought you did not mention method type in form
method ="POST"

Related

show success error message with fadein fadeout effect in Codeigniter with Bootstrap Alert

I made Crud System in Codeigniter with name, email fields, its works fine, but after completing insert, update or delete it shows success message in bootstrap alert in view part.
but i want bootstrap alert message shows & after some seconds it will fadeout.
Controller Code:-
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('User_model');
}
public function create()
{
$this->form_validation->set_rules('name','Name','required');
$this->form_validation->set_rules('email','Email','required|valid_email');
if($this->form_validation->run() == false){
$this->load->view('create');
}
else{
$formArray = array();
$formArray['name']=$this->input->post('name');
$formArray['email']=$this->input->post('email');
$formArray['created_at']= date('Y-m-d');
$this->User_model->create($formArray);
$this->session->set_flashdata('success','Record added Successfully!!!');
redirect(base_url().'User/index');
}
}
}
?>
Model Code:-
<?php
class User_model extends CI_Model {
function create($formArray){
$this->db->insert('users',$formArray);
}
}
?>
Insert Form Part :-
<div class="row">
<form method="POST" action="<?php echo base_url().'User/create';?>">
<div class="col-md-6">
<div class="form-group">
<label>Name :</label>
<input type="text" name="name" class="form-control" value="<?php echo set_value('name');?>" >
<span style="color:red;"><?php echo form_error('name');?></span>
</div>
<div class="form-group">
<label>Email-Id :</label>
<input type="text" name="email" class="form-control" value="<?php echo set_value('email');?>" >
<span style="color:red;"><?php echo form_error('email');?></span>
</div>
<div class="form-group">
<button class="btn btn-primary">Create</button>
Cancel
</div>
</div>
</form>
</div>
View Code Part:-
<div class="container">
<div class="row">
<?php
if($this->session->flashdata('success')){
?>
<div class="alert alert-success ">
<?php echo $this->session->flashdata('success'); ?>
<?php
} else if($this->session->flashdata('error')){
?>
<div class = "alert alert-danger">
<?php echo $this->session->flashdata('error'); ?>
</div>
<?php } ?>
</div>
View Code Part:-
<div class="container">
<div class="row">
<?php
if($this->session->flashdata('success')){
?>
<div class="alert alert-success " style="display:none;">
<?php echo $this->session->flashdata('success'); ?>
<?php
} else if($this->session->flashdata('error')){
?>
<div class = "alert alert-danger" style="display:none;">
<?php echo $this->session->flashdata('error'); ?>
</div>
<?php } ?>
</div>
Add script like that:-
$(".alert").fadeIn(1000).fadeOut(5000);

how to use required in form_validation in ci?

Hi when I use required in my rules in ci form_validation, I know the value of this required input post and don't show any message .but this if ($this->form_validation->run()) is always false.
thanks for your help.
my view:
<form>
<div class="row">
<div class="form-group">
<div class="col-md-2"><label>Name</label></div>
<div class="col-md-10 col-md-pull-1">
<input name="Name" class="form-control" id="Name"
value="<?php echo set_value('Name')?>"
autocomplete="off"></input>
<?php echo form_error('Name'); ?>
</div>
</div>
</div>
</form>
my controller:
public function show_info()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('Name', 'Name', 'required');
$this->form_validation->set_message('required'," please enter %s");
if ($this->form_validation->run()== FALSE)
{
echo "false";
}
else redirect();
}
In your view file, you had wrote nmae except name
<div class="row">
<div class="form-group">
<div class="col-md-2"><label>Name</label></div>
<div class="col-md-10 col-md-pull-1">
<input name="Name" class="form-control" id="Name"
value="<?php echo set_value('Name')?>"
autocomplete="off"></input>
<?php echo form_error('Name'); ?>
</div>
</div>
</div>
correct it.
please load again view file after false form_validation
public function show_info()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('Name', 'Name', 'required');
$this->form_validation->set_message('required'," please enter %s");
if ($this->form_validation->run()== FALSE)
{
echo "false";
$this->load->view('view_file','',true);
}
else redirect();
}

code igniter form_validation not work

i have a problem with form validation, this is just standart form validation.
this is weird, even i set the condition, result always pass through the condition and run "$this->model_item_management->tambah_item($item);".
my data always saved without passing form validation. I tried with dummy input $user_name and passing the data to form_validation, but didn't work either. Thank in advanced. this is my code.
public function index(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
//this is dummy post
$user_name = $this->input->post('user_name');
//i try to pass an input into variable and use it on form validation
$this->form_validation->set_rules('$user_name', 'username', 'required|min_length[4]');
$this->form_validation->set_rules('namaitem', 'namaitem', 'required');
$this->form_validation->set_rules('specitem', 'specitem', 'required');
$this->form_validation->set_rules('masagaransi', 'masagaransi', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('template/header');
$this->load->view('template/navigation');
$this->load->view('pages/form_tambah_item');
$this->load->view('template/footer');
} else {
$this->load->view('pages/form_sukses_simpan_item');
}
$nama_item = $this->input->post('namaitem');
$spec_item = $this->input->post('specitem');
$garansi_item= $this->input->post('masagaransi');
$item = array(
'NAMA_ITEM'=>$nama_item,
'SPEC_ITEM'=>$spec_item,
'MASA_GARANSI'=>$garansi_item
);
$this->model_item_management->tambah_item($item);
}
<div class="container">
<?php
$attributes = array('class' => 'form-horizontal', 'id' => 'form_tambah_item');
echo form_open('kontrol_tambah_item',$attributes); ?>
<div id="legend">
<legend class="">Tambah Item Form</legend>
</div>
<?php echo form_label( 'username', 'user_name' ); ?>
<?php echo form_input( 'user_name' ); ?>
<div class="control-group">
<!-- Nama item -->
<label class="control-label" for="namaitem">Nama Item</label>
<div class="controls">
<input type="text" id="namaitem" name="namaitem" placeholder="" class="input-xlarge">
<p class="help-block">Masukkan nama dari items</p>
</div>
</div>
<div class="control-group">
<!-- spec item-->
<label class="control-label" >Spesifikansi Item</label>
<div class="controls">
<textarea class="input-xlarge" id="specitem" name="specitem" cols="40" rows="5"></textarea>
<p class="help-block">Isi Spesifikasi Item</p>
</div>
</div>
<div class="control-group">
<!-- Masa garansi -->
<label class="control-label" for="masagaransi">Masa garansi</label>
<div class="controls">
<input type="text" id="masagaransi" name="masagaransi" placeholder="" class="input-xlarge">
<p class="help-block">Isi Masa Garansi Item</p>
</div>
</div>
<div class="control-group">
<!-- Button -->
<div class="controls">
<button type="submit" class="btn btn-success">Tambah</button>
</div>
</div>
</div>
Current:-
$this->form_validation->set_rules('$user_name', 'username', 'required|min_length[4]');
Change to : -
$this->form_validation->set_rules('user_name', 'username', 'required|min_length[4]');
Things to know :
The above function takes three parameters as input:
1.The field name - the exact name you've given the form field.
2.A "human" name for this field, which will be inserted into the error message. For example, if your field is named "user" you might give it a human name of "Username". Note: If you would like the field name to be stored in a language file, please see Translating Field Names.
3.The validation rules for this form field.
Reference :
https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules
Thanks
change
$this->form_validation->set_rules('$user_name', 'username', 'required|min_length[4]');
to
$this->form_validation->set_rules('user_name', 'username', 'required|min_length[4]');
this is so dumb, i forgot to add
<?php echo validation_errors(); ?>
in the view,

how to flash error data on view in codeigniter

how to flash error on view in codeigniter.... i m creating login on
codeigniter but if user login value not equivalent to dataase table
value. then i need to show error ....invalid user name or
password.. for that i m using set_flashdata but.... on view when i
call that by error_message(). then error showing:
Call to undefined function error_message() .....
mylogin auth controller:
public function auth() {
$data['title'] = 'Heart Surgeon';
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_login');
if ($this->form_validation->run() == TRUE) {
$Value['admin_username'] = $this->input->post('username');
$Value['admin_password'] =md5($this->input->post('password'));
if ($Value != null) {
$result = $this->user_model->login($Value);
if($this->session->set_userdata($result) != null){
redirect('dashboard');
}
}
} else {
$this->index();
}
}
my model
public function login($value) {
$query = $this->db->get_where('tbl_admin', $value, 1);
if ($query->num_rows() > 0) {
$row = $query->row_array();
$sess_arr = array(
'admin_user' => $row['admin_username'],
'adm_key' => $row['admin_key'],
'admin_type' => $row['admin_type'],
'admin_id' => $row['admin_id'],
'admin_logged_in' => TRUE
);
$this->session->set_userdata($sess_arr);
}
else{
$this->session->set_userdata(array('msg_type'=>'error'));
$this->session->set_flashdata('error', 'Invalid username/password');
redirect('home');
}
}
my view
<?php if (!defined('BASEPATH')) exit('No direct script access
allowed'); ?> <?php $this->load->view('layout/header'); ?>
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4 text-center logo-margin ">
<img src="<?php echo base_url(); ?>assets/img/logo.png" alt=""/>
</div>
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Please Sign In</h3>
</div>
<div class="panel-body">
<?php if (validation_errors()) {
?>
<div class="warning" style="padding: 0px;">
<?php echo validation_errors(); ?>
</div>
<?php
}
$atts = array(
'width' => '650',
'height' => '400',
'scrollbars' => 'yes',
'status' => 'yes',
'resizable' => 'yes',
'screenx' => '0',
'screeny' => '0'
);
?>
<div><?php echo error_message();?></div>
<?php echo form_open('home/auth'); ?>
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="E-mail" name="username" type="email" autofocus>
</div>
<div class="form-group">
<input class="form-control" placeholder="Password" name="password" type="password" value="">
</div>
<div class="checkbox">
<label>
<input name="remember" type="checkbox" value="Remember Me">Remember Me
</label>
</div>
<!-- Change this to a button or input when using this as a form -->
<input type="submit" value="login" class="btn btn-lg btn-success btn-block">
</fieldset>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div> </div> <?php $this->load->view('layout/footer'); ?>
you calling wrong function to get flash error
try set flash error on controller :-
$this->session->set_flashdata('error', 'Invalid username/password');
get error flashdata on view :-
<?php echo $this->session->flashdata('error');?>
For more :- http://ellislab.com/codeigniter/user-guide/libraries/sessions.html
Basically when we set any name value pair using set_flashdata method(),
$this->session->set_flashdata('error', 'Invalid username/password');
it will automatically be discarded/removed from the session once it will be read using
$this->session->flashdata(); method.
I am 100% agreed with Rakesh.

CodeIgniter Disallowed Key Characters after submitting the form

CodeIgniter is giving me a Disallowed Key Characters error. even after successfully inserting data in database.
here is my code.:
Controller:
class applyleavectrl extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('fill_in','fill'); //model fill in
$this->load->model('mdl_employee','emp');
$id = $this->session->userdata('id');
}
public function index()
{
$id = $this->session->userdata('id');
$this->form_validation->set_rules('leave_type', 'Leave Type', 'required|callback_check_select');
$this->form_validation->set_message('check_select', 'You need to select a leave type');
$this->form_validation->set_rules('from_date', 'From Date', 'required');
$this->form_validation->set_rules('to_date', 'To Date', 'required');
$this->form_validation->set_error_delimiters('<font size="1" face="helvetica" color="red">', '</font>');
if ($this->form_validation->run() == FALSE)
{
$data['employee_header_menus'] = $this->load->view('employee_header_menus', NULL, TRUE);
$data['employee_header_logout'] = $this->load->view('employee_header_logout', NULL, TRUE);
$data['leave_type'] = $this->fill->fill_leave_type();
$this->load->view('employee/applyleave', $data);
}
else
{
$id = $this->session->userdata('id');
$P1 = $this->input->post('leave_type');
$P2 = $this->input->post('from_date');
$P3 = $this->input->post('to_date');
$P4 = $this->input->post('comments');
$this->emp->insert_employee_leave($id, $P1, $P2, $P3, $P4);
redirect('employee/applyleavectrl');
}
}
function check_select($post_string)
{
return $post_string == '0' ? FALSE : TRUE;} }
model
public function insert_employee_leave($id, $P1, $P2, $P3, $P4)
{
$value = array(
'empid' => $id,
'leave_type' => $P1,
'from_date' => $P2,
'to_date' => $P3,
'comments' => $P4);
$this->db->insert('employee_leave', $value);
}
and the view is
<?php
$attributes = array('autocomplete' => "off");
echo form_open('employee/applyleavectrl', $attributes);
?>
<div class="row">
<div class="span3 offset2">
<label>Leave Type</label>
<select name="leave_type">
<option selected value="0">Select Leave Type</option>
<?php foreach($leave_type as $row): ?>
<option value="<?php echo $row['ID']; ?>"><?php echo $row['type']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="span3 offset2">
<label>From Date</label>
<input id="from_date" name="from_date" type="date">
</div>
</div>
<div class="row">
<div class="span3 offset2">
<label >Leave Balance</label>
<input name="leave_bal" id='leave_bal' type="text" disabled>
</div>
<div class="span3 offset2">
<label>To Date</label>
<input id="to_date" name="to_date" type="date">
</div>
</div>
<div class="row">
<div class="span2 offset2">
View Details
</div>
</div>
</br>
<div class="row">
<div class="span3 offset2">
<label>Comments</label>
<textarea name="comments" rows="3"></textarea>
</div>
</div>
<div><?php echo validation_errors(); ?></div>
<div class="line"></div>
<div class="row">
<div class="span1 offset2">
<button type="submit" class="button-red">Apply</button>
</div>
</div>
<?php echo form_close(); ?>
if you are using
echo form_open('employee/applyleavectrl', $attributes);
in your view then you should have a controller like employee not applyleavectrl.
change it and let me know if its solved your problem or not.

Categories