Error in form validation using codeigniter - php

I am facing a problem in validating the form using Codeigniter. Below is the code I am using in my project.
I have followed all the rules which I have read in the Codeigniter user guide, but I don't know what that issue is; the validation is not happening.
Controller (page.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Page extends Front_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Page_model');
$this->load->helper(array('form', 'url'));
}
public function index()
{
$data['page_title'] = 'Doctors Feedback';
$data['base_url']= $this->uri->segment_array();
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('qualification', 'Qualification', 'required');
$this->form_validation->set_rules('age', 'Age', 'required');
$this->form_validation->set_rules('date', 'Date', 'required');
$this->form_validation->set_rules('residential', 'Residential Address', 'required');
$this->form_validation->set_rules('clinic', 'Clinic Address', 'required');
$this->form_validation->set_rules('email', 'E-mail', 'required');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required');
$this->form_validation->set_rules('phone', 'Phone Number', 'required');
$this->form_validation->set_rules('comment', 'Comment', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->view('doctors_form', $data);
}
}
}
?>
View (doctors_form.php)
<form>
<div class="col-md-12 form-group">
<label>Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo set_value('name')?>" placeholder="Name">
<span class="text-danger"><?php echo form_error("name");?></span>
</div>
<div class="col-md-6 form-group">
<label>Select Gender</label>
<select class="form-control jcf-hidden" data-jcf="{"wrapNative": false, "wrapNativeOnMobile": false}">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
<div class="col-md-6 form-group">
<label>Qualification</label>
<input type="text" class="form-control" id="qualification" name="qualification" value="<?php echo set_value('qualification')?>" placeholder="Qualification">
<span class="text-danger"><?php echo form_error("qualification");?></span>
</div>
<div class="col-md-6 form-group">
<label>Age</label>
<input type="text" class="form-control" id="age" name="age" value="<?php echo set_value('age')?>" placeholder="Age">
<span class="text-danger"><?php echo form_error("age");?></span>
</div>
<div class="col-md-6 form-group">
<label>Date</label>
<input type="text" class="form-control" id="date" name="date" value="<?php echo set_value('date')?>" placeholder="Date">
<span class="text-danger"><?php echo form_error("date");?></span>
</div>
<div class="col-md-12 form-group">
<label>Residential Address</label>
<textarea rowa="5" class="form-control" id="address" name="residential" placeholder="Residential Address"></textarea>
<span class="text-danger"><?php echo form_error("residential");?></span>
</div>
<div class="col-md-12 form-group">
<label>Clinic Address</label>
<textarea rowa="5" class="form-control" id="address" name="clinic" placeholder="Clinic Address"></textarea>
<span class="text-danger"><?php echo form_error("clinic");?></span>
</div>
<div class="col-md-12 form-group">
<label>Email</label>
<input type="text" class="form-control" id="email" name="email" value="<?php echo set_value('email')?>" placeholder="Email">
<span class="text-danger"><?php echo form_error("email");?></span>
</div>
<div class="col-md-6 form-group">
<label>Mobile</label>
<input type="text" class="form-control" id="mobile" name="mobile" value="<?php echo set_value('mobile')?>" placeholder="Mobile">
<span class="text-danger"><?php echo form_error("mobile");?></span>
</div>
<div class="col-md-6 form-group">
<label>Phone</label>
<input type="text" class="form-control" id="phone" name="phone" value="<?php echo set_value('phone')?>" placeholder="Phone">
<span class="text-danger"><?php echo form_error("phone");?></span>
</div>
<div class="col-md-12 form-group">
<label>Comment</label>
<textarea rowa="5" class="form-control" id="comment" name="comment" placeholder="Comment"></textarea>
<span class="text-danger"><?php echo form_error("comment");?></span>
</div>
<div class="col-md-12 text-right">
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</form>

First of all you should rename the controller name to Page.php (Make first letter capital). Your controller code should look like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Page extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Page_model');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
public function index()
{
if ($_POST)
{
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('qualification', 'Qualification', 'required');
$this->form_validation->set_rules('age', 'Age', 'required');
$this->form_validation->set_rules('date', 'Date', 'required');
$this->form_validation->set_rules('residential', 'Residential Address', 'required');
$this->form_validation->set_rules('clinic', 'Clinic Address', 'required');
$this->form_validation->set_rules('email', 'E-mail', 'required');
$this->form_validation->set_rules('mobile', 'Mobile Number', 'required');
$this->form_validation->set_rules('phone', 'Phone Number', 'required');
$this->form_validation->set_rules('comment', 'Comment', 'required');
if ($this->form_validation->run() == FALSE)
{
$data['page_title'] = 'Doctors Feedback';
$data['base_url'] = $this->uri->segment_array();
$this->view('doctors_form', $data);
}
else
{
$name = $this->input->post('name',TRUE);
$qualification = $this->input->post('qualification',TRUE);
$age = $this->input->post('age',TRUE);
$date = $this->input->post('date',TRUE);
$residential = $this->input->post('residential',TRUE);
$clinic = $this->input->post('clinic',TRUE);
$email = $this->input->post('email',TRUE);
$mobile = $this->input->post('mobile',TRUE);
$phone = $this->input->post('phone',TRUE);
$comment = $this->input->post('comment',TRUE);
$data = array(
'name' => $name ,
'qualification' => $qualification ,
'age' => $age ,
'date' => $date ,
'residential' => $residential ,
'clinic' => $clinic ,
'email' => $email ,
'mobile' => $mobile ,
'phone' => $phone ,
'comment' => $comment ,
);
// Further deal with model using this array
}
}
}
}
In your view, you should use the codeigniter form open and close tag preferably instead of HTML .
<?php echo form_open('Controller_name/function_name'); ?> // equivalent to <form action='url'>
<?php echo form_close(); ?> // equivalent to </form>
In your case you don't have to write the function name in form_open tag. Just write the controller name because index function is loads by default.

Related

Image uploading in codeigniter in database

I have a problem with uploading images in the database in codeigniter. When I upload image in the database, it shows this type of error like:
The upload path does not appear to be valid. So what's the problem in this code. I cannot figure out, so please help me.
I tried base_url() method also but error is not solved...!
My Htmlcode:-
<?php echo form_open_multipart('student/insertstudent') ?>
<form>
<div class="panel panel-primary" >
<div class="panel-heading" >
<p class="label" style="font-size: 15px">Student Registration Form</p>
</div>
<div class="panel-body">
<fieldset>
<div class="form-group">
<label for="exampleInputEmail1">First Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter First Name" name="fname" value="<?php echo set_value('fname'); ?>">
<div class="row-lg-6">
<?php echo form_error('fname'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Last Name</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter Last Name" name="lname" value="<?php echo set_value('lname'); ?>">
<div class="row-lg-6">
<?php echo form_error('lname'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleTextarea">Address</label>
<textarea class="form-control" id="exampleTextarea" rows="2" placeholder="Enter Address" name="add"><?php echo set_value('add'); ?></textarea>
<div class="row-lg-6">
<?php echo form_error('add'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Contact Number</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter Contact Number" name="cno" value="<?php echo set_value('cno'); ?>">
<div class="row-lg-6">
<?php echo form_error('cno'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Emaid ID</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter your email" name="email" value="<?php echo set_value('email'); ?>">
<div class="row-lg-6">
<?php echo form_error('email'); ?>
</div>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Enter Course</label>
<input type="text" class="form-control" id="exampleInputPassword1" placeholder="Enter Course Name" name="cname" value="<?php echo set_value('cname'); ?>">
<div class="row-lg-6">
<?php echo form_error('cname'); ?>
</div>
</div>
<!-- <div class="form-group">
<label for="exampleInputPassword1">Select Student Photo</label>
<input type="file" class="form-control" id="exampleInputPassword1" placeholder="Enter Course Name" name="photo">
<div class="row-lg-6">
<?php echo form_error('photo'); ?>
</div>
</div> -->
<div class="form-group">
<label for="exampleInputPassword1">Select Image</label>
<?php echo form_upload(['name' => 'userfile','class'=>'form-control']); ?>
<div class="row-lg-6">
<?php if(isset($upload_error))
{
echo $upload_error;
} ?>
</div>
</div>
<h4 align="center" style="margin-left: -150px"><button type="submit" class="btn btn-primary">Register</button></h4>
<h4 align="center" style="margin-top: -43px;margin-left: 200px"><button type="reset" class="btn btn-primary">Reset</button></h4>
</fieldset>
</div>
</div>
my controller file:-
public function insertstudent()
{
$this->form_validation->set_rules('fname', 'FirstName', 'required|alpha');
$this->form_validation->set_rules('lname', 'lastName', 'required|alpha');
$this->form_validation->set_rules('add', 'Address', 'required');
$this->form_validation->set_rules('cno', 'Contact Number', 'required|numeric');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('cname', 'Course Name', 'required');
// $this->form_validation->set_rules('userfile', 'Student Photo', 'required');
$this->form_validation->set_error_delimiters("<p class='text-danger'>", "</p>");
$config['upload_path'] = base_url('asset/uploads/');
$config['allowed_types'] = 'gif|jpg|png';
$this->upload->initialize($config);
$this->load->library('upload',$config);
if ($this->form_validation->run() == TRUE && $this->upload->do_upload('userfile')) {
$post = $this->input->post();
$data = $this->upload->data();
$image_path = base_url("upload/".$data['raw_name'].$data['file_ext']);
$post['image_path'] = $image_path;
$this->load->model('useradd');
if($this->useradd->addstudent($post)){
$this->session->set_flashdata('success', 'Record Successfully Inserted');
return redirect('admin');
}else {
$this->load->view('addstudent');
}
// $data = array(
// 'firstname' => $this->input->post('fname'),
// 'lastname' => $this->input->post('lname'),
// 'address' => $this->input->post('add'),
// 'phone' => $this->input->post('cno'),
// 'email' => $this->input->post('email'),
// 'course' => $this->input->post('cname'),
// 'photo' => $image_path,
// );
// // $this->load->library('upload', $data);
// if ($result == true) {
// $this->session->set_flashdata('success', 'Record Successfully Inserted');
// return redirect('admin');
// } else {
// $this->load->view('addstudent');
// }
} else {
$upload_error = $this->upload->display_errors();
$this->load->view('addstudent',compact('upload_error'));
}
}
my Model:-
class useradd extends CI_Model
{
function addstudent($data)
{
$result = $this->db->insert('add_student', $data);
}
}
My error is:
The upload path does not appear to be valid.
Try This Code To Upload Image.and remove form tag in your view file and you didn't define form_close method at the end of the form of your view file
$file = $_FILES['filename']['name'];
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->do_upload();
$allData = $this->upload->data();

Can't pass data from view to controller

I have a strange problem.
I can't pass the data from form to controller. I think my code was right, but I don't know why it can't pass the data- and always says "failed". I've tried to print the data that I post, but it says 'no data'.
Maybe there is something I forgot to do, and can you help me?
Here's my code
view code :
<div id="register" class="text-center">
<div class="container">
<div class="section-title center">
<h2>Register</h2>
<hr>
<form action="<?php echo base_url('c_register/do_insert'); ?>" method="post">
<link href="<?php echo base_url();?>assets/user/css/login.css" rel="stylesheet">
<input type="text" name="firstname" id="firstname" placeholder="First Name">
<span class="text-danger"> <?php echo form_error("firstname"); ?></span>
<input type="text" name="lastname" id="lastname" placeholder="Last Name">
<span class="text-danger"> <?php echo form_error("lasttname"); ?></span>
<input type="text" name="fullname" id="fullname" placeholder="Full Name">
<span class="text-danger"> <?php echo form_error("fullname"); ?></span>
<input type="text" name="username" id="username" placeholder="Username">
<span class="text-danger"> <?php echo form_error("username"); ?></span>
<input type="text" name="email" id="email" placeholder="Email">
<span class="text-danger"> <?php echo form_error("email"); ?></span>
<input type="password" name="pass" id="pass" placeholder="Password">
<span class="text-danger"> <?php echo form_error("pass"); ?></span>
<!-- <input type="password" name="pass1" placeholder="Retype Password"> -->
<ul>
<p1>Gender</p1>
<select name="gender" id="gender" class="pilihan">
<span class="text-danger"> <?php echo form_error("gender"); ?></span>
<option value="men">Male</option>
<option value="women">Female</option></select>
</ul>
<ul>
<p1>Occupation</p1>
<li><input type="radio" name="job" value="doctor" id="doctor" checked> Doctor<br></li>
<span class="text-danger"> <?php echo form_error("job"); ?></span>
<li><input type="radio" name="job" value="nurse" id="nurse"> Nurse<br></li>
</ul>
<link rel="stylesheet" type="text/css" href="assets/user/login.css">
Primary link
</form>
my controller code :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class C_register extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('m_register');
}
// function index()
// {
// $this->template->load('static','register');
// }
function do_insert(){
// $this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('firstname', 'firstname', 'required');
$this->form_validation->set_rules('lastname', 'lastname', 'required');
$this->form_validation->set_rules('fullname', 'fullname', 'required');
$this->form_validation->set_rules('username', 'username', 'required');
$this->form_validation->set_rules('email', 'email', 'required');
$this->form_validation->set_rules('pass', 'pass', 'required');
$this->form_validation->set_rules('gender', 'gender', 'required');
$this->form_validation->set_rules('job', 'job', 'required');
if ($this->form_validation->run()) {
//true
$data = array(
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'fullname' => $this->input->post('fullname'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'pass' => $this->input->post('pass'),
'gender' => $this->input->post('gender'),
'job' => $this->input->post('job')
);
$this->m_register->InsertData($data);
echo "success";
} else {
//false
// $this->index();
echo "failed";
}
}
?>
my model code :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class M_register extends CI_Model {
public function InsertData($data){
$this->db->insert("member", $data);
}
}
?>
With your current implementation, you have hidden any errors you might be getting.
So to see them, quickly, you need to load the form helper and add the line
echo validation_errors();
Just before you run the validation, just so you can see what is going on.
OR you can view the post data by doing a var_dump($_POST); in the same location

what is happing with my code when always return me nable to access an error message corresponding?

I was trying to make my own custom callback function for phones , but when I wanted to call it into $this->form_validation->set_rules('phone', 'phone', 'required|callback_phone'); then always return me 'Unable to access an error message corresponding to your field name phone.(phone)' I dont know why if I have all good , any ideas for this ??
$this->form_validation->set_rules('first_name', 'first_name', 'trim|alpha|required');
$this->form_validation->set_rules('last_name', 'last_name', 'trim|alpha|required');
$this->form_validation->set_rules('email', 'email', 'trim|valid_email|required');
$this->form_validation->set_rules('phone', 'phone', 'required|callback_phone');
$this->form_validation->set_rules('address', 'address', 'trim|alpha_numeric_spaces|required');
if ($this->form_validation->run() == FALSE) {
$this->json($this->form_validation->error_array());
} else {
if (empty($data)) {
$this->json(array('msg' => 'vacio'));
} else {
if (!$this->providers->isExistsProvider($data)) {
$this->providers->addProvider($data);
$this->json(array('msg' => 'successfully added'));
} else {
$this->json(array('msg' => 'The email is used by another provider'));
}
}
}
callback_function
public function phone($phone) {
if ( preg_match( '/^[+]?([\d]{0,3})?[\(\.\-\s]?([\d]{3})[\)\.\-\s]*([\d]{3})[\.\-\s]?([\d]{4})$/', $phone ) ) {
return TRUE;
} else {
return FALSE;
}
}
html form
<form class="form-horizontal" id="provider">
<div class="form-group row">
<div class="col-md-6">
<label for="wprice" class="">First Name: </label>
<input type="text" name="first_name" id="first_name" class="form-control input-sm" placeholder="First Name" title="First Name" required>
</div>
<div class="col-md-6">
<label for="cost_price" class="">Last Name: </label>
<input type="text" name="last_name" id="last_name" class="form-control input-sm" placeholder="Last Name" title="Last Name" required>
</div>
</div>
<div class="form-group">
<label for="description" class="col-sm-2 control-label">Email: </label>
<div class="col-sm-10">
<input type="email" name="email" id="email" class="form-control input-sm" placeholder="Email" title="Invalid Email" required>
</div>
</div>
<div class="form-group row">
<div class="col-md-6">
<label for="wprice">Phone: </label>
<input type="text" name="phone" id="phone" class="form-control input-sm" placeholder="Phone" title="Phone" required>
</div>
<div class="col-md-6">
<label for="cost_price">Address: </label>
<input type="text" name="address" id="address" class="form-control input-sm" placeholder="Address" title="Address" required>
</div>
</div>
</form>
Because you add your custom rule, so you must add message for custom rule. You can add message with set_message function of form validation library. Another way is define message in set_rules. See Setting Error Messages documentation.
$this->form_validation->set_rules('first_name', 'first_name', 'trim|alpha|required');
$this->form_validation->set_rules('last_name', 'last_name', 'trim|alpha|required');
$this->form_validation->set_rules('email', 'email', 'trim|valid_email|required');
$this->form_validation->set_rules('phone', 'phone', 'required|callback_phone');
$this->form_validation->set_rules('address', 'address', 'trim|alpha_numeric_spaces|required');
$this->form_validation->set_message('phone', 'Phone number is invalid!');
if ($this->form_validation->run() == FALSE) {
$this->json($this->form_validation->error_array());
} else {
if (empty($data)) {
$this->json(array('msg' => 'vacio'));
} else {
if (!$this->providers->isExistsProvider($data)) {
$this->providers->addProvider($data);
$this->json(array('msg' => 'successfully added'));
} else {
$this->json(array('msg' => 'The email is used by another provider'));
}
}
}

Fatal error: Call to undefined function form_open() line 104

I know most of you guys will realize this is a dumb question but I don't have any idea what's wrong with my code. I'm trying to create a contact form that will be sent into the database. But when I try to go to the contact_form_view.php it says Fatal error: Call to undefined function form_open() on Line 104
Here's the form code
<?php $attributes = array("name" => "contactform");
echo form_open("contactform/index", $attributes);?>
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" name="name" placeholder="Your Full Name" type="text" value="<?php echo set_value('name'); ?>" />
<span class="text-danger"><?php echo form_error('name'); ?></span>
</div>
<div class="form-group">
<label for="email">Email ID</label>
<input class="form-control" name="email" placeholder="Email-ID" type="text" value="<?php echo set_value('email'); ?>" />
<span class="text-danger"><?php echo form_error('email'); ?></span>
</div>
<div class="form-group">
<label for="subject">Subject</label>
<input class="form-control" name="subject" placeholder="Subject" type="text" value="<?php echo set_value('subject'); ?>" />
<span class="text-danger"><?php echo form_error('subject'); ?></span>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" rows="4" placeholder="Message"><?php echo set_value('message'); ?></textarea>
<span class="text-danger"><?php echo form_error('message'); ?></span>
</div>
<div class="form-group">
<button name="submit" type="submit" class="btn btn-success">Submit</button>
</div>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
Here's the php code
<?php
class contactform extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation'));
$this->load->database();
}
function index()
{
//set validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|callback_alpha_space_only');
$this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
$this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');
//run validation on post data
if ($this->form_validation->run() == FALSE)
{ //validation fails
$this->load->view('contact_form_view');
}
else
{
//insert the contact form data into database
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'subject' => $this->input->post('subject'),
'message' => $this->input->post('message')
);
if ($this->db->insert('contacts', $data))
{
// success
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">We received your message! Will get back to you shortly!!!</div>');
redirect('contactform/index');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Some Error. Please try again later!!!</div>');
redirect('contactform/index');
}
}
}
//custom callback to accept only alphabets and space input
function alpha_space_only($str)
{
if (!preg_match("/^[a-zA-Z ]+$/",$str))
{
$this->form_validation->set_message('alpha_space_only', 'The %s field must contain only alphabets and space');
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
if you please try to load form helper in your autoload.php like this
$autoload['helper'] = array('url', 'form');
i think you load the helper in the controller which have the action of your form not the controller that call the form.
so just add the helper to autoload.php and it will work

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.

Categories