I'm probably missing a real simple point here but would one of you Codeigniter gurus' be able to help me with my issue. I have a userform which stores information for a sales page, which holds both text and images saved to a database.
I am able to save one image to the database fine, but multiple i'm struggling. Currently all the images are being transferred to the correct folder but I need them to store in the MySQL table.
Form:
<div class="form-group">
<label class="col-lg-4 control-label">Avonics:</label>
<div class="col-lg-6">
<?php
$avonics6 = array(
'name' => 'avonics6',
'class' => 'form-control',
'placeholder' => 'Avonics',
'value' => set_value('avonics6')
);
echo form_input($avonics6);
?>
</div>
</div>
</fieldset>
<fieldset class="fieldset_class">
<legend><strong><font color="black">Aircraft Condition</font></strong></legend>
<div class="form-group">
<label class="col-lg-4 control-label">Interior:</label>
<div class="col-lg-6">
<?php
$interior = array(
'name' => 'interior',
'class' => 'form-control',
'placeholder' => 'Interior 0-100%',
'value' => set_value('interior')
);
echo form_input($interior);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Exterior:</label>
<div class="col-lg-6">
<?php
$exterior = array(
'name' => 'exterior',
'class' => 'form-control',
'placeholder' => 'Exterior 0-100%',
'value' => set_value('exterior')
);
echo form_input($exterior);
?>
</div>
</div>
</fieldset>
<fieldset class="fieldset_class">
<legend><strong><font color="black">Upload Images</font></strong></legend>
<div class="form-group">
<label class="col-lg-4 control-label">Add Image (1)</label>
<div class="col-lg-6">
<?php
$aircraft1 = array(
'name' => 'userfile[]',
'class' => 'form-control',
'id' => 'userfile',
'type' => 'file',
'multiple' => ''
);
echo form_upload($aircraft1);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-4 control-label">Add Image (2)</label>
<div class="col-lg-6">
<?php
$aircraft2 = array(
'name' => 'userfile[]',
'class' => 'form-control',
'id' => 'userfile',
'type' => 'file',
'multiple' => 'true'
);
echo form_upload($aircraft2);
?>
</div>
</div>
</fieldset>
<div class="form-group">
<label class="col-lg-4 control-label"></label>
<div class="col-lg-8">
<?php echo form_submit('submit', 'Add Aircraft', 'class="btn btn-danger"');?>
</div>
</div>
<?php
echo form_close();
?>
</div>
</div><div class="clearfix"></div>
Controller:
function add_aircraft()
{
$this->load->library('form_validation');
/* handle form data then send to model */
$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('price', 'Price', 'trim|required');
$this->form_validation->set_rules('year', 'Year', 'trim|required');
$this->form_validation->set_rules('annual', 'Annual', 'trim|required');
$this->form_validation->set_rules('serial_number', 'Serial number', 'trim|required');
$this->form_validation->set_rules('airframe_hours', 'Airframe hours', 'trim|required');
$this->form_validation->set_rules('engine_type', 'Engine type', 'trim|required');
$this->form_validation->set_rules('engine_hours', 'Engine hours', 'trim|required');
$this->form_validation->set_rules('propeller_type', 'Propeller type', 'trim|required');
$this->form_validation->set_rules('notes', 'Sales Pitch', 'trim|required');
$this->form_validation->set_rules('avonics', 'Avonics', 'trim');
$this->form_validation->set_rules('avonics1', 'Avonics', 'trim');
$this->form_validation->set_rules('avonics2', 'Avonics', 'trim');
$this->form_validation->set_rules('avonics3', 'Avonics', 'trim');
$this->form_validation->set_rules('avonics4', 'Avonics', 'trim');
$this->form_validation->set_rules('avonics5', 'Avonics', 'trim');
$this->form_validation->set_rules('avonics6', 'Avonics', 'trim');
$this->form_validation->set_rules('interior', 'Interior', 'trim|required');
$this->form_validation->set_rules('exterior', 'Exterior', 'trim|required');
$this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');
if($this->form_validation->run() == FALSE)
{
$data['main_content'] = 'control/aircraftsales/add';
$this->load->view('control/includes/template_simple_header_footer', $data);
$name_array[] = $data['aircraft1'];
}
else
{
$this->load->model('control/Aircraftsales_model');
if($this->Aircraftsales_model->add_aircraft()){
$data['main_content'] = 'control/aircraftsales/add';
$this->load->view('control/includes/template_simple_header_footer', $data);
}
}
}
Model (this seems to be the issue??):
function add_aircraft()
{
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key=>$value)
for($s=0; $s<=$count-1; $s++)
{
$_FILES['userfile']['name']=$value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
/* config image */
$config['upload_path'] = './upload/aircraftsales';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
$names= implode(',', $name_array);
}
$aircraft = array(
'title' => $this->input->post('title'),
'price' => $this->input->post('price'),
'year' => $this->input->post('year'),
'annual' => $this->input->post('annual'),
'serial_number' => $this->input->post('serial_number'),
'airframe_hours' => $this->input->post('airframe_hours'),
'engine_type' => $this->input->post('engine_type'),
'engine_hours' => $this->input->post('engine_hours'),
'propeller_type' => $this->input->post('propeller_type'),
'notes' => $this->input->post('notes'),
'avonics' => $this->input->post('avonics'),
'avonics1' => $this->input->post('avonics1'),
'avonics2' => $this->input->post('avonics2'),
'avonics3' => $this->input->post('avonics3'),
'avonics4' => $this->input->post('avonics4'),
'avonics5' => $this->input->post('avonics5'),
'avonics6' => $this->input->post('avonics6'),
'interior' => $this->input->post('interior'),
'exterior' => md5($this->input->post('exterior')),
);
$insert = $this->db->insert('aircraftsales', $aircraft);
return $insert;
}
}
In your code you use return $insert;.
return act as exit. So when its reach it will stop the foreach and return value imidiatly to controller.
And you no need to use for() loop inside foreach loop
Related
So I have this form in which a user can update his information:
The problem comes when some of these inputs are optional. Let's say a user updates his email and password, then the update updates but when a user only edits his email and leave the password inputs blank the password in the database should not be updated....it currently changes the password as well even when they're empty.
Here is my HTML:
<?php echo validation_errors('<p class="alert alert-dismissable alert-danger">'); ?>
<?php echo form_open('users/edit/'.$item->id); ?>
<div class="nav-tabs-custom">
<ul class="nav nav-tabs">
<li class="active">Basics</li>
<li class="">About Me</li>
</ul>
<br>
<div class="tab-content">
<!-- Basics -->
<div class="tab-pane active" id="basics">
<!-- Email -->
<div class="form-group">
<?php echo form_label('Email', 'email'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-envelope" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'email',
'id' => 'email',
'maxlength' => '150',
'class' => 'form-control',
'value' => $item->email,
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Avatar Image -->
<div class="form-group">
<?php echo form_label('Avatar Image URL', 'avatar_img'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-id-card-o" aria-hidden="true"></i></i></div>
<?php
$data = array(
'name' => 'avatar_img',
'id' => 'avatar_img',
'class' => 'form-control',
'placeholder' => '96x96 Pixels',
'value' => $item->avatar_img
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Cover Image -->
<div class="form-group">
<?php echo form_label('Cover Img URL', 'cover_img'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-id-card-o" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'cover_img',
'id' => 'cover_img',
'class' => 'form-control',
'value' => $item->cover_img
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Occupation -->
<div class="form-group">
<?php echo form_label('Occupation', 'occupation'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-briefcase" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'occupation',
'id' => 'occupation',
'class' => 'form-control',
'value' => $item->occupation
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Website -->
<div class="form-group">
<?php echo form_label('Website', 'website'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-link" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'website',
'id' => 'website',
'class' => 'form-control',
'value' => $item->website
);
?>
<?php echo form_input($data); ?>
</div>
</div>
<!-- Password -->
<div class="form-group">
<?php echo form_label('Password', 'password'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-lock" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'password',
'id' => 'password',
'class' => 'form-control',
'value' => set_value('password'),
);
?>
<?php echo form_password($data); ?>
</div>
</div>
<!-- Password2 -->
<div class="form-group">
<?php echo form_label('Confirm Password', 'password2'); ?>
<div class="input-group date"><div class="input-group-addon"><i class="fa fa-lock" aria-hidden="true"></i></div>
<?php
$data = array(
'name' => 'password2',
'id' => 'password2',
'class' => 'form-control',
'value' => set_value('password2'),
);
?>
<?php echo form_password($data); ?>
</div>
</div>
</div>
</div>
</div>
<?php echo form_submit('mysubmit', 'Update User', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>
and here is what I have tried:
<?php
$data = array(
'email' => $this->input->post('email'),
'avatar_img' => $this->input->post('avatar_img'),
'cover_img' => $this->input->post('cover_img'),
'occupation' => $this->input->post('occupation'),
'website' => $this->input->post('website'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'password2' => password_hash($this->input->post('password2'), PASSWORD_DEFAULT),
);
if($this->input->post('password') != ''){
$data['password'] = ($this->input->post('password') && !empty($this->input->post('password'))) ? $this->input->post('password') : NULL;
}
if($this->input->post('password2') != ''){
$data['password2'] = ($this->input->post('password2') && !empty($this->input->post('password2'))) ? $this->input->post('password2') : NULL;
}
// Update User
$this->User_model->update($id, $data);
?>
but it just don't work, so I tried making more simple:
<?php
$data = array(
'email' => $this->input->post('email'),
'avatar_img' => $this->input->post('avatar_img'),
'cover_img' => $this->input->post('cover_img'),
'occupation' => $this->input->post('occupation'),
'website' => $this->input->post('website'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'password2' => password_hash($this->input->post('password2'), PASSWORD_DEFAULT),
);
if($this->input->post('password') != ''){
$data['password2'] = password_hash($this->input->post('password2'), PASSWORD_DEFAULT);
}
if($this->input->post('password2') != ''){
$data['password2'] = password_hash($this->input->post('password2'), PASSWORD_DEFAULT);
}
// Update User
$this->User_model->update($id, $data);
?>
Update method in Userr model:
public function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update($this->table, $data);
}
Thanks in advance.
If you want the password(s) to be updated on database only if the password field(s) are NOT empty, do it like below:
<?php
$data = array(
'email' => $this->input->post('email'),
'avatar_img' => $this->input->post('avatar_img'),
'cover_img' => $this->input->post('cover_img'),
'occupation' => $this->input->post('occupation'),
'website' => $this->input->post('website')
);
if(trim($this->input->post('password')) != ''){
$data['password'] = password_hash(trim($this->input->post('password')), PASSWORD_DEFAULT);
}
if(trim($this->input->post('password2')) != ''){
$data['password2'] = password_hash(trim($this->input->post('password2')), PASSWORD_DEFAULT);
}
// Update User
$this->User_model->update($id, $data);
?>
I removed "password" and "password2" from first array list and kept in condition check. I added trim in case the fields have white-spaces in it.
I hope this will work!
edit this code.
$data = array(
'email' => $this->input->post('email'),
'avatar_img' => $this->input->post('avatar_img'),
'cover_img' => $this->input->post('cover_img'),
'occupation' => $this->input->post('occupation'),
'website' => $this->input->post('website'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
'password2' => password_hash($this->input->post('password2'), PASSWORD_DEFAULT),
);
to
if(!empty($this->input->post('email'))){
$data['email'] = $this->input->post('email');
}
if(!empty($this->input->post('avatar_img'))){
$data['avatar_img'] = $this->input->post('avatar_img');
}
if(!empty($this->input->post('cover_img'))){
$data['cover_img'] = $this->input->post('cover_img');
}
if(!empty($this->input->post('occupation'))){
$data['occupation'] => $this->input->post('occupation');
}
if(!empty($this->input->post('website'))){
$data['website'] = $this->input->post('website');
}
if(!empty($this->input->post('password'))){
$data['password'] = password_hash($this->input->post('password'), PASSWORD_DEFAULT);
}
if(!empty($this->input->post('password2'))){
$data['password2'] = password_hash($this->input->post('password2'), PASSWORD_DEFAULT);
}
I am making a CRUD application using Codeigniter 3.
Whenever I edit a record and the data is invalid, instead of just showing the validation error messages, the browser outputs the message: Trying to get property of non-object.
My update function inside the controller looks like this:
public function update($customer_id) {
// data validation
$this->form_validation->set_rules('first_name', 'First name', 'required');
$this->form_validation->set_rules('last_name', 'Last name', 'required');
$this->form_validation->set_rules('email', 'Email address', 'required|valid_email');
$this->form_validation->set_rules('phone', 'Phone number', 'required');
$this->form_validation->set_rules('country', 'Country', 'required');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
if ($this->form_validation->run()) {
$data = [
// insert into these database table fields
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone'),
'country' => $this->input->post('country'),
'city' => $this->input->post('city'),
'address' => $this->input->post('address')
];
$this->load->model('Customer');
if ($this->Customer->updateCustomer($customer_id, $data)) {
$this->session->set_flashdata('update-response','Customer successfully updated');
} else {
$this->session->set_flashdata('update-response','Failed to update customer');
}
redirect('home');
} else {
$data = [
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
'phone' => $this->input->post('phone'),
'country' => $this->input->post('country'),
'city' => $this->input->post('city'),
'address' => $this->input->post('address'),
'id' => $customer_id
];
$this->load->view('update', ['customer' => $data]);
}
}
If the data in the update form is valid there are no errors. the form looks like this:
<?php echo form_open("home/update/{$customer->id}"); ?>
<div class="form-group <?php if(form_error('first_name')) echo 'has-error';?>">
<?php echo form_input('first_name', set_value('first_name', $customer->first_name),[
'id' => 'first_name',
'class' => 'form-control'
]);
if(form_error('first_name')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('first_name'); ?>
</div>
<div class="form-group <?php if(form_error('last_name')) echo 'has-error';?>">
<?php echo form_input('last_name', set_value('last_name', $customer->last_name), [
'id' => 'last_name',
'class' => 'form-control'
]);
if(form_error('last_name')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('last_name'); ?>
</div>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<?php echo form_input('email', set_value('email', $customer->email), [
'id' => 'email',
'class' => 'form-control'
]);
if(form_error('email')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('email'); ?>
</div>
<div class="form-group <?php if(form_error('phone')) echo 'has-error';?>">
<?php echo form_input('phone', set_value('phone', $customer->phone), [
'id' => 'phone',
'class' => 'form-control'
]);
if(form_error('phone')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('phone'); ?>
</div>
<div class="form-group <?php if(form_error('country')) echo 'has-error';?>">
<?php echo form_input('country', set_value('country', $customer->country), [
'id' => 'country',
'class' => 'form-control'
]);
if(form_error('country')) echo '<span class="glyphicon glyphicon-remove"></span>';
echo form_error('country'); ?>
</div>
<div class="form-group">
<?php echo form_input('city', set_value('city', $customer->city), [
'id' => 'city',
'class' => 'form-control'
]);
?>
</div>
<div class="form-group">
<?php echo form_input('address', set_value('city', $customer->address), [
'id' => 'address',
'class' => 'form-control'
]);
?>
</div>
<div class="form-group">
<?php echo form_submit('submit', 'Save', 'class = "btn btn-success btn-block"'); ?>
</div>
<?php echo form_close(); ?>
I believe $customer is turned into an associative array and this is the reason for the error message: Trying to get property of non-object. But what is causing this conversion? Or is the cause of the problem completely different?
But what is causing this conversion?
Nothing, you pass an array and you get an array which you try to access like an object.
In your example, $data is an array, but in your view you are accessing it like an object.
If you really want to access it like an object, cast the array to object:
$this->load->view('update', ['customer' => (object)$data]);
I am making 2 step registration. I have done the first step, now I am doing the second one. I want to make ajax form validation, but it gives the error right away opening the page and the error is at the top of page
Also pressing submit button it gives no errors despite empty fields.
Here is my view:
<div id="messages"></div>
<?php $attributes = array('class' => 'rex-forms', 'name' => 'continueregistrationform', 'id' => 'continueregistrationform'); ?>
<?= form_open_multipart('user/continueregistration', $attributes); ?>
<div class="container-fluid">
<div class="row">
<div class="col-md-7">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user" aria-hidden="true"></i></span>
<input id="name" type="text" class="form-control" name="name" placeholder="Name" value="<?= $instructors['name']; ?>">
</div><br>
</div>
<div class="col-md-6 col-sm-6">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-globe" aria-hidden="true"></i></span>
<input id="web" type="text" class="form-control" name="web" placeholder="Web-site" value="<?= $instructors['web']; ?>">
</div><br>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-phone" aria-hidden="true"></i></span>
<input id="tel" type="text" class="form-control" name="tel" placeholder="Phone" value="<?= $instructors['phone']; ?>">
</div><br>
</div>
<div class="col-md-6 col-sm-6">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-map-marker" aria-hidden="true"></i></span>
<input id="address" type="text" class="form-control" name="address" placeholder="Address" value="<?= $instructors['address']; ?>">
</div><br>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-facebook-square" aria-hidden="true"></i></span>
<input id="facebook" type="text" class="form-control" name="facebook" placeholder="Facebook" value="<?= $instructors['fb']; ?>">
</div><br>
</div>
<div class="col-md-6 col-sm-6">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-twitter-square" aria-hidden="true"></i></span>
<input id="twitter" type="text" class="form-control" name="twitter" placeholder="Twitter" value="<?= $instructors['twitter']; ?>">
</div><br>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-youtube-square" aria-hidden="true"></i></span>
<input id="youtube" type="text" class="form-control" name="youtube" placeholder="Youtube" value="<?= $instructors['youtube']; ?>">
</div><br>
</div>
<div class="col-md-6 col-sm-6">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-instagram" aria-hidden="true"></i></span>
<input id="instagram" type="text" class="form-control" name="instagram" placeholder="Instagram" value="<?= $instructors['instagram']; ?>">
</div><br>
</div>
</div>
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="col-md-12 input-group" id="textareadescp">
<textarea name="insdescription" class="form-control" rows="5" id="profiledesc" placeholder="Description"><?= $instructors['description']; ?></textarea>
</div><br><br>
</div>
<!-- <script>
CKEDITOR.replace('profiledesc');
</script> -->
</div>
<div class="row">
<div class="col-md-8 col-sm-12">
</div>
<div class="col-md-4">
<div class="modal-footer btncolor">
<button type="submit" name="submit" id="submit" class="rex-bottom-medium rex-btn-icon">
<span class="rex-btn-text">Submit</span>
<span class="rex-btn-text-icon"><i class="fa fa-arrow-circle-o-right"></i></span>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
Here is my controller:
function continueregistration() {
//set validation rules
$validator = array('success' => false, 'messages' => array());
$validate_data = array(
array(
'field' => 'name',
'label' => 'name',
'rules' => 'trim|required|min_length[2]|max_length[30]'
),
array(
'field' => 'web',
'label' => 'web adress',
'rules' => 'trim|required|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'facebook',
'label' => 'facebook adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'twitter',
'label' => 'twitter adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'twitter',
'label' => 'twitter adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'youtube',
'label' => 'youtube adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'instagram',
'label' => 'instagram adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'tel',
'label' => 'telephone number',
'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]|max_length[30]'
),
array(
'field' => 'address',
'label' => 'adress',
'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]|max_length[30]'
),
array(
'field' => 'insdescription',
'label' => 'description',
'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]'
)
);
$this->form_validation->set_rules($validate_data);
$this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');
$data['title'] = 'Continue Registration';
$data['instructors'] = $this->user_model->getuserinfoforreg();
$this->load->view('templates/header');
$this->load->view('registration/registration', $data);
$this->load->view('templates/footer');
//validate form input
if ($this->form_validation->run() === FALSE)
{
// fails
$validator['success'] = false;
foreach ($_POST as $key => $value) {
$validator['messages'][$key] = form_error($key);
}
}
else
{
//insert the user registration details into database
$data = array(
'name' => $this->input->post('name'),
'web' => $this->input->post('web'),
'fb' => $this->input->post('facebook'),
'twitter' => $this->input->post('twitter'),
'youtube' => $this->input->post('youtube'),
'instagram' => $this->input->post('instagram'),
'phone' => $this->input->post('tel'),
'address' => $this->input->post('address'),
'description' => $this->input->post('insdescription')
);
$id = $this->session->userdata('id');
// insert form data into database
if ($this->user_model->updateUser($id, $data)) {
$validator['success'] = true;
$validator['messages'] = array();
}
else
{
// error
$validator['success'] = false;
$validator['messages'] = '<div class="alert alert-danger text-center">Error</div>';
}
}
echo json_encode($validator);
}
here is ajax form:
$(document).ready(function() {
$("#continueregistrationform").unbind('submit').bind('submit', function() {
var form = $(this);
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data: form.serialize(),
dataType: 'json',
success:function(response) {
console.log(response);
if(response.success) {
$("#messages").html(response.messages);
$("#continueregistrationform")[0].reset();
$(".text-danger").remove();
$(".form-group").removeClass('has-error').removeClass('has-success');
}
else {
$.each(response.messages, function(index, value) {
$("#messages").html(response.messages);
var element = $("#"+index);
$(element)
.closest('.form-group')
.removeClass('has-error')
.removeClass('has-success')
.addClass(value.length > 0 ? 'has-error' : 'has-success')
.find('.text-danger').remove();
$(element).after(value);
});
}
} // /success
}); // /ajax
return false;
});
});
Try this load view on different function then have seperate function for submit form. Show us your model also
<?php
class User extends CI_Controller {
class function continueregistration() {
$data['title'] = 'Continue Registration';
$data['instructors'] = $this->user_model->getuserinfoforreg();
$this->load->view('templates/header');
$this->load->view('registration/registration', $data);
$this->load->view('templates/footer');
}
public function submit()
{
$json = array();
$validate_data = array(
array(
'field' => 'name',
'label' => 'name',
'rules' => 'trim|required|min_length[2]|max_length[30]'
),
array(
'field' => 'web',
'label' => 'web adress',
'rules' => 'trim|required|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'facebook',
'label' => 'facebook adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'twitter',
'label' => 'twitter adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'twitter',
'label' => 'twitter adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'youtube',
'label' => 'youtube adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'instagram',
'label' => 'instagram adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'tel',
'label' => 'telephone number',
'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]|max_length[30]'
),
array(
'field' => 'address',
'label' => 'adress',
'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]|max_length[30]'
),
array(
'field' => 'insdescription',
'label' => 'description',
'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]'
)
);
$this->form_validation->set_rules($validate_data);
$this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');
if ($this->form_validation->run() == FALSE)
{
foreach ($_POST as $key => $value) {
$json['messages'][$key] = form_error($key);
}
} else {
//insert the user registration details into database
$data = array(
'name' => $this->input->post('name'),
'web' => $this->input->post('web'),
'fb' => $this->input->post('facebook'),
'twitter' => $this->input->post('twitter'),
'youtube' => $this->input->post('youtube'),
'instagram' => $this->input->post('instagram'),
'phone' => $this->input->post('tel'),
'address' => $this->input->post('address'),
'description' => $this->input->post('insdescription')
);
// This does not set sessions only gets it.
$update_user = $this->user_model->updateUser($this->session->userdata('id'), $data)
if ($update_user)) {
$json['success'] = true;
} else {
$json['messages'] = '<div class="alert alert-danger text-center">Error</div>';
}
}
echo json_encode($json);
}
}
Ajax
$(document).ready(function() {
$("#submit").on('click', function() {
$.ajax({
url: "<?php echo base_url('user/submit');?>",
type: "POST",
data: $("#continueregistrationform").serialize(),
dataType: 'json',
success:function(response) {
console.log(response);
if(response.success) {
$("#messages").html(response.messages);
$("#continueregistrationform")[0].reset();
$(".text-danger").remove();
$(".form-group").removeClass('has-error').removeClass('has-success');
}
else {
$.each(response.messages, function(index, value) {
$("#messages").html(response.messages);
var element = $("#"+index);
$(element)
.closest('.form-group')
.removeClass('has-error')
.removeClass('has-success')
.addClass(value.length > 0 ? 'has-error' : 'has-success')
.find('.text-danger').remove();
$(element).after(value);
});
}
} // /success
}); // /ajax
return false;
});
});
View
<?php $attributes = array('class' => 'rex-forms', 'id' => 'continueregistrationform'); ?>
<?php echo form_open_multipart('user/submit', $attributes); ?>
<?php echo form_close();?>
1st you check ajax set or not also set the page header to json
you don't need to create another controller
function continueregistration() {
if(is_ajax_request()){
//set validation rules
$validator = array('success' => false, 'messages' => array());
$validate_data = array(
array(
'field' => 'name',
'label' => 'name',
'rules' => 'trim|required|min_length[2]|max_length[30]'
),
array(
'field' => 'web',
'label' => 'web adress',
'rules' => 'trim|required|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'facebook',
'label' => 'facebook adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'twitter',
'label' => 'twitter adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'twitter',
'label' => 'twitter adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'youtube',
'label' => 'youtube adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'instagram',
'label' => 'instagram adress',
'rules' => 'trim|valid_url|prep_url|min_length[3]'
),
array(
'field' => 'tel',
'label' => 'telephone number',
'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]|max_length[30]'
),
array(
'field' => 'address',
'label' => 'adress',
'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]|max_length[30]'
),
array(
'field' => 'insdescription',
'label' => 'description',
'rules' => 'trim|required|alpha_numeric_spaces|min_length[3]'
)
);
$this->form_validation->set_rules($validate_data);
$this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');
//validate form input
if ($this->form_validation->run() === FALSE)
{
// fails
$validator['success'] = false;
foreach ($_POST as $key => $value) {
$validator['messages'][$key] = form_error($key);
}
}
else
{
//insert the user registration details into database
$data = array(
'name' => $this->input->post('name'),
'web' => $this->input->post('web'),
'fb' => $this->input->post('facebook'),
'twitter' => $this->input->post('twitter'),
'youtube' => $this->input->post('youtube'),
'instagram' => $this->input->post('instagram'),
'phone' => $this->input->post('tel'),
'address' => $this->input->post('address'),
'description' => $this->input->post('insdescription')
);
$id = $this->session->userdata('id');
// insert form data into database
if ($this->user_model->updateUser($id, $data)) {
$validator['success'] = true;
$validator['messages'] = array();
}
else
{
// error
$validator['success'] = false;
$validator['messages'] = '<div class="alert alert-danger text-center">Error</div>';
}
}
//set header
$this->output
->set_content_type('application/json')
->set_output(json_encode($validator));
}
$data['title'] = 'Continue Registration';
$data['instructors'] = $this->user_model->getuserinfoforreg();
$this->load->view('templates/header');
$this->load->view('registration/registration', $data);
$this->load->view('templates/footer');
}
I am new to Ion Auth codeigniter library. Now I am making a custom bootstrap form to add a user using modal but I always get errors. Any help would be much appreciated. The modal is inside the userList.php view that displays all registered users.
Controller: Users.php
function create_user()
{
$this->data['title'] = "Create User";
if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
$tables = $this->config->item('tables','ion_auth');
$identity_column = $this->config->item('identity','ion_auth');
$this->data['identity_column'] = $identity_column;
// validate form input
$this->form_validation->set_rules('first_name', $this->lang->line('create_user_validation_fname_label'), 'required');
$this->form_validation->set_rules('last_name', $this->lang->line('create_user_validation_lname_label'), 'required');
if($identity_column!=='email')
{
$this->form_validation->set_rules('identity',$this->lang->line('create_user_validation_identity_label'),'required|is_unique['.$tables['users'].'.'.$identity_column.']');
$this->form_validation->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'required|valid_email');
}
else
{
$this->form_validation->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'required|valid_email|is_unique[' . $tables['users'] . '.email]');
}
$this->form_validation->set_rules('phone', $this->lang->line('create_user_validation_phone_label'), 'trim');
$this->form_validation->set_rules('company', $this->lang->line('create_user_validation_company_label'), 'trim');
$this->form_validation->set_rules('password', $this->lang->line('create_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', $this->lang->line('create_user_validation_password_confirm_label'), 'required');
if ($this->form_validation->run() == true)
{
$email = strtolower($this->input->post('email'));
$identity = ($identity_column==='email') ? $email : $this->input->post('identity');
$password = $this->input->post('password');
$additional_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone'),
);
var_dump($first_name);
}
if ($this->form_validation->run() == true && $this->ion_auth->register($identity, $password, $email, $additional_data))
{
// check to see if we are creating the user
// redirect them back to the admin page
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect("auth", 'refresh');
}
else
{
// display the create user form
// set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
$this->data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name'),
);
$this->data['last_name'] = array(
'name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name'),
);
$this->data['identity'] = array(
'name' => 'identity',
'id' => 'identity',
'type' => 'text',
'value' => $this->form_validation->set_value('identity'),
);
$this->data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$this->data['company'] = array(
'name' => 'company',
'id' => 'company',
'type' => 'text',
'value' => $this->form_validation->set_value('company'),
);
$this->data['phone'] = array(
'name' => 'phone',
'id' => 'phone',
'type' => 'text',
'value' => $this->form_validation->set_value('phone'),
);
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password',
'value' => $this->form_validation->set_value('password'),
);
$this->data['password_confirm'] = array(
'name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password',
'value' => $this->form_validation->set_value('password_confirm'),
);
$this->_render_page('userList', $this->data);
}
}
View: userList.php
<div class="modal fade" id="add-user" role="dialog">
<div class="modal-dialog modal-default">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 id="modal-user-profile" class="modal-title">Add User</h4>
</div>
<div class="modal-body">
<?php echo form_open('users/create_user', ['class' => 'form-horizontal', 'role' => 'form']); ?>
<div class="form-group">
<label for="first_name" class="col-sm-2 control-label">First Name</label>
<div class="col-sm-10">
<?php echo form_input($first_name);?>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<?php echo form_password(['name' => 'password', 'id' => 'password', 'class' => 'form-control', 'placeholder' => 'Password']); ?>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Confirm Password</label>
<div class="col-sm-10">
<?php echo form_password(['name' => 'password_confirm', 'id' => 'password_confirm', 'class' => 'form-control', 'value' => set_value('password_confirm'), 'placeholder' => 'Confirm Password']); ?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<?php echo form_input(['name' => 'email', 'id' => 'email', 'class' => 'form-control', 'value' => set_value('email'), 'placeholder' => 'Email']); ?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Contact No</label>
<div class="col-sm-10">
<?php echo form_input(['name' => 'contact', 'id' => 'contact', 'class' => 'form-control', 'value' => set_value('contact'), 'placeholder' => 'Contact']); ?>
</div>
</div>
<div class="form-group">
<label for="status" class="col-sm-2 control-label">Group</label>
<div class="col-sm-10">
<select class="form-control" id="group">
<option>--</option>
<option>Admin</option>
<option>Client</option>
<option>BIR</option>
</select>
</div>
</div>
<div class="form-group">
<label for="status" class="col-sm-2 control-label">Status</label>
<div class="col-sm-10">
<select class="form-control" id="status">
<option>--</option>
<option>Active</option>
<option>Inactive</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default create-user">Create</button>
<button type="button" class="btn btn-danger create">Cancel</button>
</div>
</div>
<?php echo form_close(); ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
What errors are you getting??
In your controller, Use this to verify your results
After query;
echo $this->db->last_query();
var_dump($_POST);exit;
First check all the post data and the query and tell the errors you're getting
How can i make a registration form where i upload an image for my profile picture, and before clicking register, there's a preview of the image that i want to set as the profile picture using CI??
here's my registration form code
register_model.php
<?php
class Register_model extends CI_Model
{
function create_user($photo_name)
{
$new_user_insert_data = array(
'id' => '',
'email' => $this->input->post('email'),
'password' => md5($this->input->post('password')),
'name' => $this->input->post('name'),
'major' => $this->input->post('user_major'),
'year' => $this->input->post('user_year'),
'bio' => $this->input->post('bio'),
'profpic' => $photo_name,
'insta' => $this->input->post('instagram'),
'twitter' => $this->input->post('twitter'),
'facebook' => $this->input->post('facebook'),
'vote_count' => '10',
'vote' => '0',
'gender' => $this->input->post('gender'),
);
$insert = $this->db->insert('user', $new_user_insert_data);
return $insert;
}
}
?>
Here's the view
register.php
<div class="margin-top-87">
<div class="container">
<h1>Register</h1>
<hr>
<div class="row">
<!-- left column -->
<div class="col-md-3">
<div class="text-center">
<img src="//placehold.it/100" class="avatar img-circle" alt="avatar">
<?php
if(!empty($_FILES['userfile'])){
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key => $value)
for ($s=0; $s<=$count-1; $s++)
{
//Original Image Upload - Start
$_FILES['userfile']['name'] = $value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = './public/images/campaign-images/';
$config['allowed_types'] = 'gif|jpg|jpeg|png|GIF|JPG|JPEG|PNG';
$config['max_size'] = '10000';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$CI->load->library('upload', $config);
$CI->upload->do_upload();
$data = $CI->upload->data();
//Original Image Upload - End
//Thumbnail Image Upload - Start
$config['image_library'] = 'gd2';
$config['source_image'] = './public/images/campaign-images/'. $value['name'][$s];
$config['new_image'] = './public/images/campaign-images/thumbs/'.$value['name'][$s];
$config['width'] = 350;
$config['height'] = 250;
//load resize library
$this->load->library('image_lib', $config);
$this->image_lib->resize();
//Thumbnail Image Upload - End
$name_array[] = $data['file_name'];
}
return $name_array;
}
?>
<h6>Please use real photo, not an avatar</h6>
<?php
$data= array
(
'name' => 'userfile',
'type' => 'file',
'class' => 'form-control',
'id' => 'form_register'
);
echo form_upload($data);
?>
</div>
</div>
<!-- edit form column -->
<div class="col-md-9 personal-info">
<div class="alert alert-info fade in">
<strong>Info!</strong> Please fill with real information of yours.
</div>
<?php
if(validation_errors('<p class="error">'!=""))
{?>
<div class="alert alert-danger fade in">
×
<strong>Attention!</strong> <?php echo validation_errors('<p class="error">') ?>
</div><?php
}?>
<h3>Personal info</h3>
<?php $attributes = array('class' => 'form-horizontal', 'role' => 'form', 'id' => 'form_register'); echo form_open('index/create_user',$attributes); ?>
<div class="form-group">
<label class="col-lg-3 control-label">UC Student email :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'email',
'placeholder' => 'Ciputra student email. Example : email#student.ciputra.ac.id',
'type' => 'email',
'class' => 'form-control',
'required' => 'required'
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Name :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'name',
'placeholder' => 'Full name. Example : Front Middle Last',
'type' => 'text',
'class' => 'form-control',
'required' => 'required'
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Gender :</label>
<div class="col-lg-8">
<div class="ui-select">
<?php
$options = array
(
'' => 'Gender',
'Man' => 'Man',
'Woman' => 'Woman'
);
echo form_dropdown('gender', $options, '', 'class="form-control"');
?>
</div>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Major :</label>
<div class="col-lg-8">
<div class="ui-select">
<?php
$options = array
(
'' => 'Insert your major',
'CB' => 'CB',
'FDB' => 'FDB',
'IBM-IC' => 'IBM-IC',
'IBM-RC' => 'IBM-RC',
'IBA' => 'IBA',
'IHTB' => 'IHTB',
'IMT' => 'IMT',
'INA' => 'INA',
'MCM' => 'MCM',
'MIS' => 'MIS',
'PSY' => 'PSY',
'VCD' => 'VCD'
);
echo form_dropdown('user_major', $options, '', 'class="form-control"');
?>
</div>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Year :</label>
<div class="col-lg-8">
<div class="ui-select">
<?php
$options = array('' => 'Year joined UC');
for ($i = date('Y'); $i >= 2006; $i--)
{
$options[$i] = $i;
}
echo form_dropdown('user_year', $options, '', 'class="form-control"');
?>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Password :</label>
<div class="col-md-8">
<?php
$data= array
(
'name' => 'password',
'placeholder' => 'Password',
'type' => 'password',
'class' => 'form-control',
'required' => 'required',
'maxlength' => '15'
);
echo form_password($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Confirm password :</label>
<div class="col-md-8">
<?php
$data= array
(
'name' => 'cpassword',
'placeholder' => 'Confirm Password',
'type' => 'password',
'class' => 'form-control',
'required' => 'required',
'maxlength' => '15'
);
echo form_password($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Short Bio :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'bio',
'placeholder' => 'Short bio (max 128 character)',
'type' => 'text',
'class' => 'form-control',
'maxlength' => '128',
'rows' => '3'
);
echo form_textarea($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Facebook :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'facebook',
'placeholder' => 'Facebook Profile link. Example : facebook.com/name',
'type' => 'url',
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Twitter :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'twitter',
'placeholder' => 'Twitter Profile link. Example : #name',
'type' => 'url',
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Instagram :</label>
<div class="col-lg-8">
<?php
$data= array
(
'name' => 'instagram',
'placeholder' => 'Instagram Profile link. Example : #name',
'type' => 'url',
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<?php
$data= array
(
'name' => 'register',
'value' => 'Register',
'type' => 'submit',
'class' => 'btn btn-primary',
);
echo form_submit($data);
?>
<span></span>
<?php
$data= array
(
'name' => 'reset',
'value' => 'Clear',
'type' => 'reset',
'class' => 'btn btn-default',
);
echo form_reset($data);
?>
</div>
</div>
</form>
<?php echo form_close(); ?>
</div>
</div>
</div>
<hr>
</div>
The "preview before uploading" thing is a javascript trick that loads the local file into the <img> source. The rest is simple file upload where you check the file types, size, resolution etc.
Here is your answer: Preview an image before it is uploaded