I'm trying to input a record into my database. I can get all data except for one ['inputPicture'](which is the picture file name)
I've already tried using var_dum($_POST) and print_r($_POST) so I am sure that I cannot get anything from 'inputPicture'
This is what I get from both of them:
Array ( [inputFirstName] => Charlie [inputLastName] => Horse [inputContactNumber] => 09154447896 [inputAddress] => Candy Mountain [inputEmailAddress] => charlie#candymountain.com [action] => )
Here is my view code:
<form class="form-horizontal" role="form" method="POST" action="<?php echo base_url('Contacts/addcontact'); ?>" enctype="multipart/form-data">
<div class="form-group">
<label for="usr">First Name</label>
<input type="text" class="form-control" name="inputFirstName" placeholder="First Name">
</div>
<div class="form-group">
<label for="usr">Last Name</label>
<input type="text" class="form-control" name="inputLastName" placeholder="Last Name">
</div>
<div class="form-group">
<label for="usr">Contact Number</label>
<input type="text" class="form-control" name="inputContactNumber" placeholder="Contact Number">
</div>
<div class="form-group">
<label for="usr">Address:</label>
<input type="text" class="form-control" name="inputAddress" placeholder="Address">
</div>
<div class="form-group">
<label for="usr">Email Address:</label>
<input type="text" class="form-control" name="inputEmailAddress" placeholder="Email Address">
</div>
<div class="form-group">
<label for="usr">Picture:</label>
<input type="file" class="form-control" name="inputPicture"></br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="action">Add</button>
</div>
</form>
Here is my controller code:
public function addcontact(){
$first_name = $this->input->post('inputFirstName');
$last_name = $this->input->post('inputLastName');
$contact_number = $this->input->post('inputContactNumber');
$address = $this->input->post('inputAddress');
$email_address = $this->input->post('inputEmailAddress');
$image_url = $this->input->post('inputImage');
$this->form_validation->set_rules('inputFirstName', 'First Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputLastName', 'Last Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputContactNumber', 'Contact Number', 'required|exact_length[11]|numeric');
$this->form_validation->set_rules('inputAddress', 'Address', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('inputEmailAddress', 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');
if($this->form_validation->run() == FALSE){
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts_model->getContacts();
$this->load->view('home', $data);
redirect();
}
else{
if(!isset($_POST['inputImage'])){
$this->contacts_model->addContactsNoPic($first_name, $last_name, $contact_number, $address, $email_address);
}
else{
$image = 'assets/images/' . $image_url;
$this->contacts_model->addContacts($first_name, $last_name, $contact_number, $address, $email_address, $image);
}
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts_model->getContacts();
$this->load->view('home', $data);
redirect();
}
}
I have a similar method which is 'update'. It works perfectly fine. I tried copy pasting my code from the update to my addContact method but it still doesnt work.
Here is my view code for Update:
<form class="form-horizontal" role="form" method="POST" action="<?php echo base_url('Contacts/update'); ?>" enctype="multipart/form-data">
<div class="form-group hidden">
<label for="usr">ID</label>
<input type="text" class="form-control" name="inputID" id="id">
</div>
<div class="form-group">
<label for="usr">First Name</label>
<input type="text" class="form-control" name="inputFirstName" id="firstname">
</div>
<div class="form-group">
<label for="usr">Last Name</label>
<input type="text" class="form-control" name="inputLastName" id="lastname">
</div>
<div class="form-group">
<label for="usr">Contact Number</label>
<input type="text" class="form-control" name="inputContactNumber" id="contactnumber">
</div>
<div class="form-group">
<label for="usr">Address:</label>
<input type="text" class="form-control" name="inputAddress" id="address">
</div>
<div class="form-group">
<label for="usr">Email Address:</label>
<input type="text" class="form-control" name="inputEmailAddress" id="emailaddress">
</div>
<div class="form-group">
<label for="usr">Picture:</label>
<input type="file" class="form-control" name="inputPicture" id="picture"></br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="action">Update</button>
</div>
</form>
and here is my controller
public function update(){
$first_name = $this->input->post('inputFirstName');
$last_name = $this->input->post('inputLastName');
$contact_number = $this->input->post('inputContactNumber');
$address = $this->input->post('inputAddress');
$email_address = $this->input->post('inputEmailAddress');
$image_url = $this->input->post('inputPicture');
$id = $this->input->post('inputID');
var_dump($_POST);exit;
$this->form_validation->set_rules('inputFirstName', 'First Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputLastName', 'Last Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputContactNumber', 'Contact Number', 'required|exact_length[11]|numeric');
$this->form_validation->set_rules('inputAddress', 'Address', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('inputEmailAddress', 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');
if($this->form_validation->run() == FALSE){
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts_model->getContacts();
$this->load->view('home', $data);
redirect();
}
else{
if(!isset($_POST['inputPicture'])){
$this->contacts_model->updateContactNoPic($id, $first_name, $last_name, $contact_number, $address, $email_address);
}
else{
$image = 'assets/images/' . $image_url;
$this->contacts_model->updateContact($id, $first_name, $last_name, $contact_number, $address, $email_address, $image);
}
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts_model->getContacts();
$this->load->view('home', $data);
redirect();
}
}
That's because contents of input type=file sent by GET/POST methods from HTML to PHP are stored inside the superglobal variable $_FILES and not $_POST (unless you don't define enctype property of the form tag as "multipart/form-data", which then causes the filename to be passed as a string to GET/POST).
If you var_dump($_FILES)/print_r($_FILES) you'll see an array like this:
Array
(
[file] => Array
(
[name] => test.pdf
[type] => application/pdf
[tmp_name] => C:\Windows\Temp\php1485.tmp
[error] => 0
[size] => 1073054
)
)
OBS: be sure to have enctype="multipart/form-data" as property of your form and file_uploads set to on in your php.ini file.
use $_FILES['inputPicture']
public function update(){
$first_name = $this->input->post('inputFirstName');
$last_name = $this->input->post('inputLastName');
$contact_number = $this->input->post('inputContactNumber');
$address = $this->input->post('inputAddress');
$email_address = $this->input->post('inputEmailAddress');
//$image_url = $this->input->post('inputPicture');
$image_url = $_FILES['inputPicture'];
$id = $this->input->post('inputID');
var_dump($_POST);exit;
$this->form_validation->set_rules('inputFirstName', 'First Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputLastName', 'Last Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputContactNumber', 'Contact Number', 'required|exact_length[11]|numeric');
$this->form_validation->set_rules('inputAddress', 'Address', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('inputEmailAddress', 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');
if($this->form_validation->run() == FALSE){
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts_model->getContacts();
$this->load->view('home', $data);
redirect();
}
else{
if(!isset($_FILES['inputPicture'])){
$this->contacts_model->updateContactNoPic($id, $first_name, $last_name, $contact_number, $address, $email_address);
}
else{
$image = 'assets/images/' . $image_url;
$this->contacts_model->updateContact($id, $first_name, $last_name, $contact_number, $address, $email_address, $image);
}
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts_model->getContacts();
$this->load->view('home', $data);
redirect();
}
}
Related
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();
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.
I want to update user data in the Code-igniter (user details)
below is my code for more checking . please check it out why I get error when i push "update" button.
note: I used some of these information in my view that I didn't copied theme for now (no need in-fact )
controller:
public function edit(){
//validation form
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|min_length[4]|xss_clean');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique[users.email]');
$this->form_validation->set_rules('username','Username','trim|required|min_length[3]|max_length[16]');
$this->form_validation->set_rules('password','Password','trim|min_length[3]|max_length[50]');
$this->form_validation->set_rules('password2','Confirm Password','trim|matches[password]');
$this->form_validation->set_rules('cellphone','Cellphone Number','trim|required');
$this->form_validation->set_rules('activation_code','Activation Code','trim');
//$data['groups'] = $this->User_model->get_groups();
//$data['user'] = $this->User_model->get_user($uid);
if($this->form_validation->run() == FALSE) {
//view
$data = array(
'userid' => $this->session->userdata('user_id')->id,
'first_name' => $this->session->userdata('user_id')->first_name,
'last_name' => $this->session->userdata('user_id')->last_name,
'fathers_name' => $this->session->userdata('user_id')->fathers_name,
'id_card_number' => $this->session->userdata('user_id')->id_card_number,
'national_number' => $this->session->userdata('user_id')->national_code,
'national_code' => $this->session->userdata('user_id')->national_code,
'age' => $this->session->userdata('user_id')->age,
'gender' => $this->session->userdata('user_id')->gender,
'is_addict' => $this->session->userdata('user_id')->is_addict,
'hiv_status' => $this->session->userdata('user_id')->hiv_status,
'hepatitis_status' => $this->session->userdata('user_id')->hepatitis_status,
'email' => $this->session->userdata('user_id')->email,
'needed_services' => $this->session->userdata('user_id')->needed_services,
'username' => $this->session->userdata('user_id')->username,
);
//$data['last_name'] = $this->session->userdata('user_id')->last_name;
//$data['data'] = $this->Profile_Model->get_user_profile();
//$this->base->set_message($msg,$type);
redirect(site_url('admin/profile'),'refresh');
$data['courses'] = $this->Profile_Model->get_user_profile();
//load view
$data['main_content'] = 'profile/index';
$this->load->view('profile/layouts/main', $data);
}else{
//create articles data array
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'cellphone' => $this->input->post('cellphone'),
'group_id' => 2,
);
if($this->input->post('user_id') == $this->session->userdata('user_id')->id){
$uid = $this->input->post('user_id');
}
$password = $this->input->post('password');
$password2 = $this->input->post('password2');
if($password != '' && $password = $password2 ){
$data['password'] = md5($this->input->post('password'));
}
if($this->input->post('activation_code') !=''){
$data['activation_code'] = $this->input->post('activation_code');
}
$this->Profile_Model->update($data, $uid);
//Create Message
$this->session->set_flashdata('User_saved','You have successfully registered');
//redirect to page
redirect('admin/profile');
}
}
also below is my model snippet code:
model:
public function update($data, $uid)
{
$this->db->where('id', $uid);
$result = $this->db->update($this->table_users, $data);
return $result;
}
and below is my view form:
view:
<?php echo form_open(base_url('admin/profile/edit'),'class="form-horizontal"');?>
<?php $uid = $this->session->userdata('user_id')->id;
$uid = intval($uid);
?>
<div class="form-group">
<label class="col-sm-1 control-label" for="first_name">نام</label>
<input type="hidden" name="user_id" value="<?php echo $uid;?>">
<div class="col-sm-5">
<input type="text" id="first_name" name="first_name" class="form-control" value="<?php echo set_value('first_name',$first_name);?>">
</div>
<label class="col-sm-1 control-label" for="last_name">نام خانوادگی</label>
<div class="col-sm-5">
<input type="text" id="last_name" name="last_name" class="form-control" value="<?php echo set_value('last_name',$last_name);?>">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label" for="email">ایمیل</label>
<div class="col-sm-5">
<input type="email" id="email" name="email" class="form-control" value="<?php /** #var dbObject $email */
echo set_value('email',$email);?>">
</div>
<label class="col-sm-1 control-label" for="username">نام کاربری</label>
<div class="col-sm-5">
<input type="text" class="form-control" value="<?php echo $username;?>" disabled aria-disabled="true" title="You cant't change your username" />
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label" for="password">رمز ورود</label>
<div class="col-sm-5">
<input type="password" id="password" name="password" class="form-control">
</div>
<label class="col-sm-1 control-label" for="password2">تایید رمز</label>
<div class="col-sm-5">
<input type="password" id="password2" name="password2" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label" for="cellphone">شماره تلفن همراه</label>
<div class="col-sm-5">
<input type="tel" id="cellphone" name="cellphone" class="form-control">
</div>
<label class="col-sm-1 control-label" for="cellphone_activation">کد تایید پیامکی</label>
<div class="col-sm-5">
<input type="text" id="cellphone_activation" name="cellphone_activation" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-sm-5 col-sm-offset-1">
<p>
<button class="btn mybtn btn-custom" type="submit">ویرایش</button>
</p>
</div>
</div>
<?php echo form_close();?>
Model Should be like this write table name instead of $this->table_users
public function update($data, $uid)
{
$this->db->where('id', $uid);
$result = $this->db->update(user, $data);
return $result;
}
I am trying to add a record to my database and validate my form. However my form_validation always returns false.
This is my view. I am using a modal to hold this form.
<form class="form-horizontal" role="form" method="GET" action="<?php echo base_url('Contacts_/addcontact'); ?>">
<div class="form-group">
<label for="usr">First Name</label>
<input type="text" class="form-control" name="inputFirstName" placeholder="First Name">
</div>
<div class="form-group">
<label for="usr">Last Name</label>
<input type="text" class="form-control" name="inputLastName" placeholder="Last Name">
</div>
<div class="form-group">
<label for="usr">Contact Number</label>
<input type="text" class="form-control" name="inputContactNumber" placeholder="Contact Number">
</div>
<div class="form-group">
<label for="usr">Address:</label>
<input type="text" class="form-control" name="inputAddress" placeholder="Address">
</div>
<div class="form-group">
<label for="usr">Email Address:</label>
<input type="text" class="form-control" name="inputEmailAddress" placeholder="Contact Number">
</div>
<div class="form-group">
<label for="usr">Picture:</label>
<input type="file" class="form-control" name="inputPicture"></br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="action">Add</button>
</div>
</form>
This is my controller
public function addcontact(){
$this->load->helper('url');
$this->load->model('contacts');
$this->load->library('form_validation');
$first_name = $this->input->get('inputFirstName');
$last_name = $this->input->get('inputLastName');
$contact_number = $this->input->get('inputContactNumber');
$address = $this->input->get('inputAddress');
$email_address = $this->input->get('inputEmailAddress');
$this->form_validation->set_rules($first_name, 'First Name', 'required|max_length[35]');
$this->form_validation->set_rules($last_name, 'Last Name', 'required|max_length[35]');
$this->form_validation->set_rules($contact_number, 'Contact Number', 'required|exact_length[11]|numeric');
$this->form_validation->set_rules($address, 'Address', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules($email_address, 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');
if($this->form_validation->run() == TRUE){
if(!isset($_GET['inputPicture'])){
$this->contacts->addContactsNoPic($first_name, $last_name, $contact_number, $address, $email_address);
}
else{
$image = 'assets/images/' . $_GET['inputPicture'];
$this->contacts->addContacts($first_name, $last_name, $contact_number, $address, $email_address, $image);
}
$data['title'] = 'Address Book';
$data['contacts_info'] = $this->contacts->getContacts();
$this->load->view('home', $data);
}
else{
$data['title'] = 'Address Book';
$data['err_add'] = 1;
$data['contacts_info'] = $this->contacts->getContacts();
$this->load->view('home', $data);
}
}
And this is my model. I have two functions, one if you did not input a picture and one if u didn't.
function addContacts($first_name, $last_name, $contact_number, $address, $email_address, $image){
$new_contact_data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'contact_number' => $contact_number,
'address' => $address,
'email_address' => $email_address,
'picture' => $image,
);
$this->db->insert('contacts', $new_contact_data);
}
function addContactsNoPic($first_name, $last_name, $contact_number, $address, $email_address){
$new_contact_data = array(
'first_name' => $first_name,
'last_name' => $last_name,
'contact_number' => $contact_number,
'address' => $address,
'email_address' => $email_address,
);
$this->db->insert('contacts', $new_contact_data);
}
Thank you for the help
PS: I even tried setting all my rules to just 'required' still my form_validation always returns false no matter what i input.
I also checked my $this->input->get values and they are just fine, they get the values that i inputted.
First parameter in set_rules method of form_validation class should be name of field, but you are passing dynamically created values instead. Your rules should be like:
$this->form_validation->set_rules('inputFirstName', 'First Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputLastName', 'Last Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputContactNumber', 'Contact Number', 'required|exact_length[11]|numeric');
$this->form_validation->set_rules('inputAddress', 'Address', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('inputEmailAddress', 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');
Also, your form tag has no enctype="multipart/form-data" which is needed for forms that uploading data.
And third but most important, when using upload form you would like to use POST method. Docs.
The correct syntax is:
$this->form_validation->set_rules('inputFirstName', 'First Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputLastName', 'Last Name', 'required|max_length[35]');
$this->form_validation->set_rules('inputContactNumber', 'Contact Number', 'required|exact_length[11]|numeric');
$this->form_validation->set_rules('inputAddress', 'Address', 'required|min_length[5]|max_length[255]');
$this->form_validation->set_rules('inputEmailAddress', 'Email Address', 'required|min_length[10]|max_length[255]|valid_email');
http://www.codeigniter.com/user_guide/libraries/form_validation.html
I have a edit function on my users codeigniter project, I would like to be able to use that one function for uploading images as well but user guide says to create a new function.
How am I able to make it with same edit() function.
function edit($user_id = 0) {
$this->load->model('users/model_user');
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$data['last_updated'] = $this->model_user->last_updated($user_id);
$data['user_id'] = $user_id;
if (!empty($this->input->post('username'))) {
$data['username'] = $this->input->post('username');
} else {
$data['username'] = $this->model_user->getUserByUsername($user_id);
}
if (!empty($this->input->post('firstname'))) {
$data['firstname'] = $this->input->post('firstname');
} else {
$data['firstname'] = $this->model_user->getUserByFirstname($user_id);
}
if (!empty($this->input->post('lastname'))) {
$data['lastname'] = $this->input->post('lastname');
} else {
$data['lastname'] = $this->model_user->getUserByLastname($user_id);
}
if (!empty($this->input->post('email'))) {
$data['email'] = $this->input->post('email');
} else {
$data['email'] = $this->model_user->getUserByEmail($user_id);
}
// User userfile
if(!empty($this->input->post('userfile'))) {
$data['userfile'] = $this->input->post('userfile');
} else {
$data['userfile'] = $this->model_user->getUserImage($user_id);
}
$config['upload_path'] = base_url('image/catalog/'); // base_url('image/catalog/');
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
} else {
$data['upload_data'] = $this->upload->data();
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
$this->load->library('password');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Your Username');
$this->form_validation->set_rules('firstname', 'Your First Name');
$this->form_validation->set_rules('lastname', 'Your Last Name');
$this->form_validation->set_rules('email', 'Your Email');
if ($this->form_validation->run() == TRUE) {
$this->model_user->editUser($user_id, $data);
redirect('users');
} else {
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
}
View File
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php echo $error ;?>
<?php
$data = array(
'role' => "form",
'class' => 'form-horizontal'
);
echo form_open('users/edit/' . $user_id, $data);
?>
<div class="form-group">
<label for="input-username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" name="username" value="<?php echo $username;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-firstname" class="col-sm-2 control-label">Firstname</label>
<div class="col-sm-10">
<input type="text" name="firstname" value="<?php echo $firstname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-lastname" class="col-sm-2 control-label">Lastname</label>
<div class="col-sm-10">
<input type="text" name="lastname" value="<?php echo $lastname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" name="email" value="<?php echo $email;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-image"></label>
<div class="col-lg-3">
<div class="thumbnail">
<img src="<?php echo $userfile;?>">
</div>
</div>
</div>
<div class="form-group">
<label for="input-image" class="col-sm-2 control-label">Image</label>
<div class="col-sm-10">
<br>
<input type="file" name="userfile" value="<?php echo $userfile;?>" size="20">
</div>
</div>
<div class="form-group">
<div class="text-right">
<h2><a href="<?php echo base_url('users/edit_password/' . $user_id);?>">Change Your Password</h2>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
<?php echo form_close();?>
I have got it to work with out using codeigniter upload image helper easier this way.
controller function
function edit($user_id = 0) {
$this->load->model('users/model_user');
$data['title'] = "Users";
$data['base'] = config_item('HTTP_SERVER');
$data['isLogged'] = $this->user->isLogged();
$data['last_updated'] = $this->model_user->last_updated($user_id);
$data['user_id'] = $user_id;
if (!empty($this->input->post('username'))) {
$data['username'] = $this->input->post('username');
} else {
$data['username'] = $this->model_user->getUserByUsername($user_id);
}
if (!empty($this->input->post('firstname'))) {
$data['firstname'] = $this->input->post('firstname');
} else {
$data['firstname'] = $this->model_user->getUserByFirstname($user_id);
}
if (!empty($this->input->post('lastname'))) {
$data['lastname'] = $this->input->post('lastname');
} else {
$data['lastname'] = $this->model_user->getUserByLastname($user_id);
}
if (!empty($this->input->post('email'))) {
$data['email'] = $this->input->post('email');
} else {
$data['email'] = $this->model_user->getUserByEmail($user_id);
}
if(!empty($this->input->post('image'))) {
$data['image'] = $this->input->post('image');
} else {
$data['image'] = $this->model_user->getUserImage($user_id);
}
$data['image_path'] = config_item('HTTP_CATALOG') . '/image/catalog/';
$this->load->library('password');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Your Username');
$this->form_validation->set_rules('firstname', 'Your First Name');
$this->form_validation->set_rules('lastname', 'Your Last Name');
$this->form_validation->set_rules('email', 'Your Email');
if ($this->form_validation->run() == TRUE) {
$this->model_user->editUser($user_id, $data);
redirect('users');
} else {
$data['header'] = $this->load->view('template/common/header', $data, TRUE);
$data['footer'] = $this->load->view('template/common/footer', NULL, TRUE);
return $this->load->view('template/users/users_form', $data);
}
}
View
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<?php
$data = array(
'role' => "form",
'class' => 'form-horizontal'
);
echo form_open('users/edit/' . $user_id, $data);
?>
<div class="form-group">
<label for="input-username" class="col-sm-2 control-label">Username</label>
<div class="col-sm-10">
<input type="text" name="username" value="<?php echo $username;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-firstname" class="col-sm-2 control-label">Firstname</label>
<div class="col-sm-10">
<input type="text" name="firstname" value="<?php echo $firstname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-lastname" class="col-sm-2 control-label">Lastname</label>
<div class="col-sm-10">
<input type="text" name="lastname" value="<?php echo $lastname;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label for="input-email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="text" name="email" value="<?php echo $email;?>" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input-image"></label>
<div class="col-lg-3">
<div class="thumbnail">
<img src="<?php echo $image_path . $image; ?>" style=" width: 100%; height: 300px;">
</div>
</div>
</div>
<div class="form-group">
<label for="input-image" class="col-sm-2 control-label">Image</label>
<div class="col-sm-10">
<br>
<input type="file" name="image" value="<?php echo $image;?>" size="20">
</div>
</div>
<div class="form-group">
<div class="text-right">
<h2><a href="<?php echo base_url('users/edit_password/' . $user_id);?>">Change Your Password</h2>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</div>
<?php echo form_close();?>