user_validation function is not invoked in Home controller in codeigniter - php

I need to validate a user form, which I am trying to submit after taking the input in view named as 'home.php', where I have specified the base_url('Home/user_validation').
With Home is name of the controller and user_validation is the method name. I tried to figure out the issue but I could not understand why not any other function is being invoked apart from index function in Home controller. Please help me in this case.
Controller Home.php
public function user_validation()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('message', 'Invalid username and password.');
redirect('home');
}
else {
$user_name123 = $this->input->post('username');
$teacher = $this->input->post('remember');
if ($user_name123 == 'admin') {
$query = $this->login_model->validate();
echo("Logged in");
}
}
}
Model login_model.php
function validate()
{
$this->db->where('admin_username', $this->input->post('username'));
$this->db->where('admin_password', $this->input->post('password'));
$this->db->where('active_status', 'Yes');
$query = $this->db->get('cis_tbl_admin');
if ($query->num_rows() == 1) {
return true;
}
else {
return false;
}
}
View home.php
<form action="<?php echo base_url('Home/user_validation') ?>" method="post" id="instantform">
<fieldset>
<div class="input-prepend" title="Username" data-rel="tooltip">
<span class="add-on"><i class="icon-user"></i></span>
<input autofocus class="input-large span10"
name="username" id="username" type="text"
placeholder="user name"/>
</div>
<div class="clearfix"></div>
<div class="input-prepend" title="Password" data-rel="tooltip">
<span class="add-on"><i class="icon-lock"></i></span>
<input class="input-large span10" name="password"
id="password" type="password"
placeholder="password" autocomplete="off"/>
</div>
<div class="clearfix"></div>
<div class="input-prepend">
<label class="remember" for="remember">
<input type="checkbox" name="remember" id="remember"
value="teacher"/>Teacher Login</label>
</div>
<div class="clearfix"></div>
<p class="center span5">
<button type="submit" name="submit" id="submit" class="btn btn-primary">Login</button>
<br/><br/>
<!--<a class="ajax-link" href="<?php echo base_url(); ?>parent_info/get_students_info"> <span class="hidden-tablet">Parent Login</span></a>-->
</p>
</fieldset>
<input type="hidden" name="sys_date" id="sys_date" value="<?php echo $sys_date; ?>">
<input type="hidden" name="sys_time" id="sys_time" value="<?php echo $sys_time; ?>">
<input type="hidden" name="details" id="details" value="<?php echo $details; ?>">
</form>

Check your config routes file to be sure you call functions of the home controller:
$route['Home/(:any)'] = 'Home/$1';

Related

form data into database using codeigniter

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Model{
function __construct(){
$this->userTbl='login';
}
public function insert($data=array()){
if(!array_key_exists("created", $data)){
$data['created'] = date("Y-m-d H:i:s");
}
if(!array_key_exists("modified", $data)){
$data['modified'] = date("Y-m-d H:i:s");
}
$insert = $this->db->insert($this->userTbl, $data);
if($insert){
return $this->db->insert_id();;
}else{
return false;
}
}
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('user');
}
/*
* User registration
*/
public function registration(){
$data=array();
$userData=array();
if($this->input->post(regisSubmit)){
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_email_check');
$this->form_validation->set_rules('password', 'password', 'required');
$this->form_validation->set_rules('conf_password', 'confirm password', 'required|matches[password]');
}
$userData=array(
'name'=>strip_tags($this->input->post('name')),
'email'=>strip_tags($this->input->post('email')),
'password'=>strip_tags($this->input->post('password')),
'gender'=>strip_tags($this->input->post('gender')),
'phone'=>strip_tags($this->input->post('phone'))
);
if($this->form_validation->run()==true){
$insert=$this->user->insert($userData);
if($insert){
$this->session->set_userdata('success_msg', 'Your registration was successfully. Please login to your account.');
redirect(users/login);
}
else{
$data['error_msg']='Try again';
}
}
}
$data['user'] = $userData;
//load the view
$this->load->view('users/registration', $data);
}
<!DOCTYPE html>
<html lang="en">
<head>
<link href="<?php echo base_url(); ?>assets/css/style.css" rel='stylesheet' type='text/css' />
</head>
<body>
<div class="container">
<h2>User Registration</h2>
<form action="" method="post">
<div class="form-group">
<input type="text" class="form-control" name="name" placeholder="Name" required="" value="<?php echo !empty($user['name'])?$user['name']:''; ?>">
<?php echo form_error('name','<span class="help-block">','</span>'); ?>
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email" required="" value="<?php echo !empty($user['email'])?$user['email']:''; ?>">
<?php echo form_error('email','<span class="help-block">','</span>'); ?>
</div>
<div class="form-group">
<input type="text" class="form-control" name="phone" placeholder="Phone" value="<?php echo !empty($user['phone'])?$user['phone']:''; ?>">
</div>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="Password" required="">
<?php echo form_error('password','<span class="help-block">','</span>'); ?>
</div>
<div class="form-group">
<input type="password" class="form-control" name="conf_password" placeholder="Confirm password" required="">
<?php echo form_error('conf_password','<span class="help-block">','</span>'); ?>
</div>
<div class="form-group">
<?php
if(!empty($user['gender']) && $user['gender'] == 'Female'){
$fcheck = 'checked="checked"';
$mcheck = '';
}else{
$mcheck = 'checked="checked"';
$fcheck = '';
}
?>
<div class="radio">
<label>
<input type="radio" name="gender" value="Male" <?php echo $mcheck; ?>>
Male
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="gender" value="Female" <?php echo $fcheck; ?>>
Female
</label>
</div>
</div>
<div class="form-group">
<input type="submit" name="regisSubmit" class="btn-primary" value="Submit"/>
</div>
</form>
<p class="footInfo">Already have an account? Login here</p>
</div>
</body>
</html>
Here in this form I need to insert data into mysql database using codeigniter. but the data is not inserting into the database and also not showing any error. Below is the code for modal, controller and view page. In database.php file, eerything is fine. Here in this, How to debug the code, and what is the error. Thanks in advance.
Define url in routes.php
$route['user/registration'] = 'Users/registration';
also define your project base_url in config/config.php file
use
form action="user/registration"
method="post">
instead of
form action="" method="post">
and view file in
<input type="text" class="form-control" name="name" placeholder="Name" required="" value="<?php echo !empty($user['name'])?$user['name']:''; ?>">
<?php echo form_error('name','<span class="help-block">','</span>'); ?>
change to:
<input type="text" class="form-control" name="name" placeholder="Name" required="" value="<?php echo isset($user['name']) ? $user['name']:''; ?>">
<?php echo form_error('name','<span class="help-block">','</span>'); ?>
You never post anything to your controller. look at this line of your view file.
<form action="" method="post">
Change to :
<form action="<?php echo base_URL(); ?>Users/registration" method="post">

value not insert into database using php codeigniter

I am trying to insert the data to the database by php CodeIgniter and display the result in the same page during submit the button. Below is my code.The problem is i got an error message as Duplicate entry '104' for key 'PRIMARY'. Please help. View Page
<div class="container col-lg-4">
<?php echo validation_errors(); ?>
<form action="<?php echo base_url();?>index.php/Welcome/gallery1" method="post">
<div class="form-group has-info">
<?php
foreach ($h->result_array() as $value){
?>
<input type="hidden" name="id" value="<?php echo $value['offer_id'];?>" >
<?php }?>
<br>
<label class="control-label col-lg-6" for="inputSuccess">Offer title</label>
<input type="text" class="form-control col-lg-6" name="offered" id="offered" value="<?php if(isset($_POST['offered'])) echo $_POST['offered']; ?>">
<label class="control-label col-lg-6" for="inputSuccess">Offer Description</label>
<textarea id="description" name="description" class="form-control col-lg-6" rows="3" value="<?php if(isset($_POST['description'])) echo $_POST['description']; ?>" ></textarea>
<br/>
<div>
<button type="submit" class="btn btn-primary col-lg-4"> <span>SUBMIT</span>
</button>
</div>
</div>
</form>
Controller page
public function gallery1()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('Login_set');
$this->form_validation->set_rules('id','id');
$this->form_validation->set_rules('offered','offered','required');
$this->form_validation->set_rules('description','description','required');
$page_id =$this->uri->segment(3);
$data['h']=$this->Login_set->select();
$this->load->view('App_stay/pages/hotel1_galery.php',$data);
if($this->form_validation->run() == FALSE)
{
}
else
{
$data['query']=$this->Login_set->add_offer();
if($data['query']!=NULL)
{
$data['ins_msg']='data inserted';
}
else
{
$data['ins_msg']='data not inserted';
}
}
}
Model Page
public function add_offer()
{
$this->form_validation->set_message('offered','offered','required');
$this->form_validation->set_message('description','description','required');
$this->load->database();
$this->load->helper('url');
$data=array
(
'offer_id'=>$this->input->post('id'),
'hotel_id'=>1,
'offers_name'=>$this->input->post('offered'),
'offers_description'=>$this->input->post('description')
);
$this->db->insert('offer_hotel1',$data);
}

codeigniter- on change password display change password successful

My code so far is working and changing the user password. I would now like to display the user that their password was changed successfully else say try again but i cant make it happen. Any help would be appreciated, thanks.
Here is the controller:
public function change_password($arg = "admin")
{
if(isset($_POST['data']) && !empty($_POST['data']))
{
$this->load->library('form_validation');
$this->form_validation->set_rules('data[oldpassword]', 'old password', 'required');
$this->form_validation->set_rules('data[password]', 'password', 'trim|required');
$this->form_validation->set_rules('data[passconf]', 'password Confirmation', 'trim|required|md5|matches[data[password]]');
if ($this->form_validation->run() == FALSE)
{
$data['requestee'] = $arg;
$this->load->view('change_password', $data);
}
else
{
$data = $_POST['data'];
array_pop($data);
$data['requestee'] = $arg;
$data['email'] = $this->session->userdata('email');
//print_r($data); exit;
if($data['requestee'] == "admin")
{
$this->load->model('login_model');
if($this->login_model->updatepassword($data))
{
redirect('/admin/show', 'refresh');
}
else
{
redirect('login/change_password/');
}
}
elseif($data['requestee'] == "org")
{
$this->load->model('login_model');
if($this->login_model->updatepassword($data))
{
redirect('/organizer/show', 'refresh');
}
else
{
redirect('login/change_password/org');
}
}
else
{
echo "could not change password";
}
}
}
else
{
$data = array('requestee' => $arg);
$this->load->view('change_password', $data);
}
}
Here is the view:
<form action="<?php echo site_url('login/change_password'); ?>" class="login-wrapper" method="post">
<div class="header">
<div class="row-fluid">
<div class="span12">
<h3>Change password<img src="<?php echo asset_url();?>img/logo1.png" alt="Logo" class="pull-right"></h3>
<p>Fill out the form below to change your password.</p>
</div>
</div>
</div>
<div class="content">
<div class="row-fluid">
<div class="span12">
<input class="input span12 password" id="" name="data[oldpassword]" placeholder="Old password" required="required" type="password" value="<?php echo set_value('oldpassword'); ?>">
</div>
</div>
<div class="row-fluid">
<div class="span12">
<input class="input span12 password" id="" name="data[password]" placeholder="New password" required="required" type="password" value="<?php echo set_value('password'); ?>">
</div>
</div>
<div class="row-fluid">
<div class="span12">
<input class="input span12 password" id="" name="data[passconf]" placeholder="Confirm new password" required="required" type="password" value="<?php echo set_value('passconf'); ?>" >
</div>
</div>
</div>
<div class="actions">
<input class="btn btn-danger" name="change_password" type="submit" value="Change" >
<div class="clearfix"></div>
</div>
</form>
It appears that after changing the users password, you are using a redirect to return the user to another page.
In your controller, just before doing the redirect, You can use CodeIgniters session data to set a message in flashdata like so:
$this->session->set_flashdata('message', 'Your Password Was Successfully Changed');
or
$this->session->set_flashdata('message', 'Your Password Was Not Changed');
Then in your view, you can add:
<?php echo $this->session->flashdata('message'); ?>
Please note that in order for this to work, if you have not done so already, you will need to either manually load the session library on every page where it is used or add it to your autoload.php file.

Bootstrap bugs on codeigniter?

perhaps the video will easily explain the problem. here's the link to my video .
here's the view code
<div class="col-sm-4">
<?php echo form_open('user/updateuser');
?>
<legend>Update User</legend>
<div class="form-group">
<label for="id">ID</label>
<input name="id" type="text" class="form-control" id="id" placeholder="Input id" value="<?php echo $id; ?>" disabled>
<?php echo form_error('id'); ?>
</div>
<div class="form-group">
<label for="username">Username</label>
<input name="username" type="input" class="form-control" id="username" placeholder="Input Username" value="<?php echo $username;?>">
<?php echo form_error('username'); ?>
</div>
<div class="form-group">
<label for="password">Old Password:</label>
<input name="old_password" type="password" class="form-control" id="password" placeholder="Input Old Password"" value ="<?php set_value('old_password');?>">
<?php echo form_error('old_password')?>
</div>
<div class="form-group">
<label for="password">New Password:</label>
<input name="password" type="password" class="form-control" id="password" placeholder="Input Password" ">
<?php echo form_error('password')?>
</div>
<div class="form-group">
<label for="password">New Password Confirmation:</label>
<input name="password_conf" type="password" class="form-control" id="password" placeholder="Input Password Confirmation">
<?php echo form_error('password_conf')?>
</div>
<div class="form-group">
<label for="email">Email address</label>
<input name="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email" value="<?php echo $email; ?>">
<?php echo form_error('email')?>
</div>
<div class="form-group" align="center">
<button type="submit" class="btn btn-success">Submit</button>
<button type="reset" class="btn btn-danger">Clear</button>
</div>
</div>
<?php
echo form_close();
?>
and here's the controller user/updateuser
function index()
{
//This method will have the credentials validation
$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('old_password', 'Old Password', 'trim|required|xss_clean|callback_check_password');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|matches[password_conf]');
$this->form_validation->set_rules('password_conf', 'Password Confirmation', 'trim|required|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
if($this->isloggedin('logged_in'))
{
if($this->form_validation->run() == FALSE)
{
$data = array(
'sess_username' => $this->isloggedin('logged_in'),
'id' => $this->input->post('id'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email')
);
$this->load->view('header');
$this->load->view('main/menu_super_admin',$data);
$this->load->view('user/modifuser');
$this->load->view('footer');
}
else
{
$query = $this->m_user->updateuser($this->input->post('id'),$this->input->post('username'),md5($this->input->post('password')),$this->input->post('email'));
if($query)
{
echo "<script>window.onload = function() { return alert(\" Update User Success ! \"); }</script>";
}
else
{
return false;
}
redirect('user/user', 'refresh');
}
}
else
{
redirect('login', 'refresh');
}
}
the problem is, i want to make the disabled input stay disabled and the values remains the same,
is there any mistakes on my code ?
Disabled inputs are not posted to the server: http://www.w3.org/TR/html401/interact/forms.html#disabled
... [a disabled input] cannot receive user input nor will its value be submitted with the form.
I suggest getting the ID from the session and not relying on the posted information in any way (This is a security concern because posted information can be manipulated by the end user). You already check to see if the user is logged in. Just get the ID from the the session while you're at it.
Session ID can be retrieved like so:
$data = array(
'sess_username' => $this->isloggedin('logged_in'),
'id' => $this->session->userdata('session_id'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email')
);
You may also need to load the library first
$this->load->library('session');
I also suggest using sess_use_database as mentioned in the docs for added session security.

set_value not working

I have the form_validation library loaded in my controller and the form validation itself is working, but when using set_value() it's not populating the form fields. Here is the code in to controller:
function addUser()
{
$this->form_validation->set_rules('firstName', 'firstname', 'trim|required|max_length[30]');
$this->form_validation->set_rules('surname', 'surname', 'trim|required|max_length[30]');
$this->form_validation->set_rules('emailAddress', 'email address', 'trim|required|valid_email|is_unique[users.email]|max_length[255]');
$this->form_validation->set_rules('password', 'password', 'trim|required|max_length[20]|min_length[5]');
$this->form_validation->set_rules('passwordVerify', 'password verification', 'trim|required|max_length[20]|min_length[5]');
if($this->form_validation->run() === FALSE) {
$this->session->set_flashdata('formValidationError', validation_errors('<p class="error">', '</p>'));
redirect('/member/register');
} else {
echo 'Passed';
}
}
And here is the code in the view:
<?php echo $this->session->flashdata('formValidationError'); ?>
<form method="POST" action="<?php echo site_url('member/addUser'); ?>">
<fieldset>
<legend>Create a FREE account</legend>
<div>
<label for="firstName">Firstname</label>
<input type="text" name="firstName" value="<?php echo set_value('firstName'); ?>" maxlength="30">
</div>
<div>
<label for="surname">Surname</label>
<input type="text" name="surname" value="<?php echo set_value('surname'); ?>" maxlength="30">
</div>
<div>
<label for="emailAddress">Email Address</label>
<input type="text" name="emailAddress" value="<?php echo set_value('emailAddress'); ?>" maxlength="255">
</div>
<div>
<label for="password">Password</label>
<input type="password" name="password" maxlength="20">
</div>
<div>
<label for="passwordVerify">Verify Password</label>
<input type="password" name="passwordVerify" maxlength="20">
</div>
<button type="submit">Register</button>
</fieldset>
</form>
Is there something I am missing? Is the redirect causing the issue?
It is not working because you are using redirection.
Instead of
$this->session->set_flashdata('formValidationError', validation_errors('<p class="error">', '</p>'));
redirect('/member/register');
just for testing, try to load the view
$this->load->view('member_register_view');
and you will see.
set_value requires that the form validation ran in the same context... you lose this context when you redirect.
i had the same issue with redirect(), i used $this->session->set_flashdata('field','value') to store data before redirect, as loading view was not possible in my case, and reused it as value of input tag,
<input type='text' value='<?= $this->session->flashdata('field') ?>' >

Categories