Codeigniter Validation form not working - php

I know that this problem is in some other question, but I can't figure it out how to handle it properly in my case. I've been searching for a while, and I can't find the solution:
I want to do one form and connect with my database, but something goes wrong and it's impossible to receive the values on the database.
Here it's the view (location: /application/view/pages/home.php):
<?php echo validation_errors();?>
<div id="signup" style='position: absolute; left:900px; top: 380px;'>
<?php echo form_open('user/signup');?>
<input type='text' required="required" placeholder="Full Name" name="fullname"/><br/>
<input type='text' required="required" placeholder="Email" name="email"/><br/>
<input type='text' required="required" placeholder="Password" name="password"/><br/>
<input type='text' required="required" placeholder="Confirm password" name="confirmpassword"/><br/>
<button type="submit" name="submit_signup">Sign up</button>
<?php echo form_close(); ?>
Here the controller (location: /application/controllers/user.php):
class User extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('User_model');
}
public index(){
$this->load->view('view');
}
public function signup(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('User_model');
$user_id = DEFAULT;
$data = array(){
'user_id' => $user_id,
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'full_name' => $this->input->post('fullname')
}
$this->User_model->add_user($data);
$this->index();
}
}
And the model (location: /application/models/user_model.php):
<?php
class User_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function add_user($data){
$this->load->database();
$this->db->insert('user',$data);
}
}
?>

You have syntax error in your array:
It should be:
$data = array(
'user_id' => $user_id,
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'full_name' => $this->input->post('fullname')
)
If still didn't work, your $user_id is probably an INT type, so just give it some INT.
And you are not doing any validation at all!?

Your Solution is here:
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}?>

Related

form validation not working in CodeIgniter

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";
}
}
}

update details ci

I am currently trying to allow a user when logged in to update their own details. So far, the user is only allowed to edit user 1 but the form does not populate either. I would like to be able to allow the specific session user to change their details, but don't know what way to go about this. Any guidance would be appreciated, thanks!
Login Controller- Login.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('login_model');
}
public function index()
{
if(($this->session->userdata('user_name')!=""))
{
$this->welcome();
}
else {
$data['title']= 'MVC Application';
$this->load->view('templates/header', $data);
$this->load->view('templates/nav');
$this->load->view('login/signin', $data);
$this->load->view('templates/footer');
}
}
public function welcome()
{
$data['title']= 'MVC Application';
$this->load->view('templates/header', $data);
$this->load->view('templates/nav');
$this->load->view('login/welcome', $data);
$this->load->view('templates/footer');
}
public function login()
{
$email=$this->input->post('email');
$password=$this->input->post('pass');
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('email', 'Your Email', 'trim|required');
$this->form_validation->set_rules('pass', 'Password', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else{
$this->login_model->login($email,$password);
$this->welcome();
}
}
public function logout()
{
$newdata = array(
'id' =>'',
'username' =>'',
'email' => '',
'logged_in' => FALSE,
);
$this->session->unset_userdata($newdata );
session_destroy();
redirect('login/index');
}
function update()
{ $data['title']= 'MVC Application';
$this->load->view('templates/header', $data);
$this->load->view('templates/nav');
$this->load->view('login/update', $data);
$this->load->view('templates/footer');
$data = array (
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
$this->load->model('login_model');
$this->login_model->update($data);
}
}
?>
Login model- Login_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function login($email, $password)
{
$this->db->where("email",$email);
$this->db->where("password",$password);
$query=$this->db->get("mvc_user");
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
'id' => $rows->id,
'username' => $rows->username,
'email' => $rows->email,
'password' => $rows->password,
'logged_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
return true;
}
return false;
}
function update($data)
{
$this->db->where('id', 1);
$this->db->update('mvc_user', $data);
}
}
?>
Update view (located in login folder) update.php
<div class="six columns">
<?php echo form_open('login/update'); ?>
<p>
<label for="user_name">Username</label>
<input type="text" name="user_name" id="user_name" />
</p>
<p>
<label for="user_email">Email</label>
<input type="text" name="user_email" id="user_email" />
</p>
<p>
<label for="user_password">Password</label>
<input type="text" name="user_password" id="user_password" />
</p>
<p><input type="submit" value="Save" /></p>
<?php echo form_close(); ?>
</div>
First, you need to pass the data from the Login controller to the view so that you can pre-populate the form fields:
function update() {
// Prepare data to pass to the view
$data = array (
'title' => 'MVC Application',
'username' => $this->session->userdata('username'),
'email' => $this->session->userdata('email'),
'password' => $this->session->userdata('password')
);
$this->load->view('templates/header', $data);
$this->load->view('templates/nav');
$this->load->view('login/update', $data);
$this->load->view('templates/footer');
$data = array (
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
$this->load->model('login_model');
$this->login_model->update($data);
}
The view can then access the array elements as individual variables and pre-populate the input fields:
<div class="six columns">
<?php echo form_open('login/update'); ?>
<p>
<label for="username">Username</label>
<input type="text" name="username" id="username" value="<?php echo $username; ?>" />
</p>
<p>
<label for="email">Email</label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
</p>
<p>
<label for="password">Password</label>
<input type="text" name="password" id="password" value="<?php echo $password; ?>" />
</p>
<p><input type="submit" value="Save" /></p>
<?php echo form_close(); ?>
</div>
To save the user's new data in Login_model.php, just get the id from the session:
function update($data) {
$my_id = $this->session->userdata('id');
if($my_id !== false) { // Just making sure we're logged in
$this->db->where('id', $my_id);
$this->db->update('mvc_user', $data);
}
}

Codelgniter Form Validation Showed double repeated form after validate

Tried to read though all the webpages and docs in google and stack flow but still could not solve the problem.
I tried to do a simple data validation for registration form and it turns out showing another form below the original one after I press submit to show the error messages with a new form.
I am a newbie in this language so please let me know if I attache not enough codes or information.
Controller account:
<?php
class Account extends MY_Controller {
public function __construct() {
parent::__construct();
session_start();
$this->load->model('user');
$this->load->helper(array('form','url','html'));
$this->load->library('session');
$this->load->library('form_validation');
}
public function registration() {
$data = $this->user->users_info();
$this->load->view('account/registration',$data);
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[20]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password ', 'required|matches[passconf]|min_length[5]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
if($this->input->post('submit')) {
$username= $this->input->post('username');
$email= $this->input->post('email');
$query_u= $this->user->retrieve_by_username($username);
$query_e= $this->user->retrieve_by_email($email);
if ($this->form_validation->run() == FALSE){
$this->load->view('account/registration',$data); ←---------------- (I think this is wrong, it makes load the second form out.)
}
else{
if(!empty($query_u) or !empty($query_e)) {
redirect('account/registrat');
}
else {
$data = array(
'username'=>$this->input->post('username'),
'email'=>$this->input->post('email'),
'password'=>$this->input->post('password'),
'is_admin'=>0,
);
$this->user->create_user($data);
redirect('/account/login');
}
}
}
}
View Registration.php
<center>
<?php echo form_open_multipart('account/registration'); ?>
<h5><?php echo $b_username;?> (Minimum 5 characters)</h5>
<input type="text" name="username" id="username" value="<?php echo set_value('username'); ?>" size="50" /><?php echo form_error('username'); ?>
<h5><?php echo $b_email;?></h5>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<?php echo form_error('email'); ?>
<h5><?php echo $b_password;?> (Minimum 5 characters)</h5>
<input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" />
<?php echo form_error('password'); ?>
<h5><?php echo $b_passconf;?></h5>
<input type="text" name="passconf" value="" size="50" />
<?php echo form_error('passconf'); ?>
<h5></h5>
<div><?php echo form_submit('submit', 'Submit') ?></div>
</center>
Model user.php
<?php
class User extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function users_info() {
$data['b_id'] = 'id';
$data['b_username'] = 'Username';
$data['b_email'] = 'Email';
$data['b_password'] = 'Password';
$data['b_passconf'] = 'Enter Password Again';
$data['b_is_admin'] = 'Is_admin';
$data['b_default_privacy'] = 'Default_privacy';
$data['b_first_name'] = 'First_Name';
$data['b_last_name'] = 'Last_Name';
$data['b_gender'] = 'Gender';
$data['b_birthday'] = 'Birthday';
$data['b_friend_id'] = 'Friend_id';
$data['b_weight'] = 'Weight';
$data['b_height'] = 'Height';
$data['b_daily_cal_intake'] = 'Daily_calorie_intake';
$data['b_target_weight'] = 'Target_weight';
$data['b_regional_id'] = 'Region';
$data['b_profile_pic'] = 'Profile Picture';
return $data;
}
function retrieve_by_username($username) {
$query = $this->db->get_where('001_users',array('username'=>$username));
return $query->row_array();
}
function retrieve_by_email($email) {
$query = $this->db->get_where('001_users', array('email'=>$email));
return $query->row_array();
}
Change your function to this and try..
public function registration()
{
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[20]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password ', 'required|matches[passconf]|min_length[5]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
if ($this->form_validation->run() == false) // if validation fails for first time and every time when ever condtion is not satisified
{
$data = $this->user->users_info();
$this->load->view('account/registration',$data);
}
else
{
$username= $this->input->post('username');
$email= $this->input->post('email');
$query_u= $this->user->retrieve_by_username($username);
$query_e= $this->user->retrieve_by_email($email);
if(!empty($query_u) or !empty($query_e)) {
redirect('account/registrat');
}
else {
$data = array(
'username'=>$this->input->post('username'),
'email'=>$this->input->post('email'),
'password'=>$this->input->post('password'),
'is_admin'=>0
);
$this->user->create_user($data);
// send sucess msg here
redirect('/account/login');
}
}
}

Codeigniter validation is not working

I am new in codeigniter .I have done validation for my project .But it is not working fine .I have written my all code here .First is view page and second is my controller page.Please help anyone
<?php $this->load->helper('form');
echo validation_errors();
echo form_open('SM_in_controller/sm_login_action');
?>
<input class="login_input" type="text" placeholder="Username" name="username" id="username"/>
<input type="submit" value="Login" class="login_button" id="login_button"/>
</form>
and my controller
public function sm_login_action() {
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
}
In your controller, when you call the "run" method is when the proccess is done:
public function sm_login_action() {
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
if( $this->form_validation->run() ) { // Return TRUE on success
// Success
} else {
// Failure
}
}
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
try this for more details.
https://www.codeigniter.com/user_guide/libraries/form_validation.html
Try this
<input class="login_input" type="text" placeholder="Username" name="username" id="username" required/>
Hope this helps

Form not submitting into the database using Codeigniter_2.1.4 framework

I have created a form in Codeigniter but it does not to submit values to the database. I have tried to debug my code by echoing out "hello" in my method that works but the actual method does not submit anything to the database.
This is code in my view page
<div data-role="content">
<?php echo form_open('index.php/welcome/adduser'); ?>
<div data-role="fieldcontain">
<?php echo validation_errors('<p class="error">','</p>'); ?>
<p>
<label>Firstname: </label>
<?php echo form_input('firstname', set_value( 'firstname' ) ); ?>
</p>
<p>
<label>Lastname: </label>
<?php echo form_input('lastname', set_value( 'lastname' ) ); ?>
</p>
<p>
<label>Email: </label>
<?php echo form_input('email', set_value( 'email' ) ); ?>
</p>
<p>
<label>Age: </label>
<?php echo form_input('age', set_value( 'age' ) ); ?>
</p>
<p>
<label>username: </label>
<?php echo form_input('username', set_value( 'username' ) ); ?>
</p>
<p>
<label>Password: </label>
<?php echo form_input('password', set_value( 'password' ) ); ?>
</p>
<p>
<?php echo form_submit('submit', 'Submit'); ?>
</p>
</div>
This is my controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Welcome extends CI_Controller {
/** loading services* */
function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('form_validation');
}
/** loading several pages * */
public function index() {
$this->load->view('abt-header');
$this->load->view('abt-abovetheblues');
$this->load->view('abt-footer');
}
public function adduser() {
if ($this->_adduser_validate() === FALSE) {
$this->index();
return;
}
$data = new user();
$data->firstname = $this->input->post('firstname');
$data->lastname = $this->input->post('lastname');
$data->username = $this->input->post('username');
$data->password = $this->input->post('password');
$data->email = $this->input->post('email');
$data->age = $this->input->post('age');
$this->load->view('#abt-profile');
}
private function _adduser_validate() {
// validation rules
$this->form_validation->set_rules('firstname', 'Firstname', 'required');
$this->form_validation->set_rules('lastname', 'Lastname', 'required');
$this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric|min_length[6]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[12]');
// $this->form_validation->set_rules('passconf', 'Confirm Password', 'required|matches[password]');
$this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
$this->form_validation->set_rules('age', 'Age', 'required');
return $this->form_validation->run();
}
}
?>
You are not specifying what data to be saved in which table.
Use something like this...
/*
$data = array(
'tableCol1' => $this->input->post('postedItem1'),
'tableCol2' => $this->input->post('postedItem2'),
);
*/
$this->db->set($data);
$this->db->insert($tableName);
First of all you have not closed your form, add this to your view:
<?php echo form_close();?>
I don't see where you have run your insert query which will insert data in database. Your Adduser function can be like this:
public function adduser() {
if ($this->_adduser_validate() === FALSE) {
$this->index();
return;
}
$data = array(
'firstname' = $this->input->post('firstname'),
'lastname' = $this->input->post('lastname'),
'username' = $this->input->post('username'),
'password' = $this->input->post('password'),
'email' = $this->input->post('email'),
'age' = $this->input->post('age'),
);
$this->db->insert('user',$data);
$this->load->view('#abt-profile');
}
I am assuming you have set your database name and login credentials in config/database.php file
This will for sure help you. If there is any problem let me know.

Categories