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]);
Related
I have a form that is not submitting anytime the submit button is clicked but it is validating.
see the form below:
<?php
$form = ActiveForm::begin([
'id' => 'contact-form',
'action' => ['site/index'],
'options' => [
'class' => 'contact-form wow fadeInUp',
'data-row-duration' => '1s',
]
])
?>
<div class="form-validation alert">
<div class="form-group col-md-12">
<?=
$form->field($model, 'name', [
'options' => ['style' => 'margin:0;padding:0'],
'inputOptions' => [
'class' => 'form-control',
'placeholder' => 'Full Name',
'autocomplete' => 'off'
]
])->label(false)
?>
</div>
<div class="form-group col-md-6">
<?=
$form->field($model, 'email', [
'options' => ['style' => 'margin:0;padding:0'],
'inputOptions' => [
'class' => 'form-control',
'placeholder' => 'Email',
'autocomplete' => 'off'
]
])->label(false)
?>
</div>
<div class="form-group col-md-6">
<?=
$form->field($model, 'phone', [
'options' => ['style' => 'margin:0;padding:0'],
'inputOptions' => [
'class' => 'form-control',
'placeholder' => 'Phone',
'autocomplete' => 'off'
]
])->label(false)
?>
</div>
<div class="form-group col-md-12">
<?=
$form->field($model, 'name', [
'options' => ['style' => 'margin:0;padding:0'],
'inputOptions' => [
'class' => 'form-control',
'placeholder' => 'Message',
'autocomplete' => 'off',
'rows' => '5'
]
])->textarea()->label(false)
?>
</div>
<div class="form-group col-md-4 col-md-offset-8">
<?=Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
</div>
<?php ActiveForm::end(); ?>
SiteController/actionIndex:
public function actionIndex() {
$model = new ContactForm;
if( $model->load(Yii::$app->request->post()) && $model->validate() ){
if( $model->sendEmail(Yii::$app->params['adminEmail']) ){
Yii::$app->session->setFlash('success', 'Thank you for reaching us. We will respond to you shortly');
} else{
Yii::$app->session->setFlash('error', 'Something went wrong. Message not send successfuly');
}
return $this->refresh();
} else{
return $this->render('index', ['model' => $model]);
}
}
NOTE: I'm not getting any error. it's validating but after filling the form to click on submit, the button doesn't work I even used die() in place of the Yii::$app->session->setFlash() still nothing happened. it is just not responding.
Apparently, you have an error but you are not noticing it because you are rendering the name field instead of the message field inside your ActiveForm, I am talking about the very last field before the submit button.
<div class="form-group col-md-12">
<?=
$form->field($model, 'name', [
'options' => ['style' => 'margin:0;padding:0'],
'inputOptions' => [
'class' => 'form-control',
'placeholder' => 'Message',
'autocomplete' => 'off',
'rows' => '5'
]
])->textarea()->label(false)
?>
</div>
and although you have a validation error when you call the $model->validate() against the message field but it is unable to display because it assigns the error to the attribute field that is used in the form but apparently there isn't any field with the name message in the form, so it does not display anything. If you add this line after the form declaration you will immediately see the error
<?= $form->errorSummary($model); ?>
So, change the last field to below and everything will work now.
<div class="form-group col-md-12">
<?=
$form->field($model, 'message', [
'options' => ['style' => 'margin:0;padding:0'],
'inputOptions' => [
'class' => 'form-control',
'placeholder' => 'Message',
'autocomplete' => 'off',
'rows' => '5'
]
])->textarea()->label(false)
?>
</div>
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 got stuck in a a problem in which I need to update some info from database in the same page that is shown.
On this case I'm fetching some "global settings" from a website in an index page which comes with a form in it. Here is a picture of it just to make it more clear to understand what I mean.
As you can see I created the button and I made it possible to see the values from the database, the problem is that I can not figure it out how to update it from there. Can somebody suggest an idea?
Here is my controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Settings extends Admin_Controller {
public function index()
{
$this->form_validation->set_rules('website_favicon', 'Favicon', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_logo', 'Logon', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_name', 'Website Name', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_credits', 'Credits', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_credits_link', 'Credits Link', 'trim|required|min_length[4]');
$this->form_validation->set_rules('website_copyright', 'Copyright', 'trim|required|min_length[4]');
if($this->form_validation->run() == FALSE){
// Get Current Subject
$data['item'] = $this->Settings_model->get_website_data();
//Load View Into Template
$this->template->load('admin', 'default', 'settings/index', $data);
} else {
// Create website settings
$data = array(
'website_favicon' => $this->input->post('website_favicon'),
'website_logo' => $this->input->post('website_logo'),
'website_name' => $this->input->post('webiste_name'),
'website_credits' => $this->input->post('website_credits'),
'website_credits_link' => $this->input->post('website_credits_link'),
'website_copyright' => $this->input->post('website_copyright'),
);
// Update User
$this->Settings_model->update($id, $data);
// Activity Array
$data = array(
'resource_id' => $this->db->insert_id(),
'type' => 'website settings',
'action' => 'updated',
'user_id' => $this->session->userdata('user_id'),
'message' => 'User (' . $data["username"] . ') updated the website settings'
);
// Add Activity
$this->Activity_model->add($data);
//Create Message
$this->session->set_flashdata('success', 'Website setting has been updated');
//Redirect to Users
redirect('admin/settings');
}
}
}
Here is my model:
<?php
class Settings_model extends CI_MODEL
{
function __construct()
{
parent::__construct();
$this->table = 'website_settings';
}
public function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update($this->table, $data);
}
public function get_website_data()
{
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('id', 1);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->row();
} else {
return false;
}
}
}
and here is my view(index.php) with the form:
<h2 class="page-header">Website Settings</h2>
<?php if($this->session->flashdata('success')) : ?>
<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-check"></i> Alert!</h4>
<?php echo $this->session->flashdata('success') ?></div>
<?php endif; ?>
<?php if($this->session->flashdata('error')) : ?>
<div class="alert alert-danger alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h4><i class="icon fa fa-ban"></i> Alert!</h4>
<?php echo $this->session->flashdata('error') ?></div>
<?php endif; ?>
<?php echo validation_errors('<p class="alert alert-danger">'); ?>
<?php echo form_open('admin/settings/index/'.$item->id); ?>
<!-- Website Favicon -->
<div class="form-group">
<?php echo form_label('Website Favicon', 'title'); ?>
<?php
$data = array(
'name' => 'website_favicon',
'id' => 'website_favicon',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_favicon
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Logo -->
<div class="form-group">
<?php echo form_label('Website Logo', 'website_logo'); ?>
<?php
$data = array(
'name' => 'website_logo',
'id' => 'website_logo',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_logo
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Name -->
<div class="form-group">
<?php echo form_label('Website Name', 'website_name'); ?>
<?php
$data = array(
'name' => 'website_name',
'id' => 'website_name',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_name
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Credits -->
<div class="form-group">
<?php echo form_label('Website Credits to', 'website_credits'); ?>
<?php
$data = array(
'name' => 'website_credits',
'id' => 'website_credits',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_credits
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Credits Link -->
<div class="form-group">
<?php echo form_label('Website Credits to Link', 'website_credits_link'); ?>
<?php
$data = array(
'name' => 'website_credits_link',
'id' => 'website_credits_link',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_credits_link
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website Copyright -->
<div class="form-group">
<?php echo form_label('Copyrights', 'website_copyright'); ?>
<?php
$data = array(
'name' => 'website_copyright',
'id' => 'website_copyright',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_copyright
);
?>
<?php echo form_input($data); ?>
</div>
<!-- Website First Ad -->
<div class="form-group">
<?php echo form_label('Ad One', 'website_first_ad'); ?>
<?php
$data = array(
'name' => 'website_first_ad',
'id' => 'website_first_ad',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_first_ad
);
?>
<?php echo form_textarea($data); ?>
</div>
<!-- Website Second Ad -->
<div class="form-group">
<?php echo form_label('Ad Two', 'website_second_ad'); ?>
<?php
$data = array(
'name' => 'website_second_ad',
'id' => 'website_second_ad',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_second_ad
);
?>
<?php echo form_textarea($data); ?>
</div>
<!-- Website Third Ad -->
<div class="form-group">
<?php echo form_label('Ad Three', 'website_third_ad'); ?>
<?php
$data = array(
'name' => 'website_third_ad',
'id' => 'website_third_ad',
'maxlength' => '100',
'class' => 'form-control',
'value' => $item->website_third_ad
);
?>
<?php echo form_textarea($data); ?>
</div>
<?php echo form_submit('mysubmit', 'Update Website', array('class' => 'btn btn-primary')); ?>
<?php echo form_close(); ?>
Thanks for helping.
Check if the data is posted thru the controller. then use set_value to your input fields to retain the values after submit
CONTROLLER
public function index(){
if($this->input->post()){
//set rules for form validation
if($this->form_validation->run() !== FALSE){
//then update
}
}
//your views, data or any other things you do
}
VIEW
echo form_input('name', set_value('name'));
On click of the button you can bind the ajax call to submit the data to the update action of the controller and you can handle response to show relevant message on the same page.
Sample ajax call
$.ajax({
url:'settings/update',//controller action
type:'POST',
dataType:'JSON',
data:{'data':data,'id':id},//form data you need to upate with the id
success:function(response) {
//show success message here
},
error:function(response) {
//show error message here
}
});
Hope this helps.
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');
}
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
<?= $form->field($model, 'email', [
'inputOptions' => [ 'placeholder' => 'Ihre E-Mail Adresse', 'class' => 'newsletter-cta-mail' ]
])->label(false)->textInput(); ?>
<?= Html::submitButton('20€ Gutschein sichern', ['class' => 'green newsletter-cta-button', 'name' => 'contact-button', 'value' => 'hallo']) ?>
<?php ActiveForm::end(); ?>
results into:
<form id="contact-form" action="/" method="post" role="form">
<input type="hidden" name="_csrf" value="WFlFWnIwU1Y3HnQKSn06GG46PXcjQRUzNCA9KhRiYCxvFXQ9RHIiPA=="> <div class="form-group field-newsletterform-email required has-error">
<input type="text" id="newsletterform-email" class="newsletter-cta-mail" name="NewsletterForm[email]" placeholder="Ihre E-Mail Adresse">
<p class="help-block help-block-error">Verification Code cannot be blank.</p>
</div> <button type="submit" class="green newsletter-cta-button" name="contact-button" value="hallo">20€ Gutschein sichern</button></form>
But I dont need the wrapping
How to disable this?
You could simply use Html::activeTextInput() :
<?= Html::activeTextInput($model, 'email', ['placeholder' => 'Ihre E-Mail Adresse', 'class' => 'newsletter-cta-mail']); ?>
Or change ActiveForm::$fieldConfig configuration :
ActiveForm::begin([
'id' => 'contact-form',
'fieldConfig' => [
'options' => [
'tag' => false,
],
],
]);
or You could something like this (change div to span)
$form = ActiveForm::begin([
'id' => 'contact-form',
'fieldConfig' => [
'template' => "{input}",
'options' => [
'tag'=>'span'
]
]
]);
<?= $form->field($model, 'email', [
'template' => '{input}', // Leave only input (remove label, error and hint)
'options' => [
'tag' => false, // Don't wrap with "form-group" div
],
]) ?>
I have solved it like this....put tag as span.. also you can prepend icons infront of the box.
<div id="login-box-inner">
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['role'=>'form'],
'fieldConfig' => [
'options' => [
'tag' => 'span',
],
],
]); ?>
<?= $form->field($model, 'username',[
'template' => '
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user emerald"></i></span>
{input}
</div>
{error}',
'inputOptions' => [
'placeholder' => 'Username ...',
'class'=>'form-control',
]])
?>
<?= $form->field($model, 'password', [
'template' => '
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-key emerald"></i></span>
{input}
</div>
{error}',
'inputOptions' => [
'placeholder' => 'Password ...',
'class'=>'form-control',
]])->input('password')
?>
<?php ActiveForm::end(); ?>