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
Related
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 have problem that my input "type" is never post in database because I don't give input type in the view (I want value of my type is set by controller/hidden)... and this is my code
Controller :
<?php
public function addSmsCampaign() {
if (isset($_POST['addSmsCampaign'])) {
$this->form_validation->set_rules('campaign_name', 'campaign name', 'required|is_unique[campaigns.campaign_name]');
$this->form_validation->set_rules('sequence_qty', 'sequence quantity', 'required|integer');
$this->form_validation->set_rules('label_id', 'label id', 'required');
$this->form_validation->set_rules('type', '', 'required');
//if form validation true
if ($this->form_validation->run() == TRUE) {
$sms = 1;
$newcampaign = [
'campaign_name' => $_POST['campaign_name'],
'sequence_qty' => $_POST['sequence_qty'],
'label_id' => $_POST['label_id'],
'type' => $this->input->post('type'),
'created_at' => date('Y-m-d')
];
$this->db->insert('campaigns', $newcampaign);
redirect('userCont/sequenceform', 'refresh');
}
}
?>
and this is my view:
<form action="" method="POST">
<div class="form-group">
<label for="campaign_name">Input Campaign Title </label>
<input type="text" class="form-control" name="campaign_name" id="name">
</div>
<div class="form-group">
<label for="sequence_qty">Sequence qty </label>
<input type="text" class="form-control" name="sequence_qty" id="qty">
<input type="hidden" class="form-control" name="type" value="1">
</div>
<div class="form-group">
<label for="label_id">Choose Category</label>
<select class="form-control" name="label_id" id="label_id">
<?php
foreach ($label_content as $e) {
echo "<option value='$e->id;'>" . $e->label_name . "</option>";
}
?>
</select>
</div>
<div class="text-right">
<button class="btn btn-primary form-control" value="1" name="addSmsCampaign type">next</button>
</div><hr>
</form>
every time I post value of type is default = 0, and I want set value to 1...
thanks a lot
try changing name from 'type' to some other name in db, controller and view
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 new in codeigniter and I can't figure out why my form_validation keeps on returning false. Here are my codes:
Login Controller
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('user','',TRUE);
}
function index()
{
$this->load->view('login_view');
}
function user_registration_show(){
$this->load->view('registration_view');
}
function user_login_process(){
}
function user_registration_process(){
$this->form_validation->set_rules('fname', 'First Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('lname', 'Last Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('gender', 'Gender', 'trim|required|xss_clean');
$this->form_validation->set_rules('address', 'Address', 'trim|required|xss_clean');
$this->form_validation->set_rules('emailadd', 'Email Address', 'trim|required|xss_clean');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
$this->form_validation->set_rules('password1', 'Re-type Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('registration_view');
} else {
$pass = $this->input->post('password');
$pass1 = $this->input->post('password1');
if($pass!=$pass1){
$data['message_display'] = 'Password mismatched!';
}else{
$data = array(
'Fname' => $this->input->post('Fname'),
'Lname' => $this->input->post('Lname'),
'Gender' => $this->input->post('Gender'),
'Address' => $this->input->post('Address'),
'EmailAdd' => $this->input->post('EmailAdd'),
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
}
}
}
}
registration_view
<?php echo form_open('login/user_registration_process'); ?>
<div class="content-box-wrapper">
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" name="fname" placeholder="First Name" >
</div>
</div>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" name="lname" placeholder="Last Name" >
</div>
</div>
<div class="form-group">
<div class="input-group">
<select class="form-control" name="gender" >
<option disabled="disabled">Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div>
</div>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" name="address" placeholder="Address">
</div>
</div>
<div class="form-group">
<div class="input-group">
<input type="email" class="form-control" name="emailadd" placeholder="Email Address" >
</div>
</div>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" name="username" placeholder="Username" >
</div>
</div>
<div class="form-group">
<div class="input-group">
<input type="password" class="form-control" name="password" placeholder="Password" >
</div>
</div>
<div class="form-group">
<div class="input-group">
<input type="password" class="form-control" name="password1" placeholder="Re-type Password" >
</div>
</div>
<input type="submit" class="btn btn-blue-alt btn-block" value="Register"/>
</div>
<?php echo form_close(); ?>
I am using codeigniter 3.1.3. Can anyone help me?
Your form validation fails due to use of xss clean check in all the fields. If you remove that it validate your fields.As your data is already xss cleaned(assuming global_xss_filtering is TRUE in config)
I think there is not matching in field names at getting values using $this->input->post().So try like this..
$data = array(
'Fname' => $this->input->post('fname'),
'Lname' => $this->input->post('lname'),
'Gender' => $this->input->post('gender'),
'Address' => $this->input->post('address'),
'EmailAdd' => $this->input->post('emailadd'),
'username' => $this->input->post('username'),
'password' => $this->input->post('password')
);
Please check your all errors in controller. This will return cause of error.
<?php echo validation_errors(); ?>
OR
echo "<pre>";
print_r($this->form_validation->get_all_errors());
echo "</pre>";
This will return all form error in controller and you can easily handle your request.
With the help of sir #GauravRai and advice of sir #Hek-mat this is now my code.
Login controller
function __construct()
{
parent::__construct();
$this->load->helper(array('url','form'));
$this->load->library('form_validation');
$this->load->library('session');
$this->load->helper('security');
$this->load->model('user');
}
function index()
{
$this->load->view('login_view');
}
function user_registration_show(){
$this->load->view('registration_view');
}
function user_registration_process(){
$this->form_validation->set_rules('fname', 'First Name', 'required|xss_clean');
$this->form_validation->set_rules('lname', 'Last Name', 'required|xss_clean');
$this->form_validation->set_rules('gender', 'Gender', 'required|xss_clean');
$this->form_validation->set_rules('address', 'Address', 'required|xss_clean');
$this->form_validation->set_rules('emailadd', 'Email Address', 'required|xss_clean');
$this->form_validation->set_rules('username', 'Username', 'required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
$this->form_validation->set_rules('password1', 'Re-type Password', 'required|xss_clean|matches[password]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('registration_view');
} else {
$data = array(
'Fname' => $this->input->post('fname'),
'Lname' => $this->input->post('lname'),
'Gender' => $this->input->post('gender'),
'Address' => $this->input->post('address'),
'EmailAdd' => $this->input->post('emailadd'),
'username' => $this->input->post('username'),
'password' => MD5($this->input->post('password'))
);
$result = $this->user->registration_insert($data);
if($result){
$data['message_display'] = 'Registered Successfully! Please Login to your account.';
$this->load->view('login_view', $data);
}else{
$data['message_display'] = 'Username already exist!';
$this->load->view('registration_view', $data);
}
}
}
I added the $this->load->helper('security'); in order for the xss_clean to validate my form. I also added the matches[password] validation to match my password field and Re-type Password field. Thank you to all of you for helping me solving this problem :)
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();
}
}