CodeIgniter 3: form is not submitting - php

I'm new to codeigniter, I have tried everything but my form is still not submitting to the database.
here's my code. I seriously can't find where I went wrong anymore.
VIEW
<?= form_open('forms/submit_shifter_form');?>
<div id="form-interview-fill-mainform" class="form-group">
<div class="container">
<div class="col-sm-12">
<div class="input-group">
<label>Reason/s for shifting:</label>
<input type="text" class="form-control form-input" name="shifter_reason">
</div>
<div class="col-sm-6">
<div class="input-group">
<label>Strengths:</label>
<input type="text" class="form-control form-input" name="shifter_strength">
</div>
</div>
<div class="col-sm-6">
<div class="input-group">
<label>Weaknesses:</label>
<input type="text" class="form-control form-input" name="shifter_weakness">
</div>
</div>
</div>
</div>
<?php echo form_submit('submit','Confirm', 'class="btn btn-success btn-lg" id="submit"');?>
<?php echo form_close();?>
CONTROLLER
<?php
defined('BASEPATH') or exit('No direct script access allowed ');
class Forms extends CI_Controller {
public function session(){
if($this->session->userdata('logged_in')){
$session_data = $this->session->userdata('logged_in');
$data['id'] = $session_data['id'];
$data['username'] = $session_data['username'];
$data['password'] = $session_data['password'];
return $data;
}
}
public function submit_shifter_form(){
$shifter = array(
'course' => $this->input->post('shifter_course'),
'reason' => $this->input->post('shifter_reason'),
'strength' => $this->input->post('shifter_strength'),
'weakness' => $this->input->post('shifter_weakness'),
'futureContribution' => $this->input->post('shifter_contribution')
);
$session_data = $this->session->userdata('logged_in');
$id = $session_data['id'];
$this->load->model('forms_model');
$this->forms_model->shifter_form_submit($id,$shifter);
redirect(site_url('profile'), 'refresh');
}
}
MODEL
<?php
defined('BASEPATH') or exit('No direct script access allowed ');
class Forms_Model extends CI_Model{
function shifter_form_submit($id,$shifter)
{
$this->db->insert('tbl_prototype_shifter',$shifter);
$insert_id = $this->db->insert_id();
$shift = array(
'studentId' => $id
);
$this->db->where('id', $insert_id);
$this->db->update('tbl_prototype_shifter', $shift);
}
}
My other forms submit data properly in my database, after I added this new form, I seemed like it's not submitting properly anymore. Please, can someone help, I cannot figure out where I went wrong

You got
$shifter = array(
'course' => $this->input->post('shifter_course'),
'reason' => $this->input->post('shifter_reason'),
'strength' => $this->input->post('shifter_strength'),
'weakness' => $this->input->post('shifter_weakness'),
'futureContribution' => $this->input->post('shifter_contribution')
);
But your form has,
shifter_reason
shifter_strength
shifter_weakness
So rest all fields will be null, is your table accepting null for those remaining fields ?
If no then set some default value,
In controller
Make sure you loaded database:
function __construct() {
parent::__construct();
$this->load->database();
$this->load->model('forms_model');
$this->load->helper('form');
}
and in your other method,
$session_data = $this->session->userdata('logged_in');
$this->forms_model->shifter_form_submit($session_data['id'],$shifter);
In model,
function shifter_form_submit($id,$shifter)
{
// try to insert
$this->db->insert('tbl_prototype_shifter',$shifter);
// check if insert was ok
if($this->db->affected_rows() > 0)
{
// if ok get last insert id
$insert_id = $this->db->insert_id();
// data to be updated
$shift = array(
'studentId' => $id
);
// update
$this->db->where('id', $insert_id);
$this->db->update('tbl_prototype_shifter', $shift);
}else{
// failed to insert, so cannot update other table too
echo $this->db->_error_message();
throw new Exception("Can't insert the resource");
}
}

Try this insted of your submitting button.
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>

Related

Too few arguments to function Admin::edit(),

I'm new in codeigniter and php,
and am trying to create crud (update)
how to solve this?
thanks in advance
Model :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Role_model extends CI_Model
{
public function DeleteRole($id)
{
$this->db->where('id', $id);
$this->db->delete('user_role');
}
public function GetId($id)
{
return $this->db->get_where('user_role', ['id' => $id])->row_array();
}
public function EditRole()
{
$data = [
"role" => $this->input->post('role' , true)
];
$this->db->where('id', $this->input->post('id'));
$this->db->update('user_role', $data);
}
}
controller :
public function __construct()
{
parent::__construct();
is_logged_in();
$this->load->model('Role_model');
}
public function edit($id)
{
$data['title'] = 'Role';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
$data['user_role'] = $this->Role_model->GetId($id);
$this->form_validation->set_rules('role', 'Role', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar', $data);
$this->load->view('templates/topbar', $data);
$this->load->view('admin/edit', $data);
$this->load->view('templates/footer');
} else {
$this->Role_model->EditRole();
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Role Edited!</div>');
redirect('admin/role');
}
}
view :
<div class="card-body">
<?= $this->session->flashdata('message'); ?>
<form action="<?= base_url('admin/edit/');?>" method="post">
<input type="hidden" name="id" value="<?= $user_role['id']; ?>">
<div class="form-group text-gray-900">
<label for="role">Edit Role</label>
<input type="text" class="form-control" id="role" name="Role" value="<?= $user_role['role']; ?>">
<?= form_error('role', ' <small class="text-danger pl-3">', '</small>'); ?>
</div>
</div>
and it display like this
An uncaught Exception was encountered
Type: ArgumentCountError
Message: Too few arguments to function Admin::edit(), 0 passed in
C:\xampp\htdocs\KingflowWP2\system\core\CodeIgniter.php on line 532 and exactly 1 expected
Let me explain to you.
Edit function needs one parameter (i.e. id). In your form, you are submitting the form without id.
You just need to add the id at the end of the URL like below. Suppose id is 2 then you have to add 2.
base_url('admin/edit/2')
<form action="<?= base_url('admin/edit/2');?>" method="post">
You are only passing $id in
$this->usermodel->Role_model($get['id']);
You have to change your function if you want to post value in hidden input. Oner more thing you have to use default argument if you are not sure about the data is present or not. Always check step by step that data is present or not than do anything.
Write echo here so you get the value in hidden field
<input type="hidden" name="id" value="<?php echo $user_role['id']; ?>">
After that change in the function:
public function edit()
{
$id = $this->input->post('id');
$data['title'] = 'Role';
$data['user'] = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
if(!empty($id)):
$data['user_role'] = $this->Role_model->GetId($id);
else:
//some error message
endif;
$this->form_validation->set_rules('role', 'Role', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('templates/header', $data);
$this->load->view('templates/sidebar', $data);
$this->load->view('templates/topbar', $data);
$this->load->view('admin/edit', $data);
$this->load->view('templates/footer');
} else {
$this->Role_model->EditRole();
$this->session->set_flashdata('message', '<div class="alert alert-success" role="alert">Role Edited!</div>');
redirect('admin/role');
}
}

Why my PHP script doesn't work in my html ? (i use codeigniter framework)

I put php script in my html (I use codeigniter), but when I show up in localhost, my php script eliminate all tag.
I want to put input text box for edit/update something and to show the previous text in my text box. But when I use php script, the element (input form and button) in tag php is missing in localhost.
<form method="post" action="<?php echo base_url('dashboard/categories_update'); ?>">
<div class="box-body">
<div class="form-group">
<label>Categories Name</label>
<?php foreach ($kategori as $k) { ?>
<input
type="hidden" name="id"
value="<?php echo base_url().$k->kategori_id; ?>"
>
<input
type="text" name="categories"
value="<?php echo base_url().$k->kategori_nama; ?>" placeholder="Type here. . . "
>
<?php } ?>
</div>
</div>
<div class="box-footer">
<input type="submit" class="btn btn-success" value="update">
</div>
</form>
This my controller code :
public function categories()
{
$this->load->model('m_data');
$data['kategori'] = $this->m_data->get_data('kategori')->result();
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories',$data);
$this->load->view('dashboard/v_footer');
}
public function add_categories()
{
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories_add');
$this->load->view('dashboard/v_footer');
}
public function categories_action()
{
$this->form_validation->set_rules('categories','Categories','required');
if ($this->form_validation->run() !=false) {
$categories = $this->input->post('categories');
$data = array(
'kategori_nama' => $categories,
'kategori_slug' => strtolower(url_title($categories))
);
$this->load->model('m_data');
$this->m_data->insert_data($data,'kategori');
redirect(base_url().'dashboard/categories');
} else {
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories_add');
$this->load->view('dashboard/v_footer');
}
}
public function categories_edit()
{
$id = $this->input->post('id');
$where = array(
'kategori_id' => $id
);
$this->load->model('m_data');
$data['kategori']= $this->m_data->edit_data($where,'kategori')->result();
$this->load->view('dashboard/v_header');
$this->load->view('dashboard/v_categories_edit',$data);
$this->load->view('dashboard/v_footer');
}
public function categories_update()
{
$this->form_validation->set_rules('categories','Categories','required');
if ($this->form_validation->run() != false) {
$id = $this->input->post('id');
$kategori = $this->input->post('categories');
$where = array(
'kategori_id' => $id
);
$data = array (
'kategori_nama' => $kategori,
'kategori_slug' => strtolower(url_title($kategori))
);
$this->load->model('m_data');
$this->m_data->update_data($where,$data,'kategori');
redirect(base_url().'dashboard/categories');
}
}
there's incorrect script in my controller,
before :
public function categories_edit() {
$id = $this->input->post('id');
it should be :
public function categories_edit($id)
and now all my script is working ! thankyou guys, especially bro #RajendraSingh !

how to store id or any value in session variable and call session in all pages using ci?

controller: test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
function __construct()
{
parent :: __construct();
$this->load->helper(array('form', 'url', 'captcha', 'email'));
$this->load->model('Fetch_data');
}
public function student()
{
$this->load->view('student-dashboard/index');
}
}
view: login.php
<?php
if($this->input->post('login'))
{
$email2 = $this->input->post('email2');
$password2 = $this->input->post('password2');
$this->db->select('email,password');
$this->db->from('students');
$where = "email='$email2' and password = '$password2'";
$this->db->where($where);
$query = $this->db->get();
$result = $query->result_array();
$num = $query->num_rows();
if($num >'0')
{
$this->db->select('email,password,student_id');
$this->db->from('students');
$where = "email='$email2' and password = '$password2'";
$this->db->where($where);
$query = $this->db->get();
$result = $query->result_array();
$data['student_id'] = $this->session->set_userdata('student_id',$result);
if($result == true)
{
redirect('/test/student', $data);
}
}
else
{
echo "<p style='color: red;font-weight: bold;'>Wrong email id or password! </p>";
}
}
?>
<form method="post">
<div class="form-group">
<input type="text" name="email2" id="email2" placeholder="Enter Your Email" class="text-line"/>
</div>
<div class="form-group">
<input type="password" name="password2" id="password2" placeholder="Enter Your Password" class="text-line"/>
</div>
<div class="form-group">
<input type="submit" name="login" id="login" value="Login" class="btn btn-warning"/>
</div>
</form>
I am new in codeigniter. In this code I am creating login form and it work perfectly. Now, I want to store student_id into session variable through which I can give welcome message on my all pages. So, How can I do this ? please help me.
Thank You
Change this line
$data['student_id'] = $this->session->set_userdata('student_id',$result['student_id']);
to this one:
$this->session->set_userdata('student_id',$result['student_id']);
$data['student_id'] = $this->session->userdata('student_id');
You have to load session also
$this->load->library('session');
If you want to add/create session data :
$data = array(
'student_id' => 'id',
'name' => 'name',
<!-- Your parameters -->
);
$this->session->set_userdata($data);
In order to use Session library you should load it in the constructor method in each controller you need it as :
$this->load->library('session');
By doing this, session variables will be available for all views you are loading in this controller.
In your view you can give welcome message with :
<?php echo "Hello ".$this->session->userdata('student_id');?>
Anyway, i suggest you to move your php code from the view (login.php) to the controller (test.php). It eases your work
you can stored the data as array like this change field as you want
$session = array(
'isLoggedIn' => TRUE,
'username' => $result['username'],
'roleid' => $result['roleid'],
'id' => $result['id']
);
$this->session->set_userdata($session);
and retrive data like
$roleid=$this->session->userdata('roleid');

How to get login username and insert into another table in codeigniter php

Hi i am a blog page where i will be inserting blogs from admin panel.while inserting blogs into database i need to insert admin username as well into the database for the blogs.How to get the admin username and insert into blogs table.
Controller:
function addblogs()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>');
$this->form_validation->set_rules('blog_title','Blog Title');
$this->form_validation->set_rules('description','Blog Description');
if($this->form_validation->run()== FALSE)
{
$data['mainpage']='blogs';
$data['mode']='add';
$this->load->view('templates/template',$data);
}
else
{
$this -> blogs_model -> insertblogs();
$this->flash->success('<h2>blogs Added Successfully!</h2>');
redirect('blogs');
}
}
Model:
function insertblogs()
{
$username = $_SESSION['name'];
$title=$this->input->post('blog_title');
$result = str_replace(" ", "-", $title);
$data=array(
'blog_title'=>$this->input->post('blog_title'),
'blogtitle'=>$result,
'description'=>$this->input->post('description'),
'user'=>$username
);
$this->db->insert('blogs',$data);
}
}
View:
<?php
$form_attributes = array('name'=>'adds', 'id'=>'adds', 'enctype' => "multipart/form-data");
echo form_open('blogs/addblogs',$form_attributes);
?>
<div class="element">
<label for="blogtitle"><font color ="black">Blog Title</font></label>
<input class="text err" type="text" name="blog_title" id="blog_title" value="<?php echo set_value('blog_title');?>"/>
</div>
<div class="element">
<label for="description"><font color ="black">Blog Description</font></label>
<textarea name="description" id="myArea1" rows="4" cols="173"></textarea>
</div> <br/>
<div align="center">
<input type="submit" id="submit" value="Submit" />
</div>
<div class="clear"></div>
<?php echo form_close();?>
Login Controller:
if($this->session->userdata('admin_logged_in')){
$data['admin_details'] = $this->session->userdata('admin_logged_in');
$data['records']=$this->career_model->get_jobs_list();
$data['mode'] = "all";
$data['mainpage'] = "career";
$this->load->view('templates/template', $data);
}
else{
$this->load->view('login');
}
Login Model:
<?php
class login_model extends MY_Model
{
function login_user($user_name = '', $password=''){
$userdetails = array(
'user_name' => $user_name,
'password' => md5($password),
);
$this->db->where($userdetails);
$query = $this->db->get('login_details');
if($query->num_rows()):
$user = $query->result();
$sess_arry = array(
'user_id' => $user[0]->user_id,
'name' => $user[0]->name
);
$this->session->set_userdata('admin_logged_in', $sess_arry); //add admin details to session
return true;
else:
return false;
endif;
}
}
You can some change your model in insertblogs() method like that :
$this->load->library('session');
$logged_data = $this->session->userdata('admin_logged_in');
$user_id = $logged_data['user_id'];
$user_name = $logged_data['name'];
$username = $user_name;//$_SESSION['name'];
You can get the name of the logged-in user by:
$username = $this->session->userdata('username');
now you can use this $username to insert it in database or you can perform some other functionality.

update details ci

I am currently trying to allow a user when logged in to update their own details. So far, the user is only allowed to edit user 1 but the form does not populate either. I would like to be able to allow the specific session user to change their details, but don't know what way to go about this. Any guidance would be appreciated, thanks!
Login Controller- Login.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('login_model');
}
public function index()
{
if(($this->session->userdata('user_name')!=""))
{
$this->welcome();
}
else {
$data['title']= 'MVC Application';
$this->load->view('templates/header', $data);
$this->load->view('templates/nav');
$this->load->view('login/signin', $data);
$this->load->view('templates/footer');
}
}
public function welcome()
{
$data['title']= 'MVC Application';
$this->load->view('templates/header', $data);
$this->load->view('templates/nav');
$this->load->view('login/welcome', $data);
$this->load->view('templates/footer');
}
public function login()
{
$email=$this->input->post('email');
$password=$this->input->post('pass');
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('email', 'Your Email', 'trim|required');
$this->form_validation->set_rules('pass', 'Password', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$this->index();
}
else{
$this->login_model->login($email,$password);
$this->welcome();
}
}
public function logout()
{
$newdata = array(
'id' =>'',
'username' =>'',
'email' => '',
'logged_in' => FALSE,
);
$this->session->unset_userdata($newdata );
session_destroy();
redirect('login/index');
}
function update()
{ $data['title']= 'MVC Application';
$this->load->view('templates/header', $data);
$this->load->view('templates/nav');
$this->load->view('login/update', $data);
$this->load->view('templates/footer');
$data = array (
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
$this->load->model('login_model');
$this->login_model->update($data);
}
}
?>
Login model- Login_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function login($email, $password)
{
$this->db->where("email",$email);
$this->db->where("password",$password);
$query=$this->db->get("mvc_user");
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$newdata = array(
'id' => $rows->id,
'username' => $rows->username,
'email' => $rows->email,
'password' => $rows->password,
'logged_in' => TRUE,
);
}
$this->session->set_userdata($newdata);
return true;
}
return false;
}
function update($data)
{
$this->db->where('id', 1);
$this->db->update('mvc_user', $data);
}
}
?>
Update view (located in login folder) update.php
<div class="six columns">
<?php echo form_open('login/update'); ?>
<p>
<label for="user_name">Username</label>
<input type="text" name="user_name" id="user_name" />
</p>
<p>
<label for="user_email">Email</label>
<input type="text" name="user_email" id="user_email" />
</p>
<p>
<label for="user_password">Password</label>
<input type="text" name="user_password" id="user_password" />
</p>
<p><input type="submit" value="Save" /></p>
<?php echo form_close(); ?>
</div>
First, you need to pass the data from the Login controller to the view so that you can pre-populate the form fields:
function update() {
// Prepare data to pass to the view
$data = array (
'title' => 'MVC Application',
'username' => $this->session->userdata('username'),
'email' => $this->session->userdata('email'),
'password' => $this->session->userdata('password')
);
$this->load->view('templates/header', $data);
$this->load->view('templates/nav');
$this->load->view('login/update', $data);
$this->load->view('templates/footer');
$data = array (
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
$this->load->model('login_model');
$this->login_model->update($data);
}
The view can then access the array elements as individual variables and pre-populate the input fields:
<div class="six columns">
<?php echo form_open('login/update'); ?>
<p>
<label for="username">Username</label>
<input type="text" name="username" id="username" value="<?php echo $username; ?>" />
</p>
<p>
<label for="email">Email</label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
</p>
<p>
<label for="password">Password</label>
<input type="text" name="password" id="password" value="<?php echo $password; ?>" />
</p>
<p><input type="submit" value="Save" /></p>
<?php echo form_close(); ?>
</div>
To save the user's new data in Login_model.php, just get the id from the session:
function update($data) {
$my_id = $this->session->userdata('id');
if($my_id !== false) { // Just making sure we're logged in
$this->db->where('id', $my_id);
$this->db->update('mvc_user', $data);
}
}

Categories