i have a problem : I have a view that needs to call a method in my controller EmployeeController to go to the page to add an employee. But it don't work.. also with other views it doe
This is the view:
<body>
<form class="form-horizontal" id='employeeform' id='employeeform' action="<?php echo base_url();?>EmployeeController/addEmployee" method="post">
<div class="container">
<div class="row">
<div class="col-lg-12 col-sm-12">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>ID</th>
<th>Naam werknemer</th>
<th>Datum aanwerving</th>
<th>Salaris</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($employeelist); ++$i) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $employeelist[$i]->employee_no; ?></td>
<td><?php echo $employeelist[$i]->employee_name; ?></td>
<td><?php echo $employeelist[$i]->hired_date; ?></td>
<td><?php echo $employeelist[$i]->salary; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<input id="btn_add" name="btn_add" type="submit" class="btn btn-primary" value="Add employee" />
</div>
</div>
</div>
</form>
</body>
THIS IS THE CONTROLLER:
class EmployeeController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->helper('form');
$this->load->helper('url');
$this->load->database();
$this->load->library('form_validation');
//load the employee model
$this->load->model('employee_model');
}
//index function
function index() {
$employeelist = $this->employee_model->get_employee_list();
$data['employeelist'] = $employeelist;
$this->load->view('employee/employee_add', $data);
}
function createEmployee() {
$this->form_validation->set_rules('employee_no', 'Employee No', 'trim|required|numeric');
$this->form_validation->set_rules('employee_name', 'Employee Name', 'trim|required|xss_clean|callback_alpha_only_space');
$this->form_validation->set_rules('hired_date', 'Hired Date', 'required');
$this->form_validation->set_rules('salary', 'Salary', 'required|numeric');
if ($this->form_validation->run() == FALSE) {
//fail validation
$this->load->view('employee/employee_add');
} else {
//pass validation
$data = array(
'employee_no' => $this->input->post('employeeno'),
'employee_name' => $this->input->post('employeename'),
'hired_date' => #date('Y-m-d', #strtotime($this->input->post('hireddate'))),
'salary' => $this->input->post('salary'),
);
//insert the form data into database
$this->db->insert('tbl_employee', $data);
//display success message
$employeeresult = $this->employee_model->get_employee_list();
$data['employeelist'] = $employeeresult;
//load the department_view
$this->load->view('employee/employee_view', $data);
redirect('employee/employee_view');
}
}
function addEmployee() {
$this->load->view('employee/employee_add');
//set validation rules
}
}
?>
THE ERROR I GET IS :
The requested URL /BoMaTec_afstudeerproject/CodeIgniter-3.0.1/EmployeeController/createEmployee was not found on this server.
Seems like you don't have any router for that url, or the router is wrong. As per the error, it's trying to load a folder named EmployeeController and within that folder, another folder named createEmployee. Check your .htaccess file and your routers in the framework you are using.
Related
I want my admin table view to reload after uses selects Edit, changes entry and selects Update Entry with the edits reflected in the revised table row.
The admin edit page is working fine, and I know the solve for redirecging the update to the table page is basic. Some light guidance would be much appreciated!
Admin View:
<html>
<head>
<title>Admin Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<table class="table table-bordered">
<center>
<thead>
<tr style="font-size: 24; color: white;">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Actions</th>
</tr>
</thead>
</center>
<tbody style="background-color: #F0F8FF;">
<?php
foreach($users as $u)
{
$class = "btn btn-primary";
$button = "Approve";
?>
<tr style="font-size: 20;">
<td><?php echo $u->first_name; ?></td>
<td><?php echo $u->last_name; ?></td>
<td><?php echo $u->email; ?></td>
<td><a class="btn btn-success" href="<?php echo site_url('myform/edituser/' . $u->id); ?>">Edit</a></td>
<td><a class="<?php echo $class;?>" href="/ci/index.php/myform/approve/?user_id=<?php echo $u->id;?>"><?php echo $button;?></a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</body>
</html>
Admin Edit View:
<html>
<head>
<title>Edit User Info</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<div class="jumbotron">
<form action="" method="post">
<p><font color="black" font size='5'/><strong>Entry for ID </strong><strong>"<?php echo $id; ?>"</strong></font></p>
<font color="black" font size='5'/><strong>First Name </strong></font><input type="text" name='first_name' value="<?php echo $first_name; ?>"?><br/>
<font color="black" font size='5'/><strong>Last Name </strong></font><input type="text" name='last_name' value="<?php echo $last_name; ?>"/><br/>
<font color="black" font size='5'/><strong>Email </strong></font><input type="text" name='email' value="<?php echo $email; ?>"/><br/>
<br/>
<input type="button" class="btn btn-info" value="Update Entry">
</div>
</div>
</form>
</body>
</html>
Controller:
<?php
class Myform extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('user[first_name]', 'First Name', 'required');
$this->form_validation->set_rules('user[last_name]', 'Last Name', 'required');
$this->form_validation->set_rules('user[email]', 'Email Address', 'required|trim');
$form['userValues'] = [
'first_name' => $this->input->post('user[first_name]'),
'last_name' => $this->input->post('user[last_name]'),
'email' => $this->input->post('user[email]'),
'id' => $this->input->post('id'),
];
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$user = $this->input->post('user');
//$company = $this->input->post('company');
echo '<pre>';
print_r($user);
//print_r($company);
if ($this->input->post('id') !== '' && $this->input->post('id') !== NULL)
{
$this->db->where('id', (int) $this->input->post('id'));
$this->db->update("User_Inputs", $user);
}
else
{
$this->db->insert("User_Inputs", $user);
}
//$this->db->insert("companies", $company);
redirect("/myform/successmessage");
}
}
public function successmessage(){
$this->load->view('formsuccess');
}
public function getDatabase(){
$data['users'] = $this->db->get("User_Inputs")->result();
$this->load->view('adminlogin', $data);
}
public function approve(){
$id = $this->input->get("user_id");
$this->db->where("id", $id);
$data['user'] = $this->db->get("User_Inputs")->row();
echo "<pre>";
print_r($this->db->last_query());
print_r($data);
echo "<h1>". $data['user']->email."</h1>";
die();
}
public function edituser($user_id = NULL)
{
$user_id = (int) $user_id;
$data['user'] = $this->db
->where('id', $user_id)
->get("User_Inputs")
->row();
$form['dbValue'] = [
'first_name' => $data['user']->first_name,
'last_name' => $data['user']->last_name,
'email' => $data['user']->email,
'id' => $user_id
];
$this->load->view('editfields', $data['user']);
}
}
i have db with id 1,2,4 (img-1)
but if I try to update id_bank = 1, i got 404 error (img-2)
but that update work with another id (img-3)
this application work fine yesterday, but I got this error this morning, I dont know why, I really need help :(
Update:
I use codeigniter framework,
this is my controller code
function form($id_bank = false)
{
$this->id_bank = $id_bank;
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
$data['page_title'] = lang('bank_form');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => lang('home'),
'href' => base_url()
);
$data['breadcrumbs'][] = array(
'text' => lang('bank_form'),
'href' => base_url('bank')
);
//default values are empty if the provinsi is new
$data['id_bank'] = '';
$data['nama_bank'] = '';
$this->form_validation->set_rules('nama_bank', 'lang:nama_bank', 'trim|required|max_length[64]');
//$this->form_validation->set_rules('id_categories', 'lang:id_categories', 'trim|required|max_length[64]');
// validate the form
if ($this->form_validation->run() == FALSE)
{
$this->view('bank/form', $data);
}
else
{
if (!$this->auth->hasPermission('modify', 'bank'))
{
$this->session->set_flashdata('error', lang('error_acces'));
redirect('bank');
}
$this->load->helper('text');
$save['id_bank'] = $id_bank;
$save['nama_bank'] = $this->input->post('nama_bank');
$this->Model_bank->save_provinsi($save);
$this->session->set_flashdata('message', lang('message_provinsi_saved'));
//go back to the category list
redirect('bank');
}
}
this is on my view
<div class="table-responsive">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th><?php echo lang('no');?></th>
<th><?php echo sort_url('nama_bank', 'nama_bank', $order_by, $sort_order, $code);?></th>
<th><?php echo sort_url('gambar', 'image', $order_by, $sort_order, $code);?></th>
<th class="text-center"><?php echo lang('action');?></th>
</tr>
</thead>
<tbody>
<?php echo (count($type) < 1)?'<tr><td style="text-align:center;" colspan="5">'.lang('no_type').'</td></tr>':''?>
<?php
$pages = ($page-1)*10;
foreach ($type as $merks):
$pages+=1;
?>
<tr>
<td style="width:10px;"><?php echo $pages; ?></td>
<td style=""><?php echo $merks->nama_bank; ?></td>
<td style="text-align:center"> <span><img width="50px" height="50px" src='<?php echo $this->config->item('url_image').'photo/bank/'.$merks->image?>'></span></td>
<td style="width:10%;" class="text-center">
<i class="fa fa-pencil"></i>
<?php if ($this->auth->hasPermission('delete', 'bank')) { ?>
<i class="fa fa-trash-o"></i>
<?php } ?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
this on my routes:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "dashboard";
I'm unsure how to get my editfields.php file to load when an administrator selects the Edit button for whatever row in my SQL table (adminlogin.php).
I feel my editfields.php file has the right stuff, but I'm stuck when it comes to getting the data and having that data display in my view as opposed to what I have printing according to my edituser function.
** PLEASE ignore the approve function for now, as well as the if conditionak in adminlogin.php, both items for separate discussion **
Below are the following files:
Controller (Myform.php)
<?php
class Myform extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('user[first_name]', 'First Name', 'required');
$this->form_validation->set_rules('user[last_name]', 'Last Name', 'required');
$this->form_validation->set_rules('user[email]', 'Email Address', 'required|trim');
if ($this->form_validation->run() == FALSE){
$this->load->view('myform');
}
else{
$user = $this->input->post('user');
$company = $this->input->post('company');
echo '<pre>';
print_r($user);
print_r($company);
$this->db->insert("User_Inputs", $user);
$this->db->insert("companies", $company);
redirect("/myform/successmessage");
}
}
public function successmessage(){
$this->load->view('formsuccess');
}
public function getDatabase(){
$data['users'] = $this->db->get("User_Inputs")->result();
$this->load->view('adminlogin', $data);
}
public function approve(){
$id = $this->input->get("user_id");
$this->db->where("id", $id);
$data['user'] = $this->db->get("User_Inputs")->row();
echo "<pre>";
print_r($this->db->last_query());
print_r($data);
echo "<h1>". $data['user']->email."</h1>";
die();
}
public function edituser(){
$data['user'] = $this->db->get("User_Inputs")->row();
echo "<pre>";
print_r($this->db->last_query());
print_r($data);
echo "<h3>Showing results for ID #". $data['user']->id."</h3>";
die();
/* $data = array($id);
$this->db->where('id', $id);
$this->db->update('users', $data); */
}
}
Table View (adminlogin.php)
<html>
<head>
<title>Admin Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<table class="table table-bordered">
<center>
<thead>
<tr style="font-size: 24; color: white;">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Actions</th>
</tr>
</thead>
</center>
<tbody style="background-color: #F0F8FF;">
<?php foreach($users as $u){
$class = "btn btn-primary";
$button = "Approve";
if ($u->id == 7){
$class = "btn btn-danger";
$button = "Deny";
} ?>
<tr style="font-size: 20;">
<td><?php echo $u->first_name; ?></td>
<td><?php echo $u->last_name; ?></td>
<td><?php echo $u->email; ?></td>
<td><a class="btn btn-success" href="/ci/index.php/myform/edituser?user_id=<?php echo $u->id;?>">Edit</a></td>
<td><a class="<?php echo $class;?>" href="/ci/index.php/myform/approve?user_id=<?php echo $u->id;?>"><?php echo $button;?></a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</body>
</html>
Edit View (editfields.php)
<html>
<head>
<title>Edit User Info</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong><?php echo $id; ?></p>
<strong>First Name</strong><input type="text" name='first_name' value="<?php echo $first_name; ?>"?>"/><br/>
<strong>Last Name</strong><input type="text" name='last_name' value="<?php echo $last_name; ?>"/><br/>
<strong>Email</strong><input type="hidden" name='email' value="<?php echo $email; ?>"/><br/>
<input type="submit" name="submit" value="Update">
</div>
</form>
</body>
</html>
The most basic, fundamental example I want to achieve at this juncture is at the following, though this example may not fit according to my code:
http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/
I've updated my answer with the full test code for controller and views:
Myform.php
<?php
class Myform extends CI_Controller {
public function __construct()
{
parent::__construct();
// Added form and url helper so all functions can use them.
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('user[first_name]', 'First Name', 'required');
$this->form_validation->set_rules('user[last_name]', 'Last Name', 'required');
$this->form_validation->set_rules('user[email]', 'Email Address', 'required|trim|valid_email');
// The only reason i store the values in this variable is to
// allow the re-use of the same form for both index and edituser
// functions. This allows the re-population rule to work.
$form['dbValue'] = [
'first_name' => $this->input->post('user[first_name]'),
'last_name' => $this->input->post('user[last_name]'),
'email' => $this->input->post('user[email]'),
'id' => $this->input->post('id'),
];
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform', $form);
}
else
{
// I've disabled company here as i could not find any reference to it
// in the form you have given as an example
$user = $this->input->post('user');
// $company = $this->input->post('company');
echo '<pre>';
print_r($user);
// print_r($company);
if ($this->input->post('id') !== '' && $this->input->post('id') !==NULL)
{
// id is defined so we update the user
$this->db
->where('id', (int) $this->input->post('id'))
->update("User_Inputs", $user);
}
else
{
// id is not define so we create the user
$this->db->insert("User_Inputs", $user);
}
// $this->db->insert("companies", $company);
redirect("/myform/getDatabase");
}
}
public function successmessage()
{
$this->load->view('formsuccess');
}
public function getDatabase()
{
$data['users'] = $this->db->get("User_Inputs")->result();
$this->load->view('adminlogin', $data);
}
public function approve()
{
$id = $this->input->get("user_id");
$this->db->where("id", $id);
$data['user'] = $this->db->get("User_Inputs")->row();
echo "<pre>";
print_r($this->db->last_query());
print_r($data);
echo "<h1>" . $data['user']->email . "</h1>";
die();
}
public function edituser($user_id = NULL)
{
// Just making sure we are capturing the id as an integer
$user_id = (int) $user_id;
// Get the user based on the $user_id
$data['user'] = $this->db
->where('id', $user_id)
->get("User_Inputs")
->row();
// We will use the below variable to set the form fields to the correct values.
$form['dbValue'] = [
'first_name' => $data['user']->first_name,
'last_name' => $data['user']->last_name,
'email' => $data['user']->email,
'id' => $user_id
];
$this->load->view('myform', $form);
}
}
View: adminlogin.php
<html>
<head>
<title>Admin Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<table class="table table-bordered">
<center>
<thead>
<tr style="font-size: 24; color: white;">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Actions</th>
</tr>
</thead>
</center>
<tbody style="background-color: #F0F8FF;">
<?php
foreach ($users as $u)
{
$class = "btn btn-primary";
$button = "Approve";
if ($u->id == 7)
{
$class = "btn btn-danger";
$button = "Deny";
}
?>
<tr style="font-size: 20;">
<td><?php echo $u->first_name; ?></td>
<td><?php echo $u->last_name; ?></td>
<td><?php echo $u->email; ?></td>
<td><a class="btn btn-success" href="<?php echo site_url('myform/edituser/' . $u->id); ?>">Edit</a></td>
<td><a class="<?php echo $class; ?>" href="<?php echo site_url('myform/approve/' . $u->id); ?>"><?php echo $button; ?></a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</body>
</html>
View: myform.php
<?php
// if the set_value default values are not set set them to NULL
echo form_open('myform');
echo form_label('First Name', 'first_name');
echo form_input('user[first_name]', set_value('user[first_name]', isset($dbValue['first_name'])) ? $dbValue['first_name'] : '' );
echo form_label('Last Name', 'last_name');
echo form_input('user[last_name]', set_value('user[last_name]', isset($dbValue['last_name'])) ? $dbValue['last_name'] : '' );
echo form_label('Email', 'email');
echo form_input('user[email]', set_value('user[email]', isset($dbValue['email'])) ? $dbValue['email'] : '' );
echo form_hidden('id', set_value('id', isset($dbValue['id'])) ? $dbValue['id'] : '' );
echo form_submit('Submit', 'submit');
echo form_close();
echo validation_errors();
OK, for ajax see the below modifications:
I've added jquery library and bootstrap library,
adminlogin.php
<html>
<head>
<title>Admin Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body background=http://wallpapercave.com/wp/ItmVuT6.jpg>
<div class="container">
<table class="table table-bordered">
<center>
<thead>
<tr style="font-size: 24; color: white;">
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colspan="2">Actions</th>
</tr>
</thead>
</center>
<tbody style="background-color: #F0F8FF;">
<?php foreach ($users as $u): ?>
<?php
// added approve class
$class = "approve btn btn-primary";
$button = "Approve";
if ($u->id == 7)
{
$class = " deny btn btn-danger";
$button = "Deny";
}
?>
<!-- id is added to each row to uniquely identify them so we can change it's color-->
<tr id="row_<?= $u->id ?>"style="font-size: 20;">
<td><?php echo $u->first_name; ?></td>
<td><?php echo $u->last_name; ?></td>
<td><?php echo $u->email; ?></td>
<td><a class="btn btn-success" href="<?php echo site_url('myform/edituser/' . $u->id); ?>">Edit</a></td>
<!-- data-id was added to store the user id -->
<td><a class="<?php echo $class; ?>" data-id="<?= $u->id ?>" href="<?php echo site_url('myform/approve/' . $u->id); ?>"><?php echo $button; ?></a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<script>
$('.approve').on("click", function (e) {
// prevent the browser to loading the page directly
e.preventDefault();
// get the user id from the button we clicked.
var userid = $(this).data('id');
//initiate ajax request
$.ajax({
url: $(this).attr('href'),
method: 'POST',
success: function (data) {
// on success add the bootstrap success class to the row
$('#row_' + userid).addClass('success')
},
error: function (data) {
// on error add boorstrap dange class to the row
$('#row_' + userid).addClass('danger')
}
});
});
</script>
</body>
</html>
for testing purposes i've changed the myform/approve function
public function approve()
{
//do something this is for testing
$result = true;
if ($result)
{
// success you have done was OK and we send back success code
$this->output->set_status_header(200);
} else
{
// failure we send back an 4* cient error code
$this->output->set_status_header(400);
}
}
The thing is when i use this code
in my model :
public function get_all_subject_tasks(){
$this->db->select('*');
$this->db->from('task');
$this->db->where("user_id",$this->session->userdata('user_id'));
$this->db->order_by("task_id", "desc");
$query_result=$this->db->get();
$result=$query_result->result();
return $result;
}
in my controller:
public function subjects($t,$action=""){
$data=array();
$data['subject_tasks']=$this->Schoolmodel->get_all_subject_tasks($t);
if($action=='asyn'){
$this->load->view('theme/task',$data);
}else{
$this->load->view('theme/include/header');
$this->load->view('theme/include/school_sidebar');
$this->load->view('theme/task',$data);
$this->load->view('theme/include/footer');
}
}
in my php page:
<div class="panel-body">
<table id="teacher_table" class="table table-striped table-bordered table-condensed">
<th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
<?php foreach($subject_tasks as $list){ ?>
<tr>
<td class="task_name"><?php echo $list->task_name ?></td>
<td class="task_desc"><?php echo $list->task_desc ?></td>
</tr>
<?php } ?>
</table>
</div>
I get all of the tasks that are in the database, without any subject filter.
So my question is
How do i make the page echo the tasks based on which subject they are in?
Also here is how i have setup my database structure
subject_id
task_id
task_name
task_desc
user_id
Where subject_id is the id of the subject where the task is inserted in.
<?php
// your model
public function getTask($subject_id)
{
$result = $this->db
->where("subject_id", $subject_id) // !!!
->where("user_id", $this->session->userdata('user_id')) // !!!
->order_by("task_id", "desc")
->get('tasks');
return $result->num_rows() > 0 ?
$result->result() : false;
}
// your class
class School extends CI_Controller {
public function __construct() {
parent::__construct();
if($this->session->userdata('logged_in')==FALSE){
redirect('User');
}
$this->load->database();
$this->load->model('Schoolmodel');
$this->load->library('form_validation');
}
public function subjects($subject_id,$action=""){
$data=array();
$data['subject_tasks'] = $this->Schoolmodel->getTask($subject_id);
if($action=='asyn'){
$this->load->view('theme/task',$data);
}else{
$this->load->view('theme/include/header');
$this->load->view('theme/include/school_sidebar');
$this->load->view('theme/task',$data);
$this->load->view('theme/include/footer');
}
}
}
?>
// your view
<div class="panel-body">
<table id="teacher_table" class="table table-striped table-bordered table-condensed">
<th>Name</th><th><?php get_phrase('teacher_email') ?></th><th width="110"><?php get_phrase('action') ?></th>
<?php if(false !== $subject_tasks) { foreach($subject_tasks as $list){ ?>
<tr>
<td class="task_name"><?php echo $list->task_name ?></td>
<td class="task_desc"><?php echo $list->task_desc ?></td>
</tr>
<?php } } ?>
</table>
</div>
This is not going to work:
$this->db->where("subject_id",$subject_id AND "user_id",$this->session->userdata('user_id'));
From Active Record documentations: Multiple calls to where will result in AND:
$this->db->where("subject_id", $subject_id);
$this->db->where("user_id", $this->session->userdata('user_id'));
I am developing a web site with Code Igniter 2.1.3. I am using an AJAX call to post data to my controller, but for some reason the data is not being sent.
I am using AJAX calls all over the application and they all work well, just not this one.
My view:
<div id="ajaxResult">
<div class="page-header"><h2>Review Comments</h2></div>
<div class="row-fluid">
<div class="span12">
<?php if(count($comments)): ?>
<table class="table">
<thead>
<tr>
<th>Author</th>
<th>Email</th>
<th>IP Address</th>
<th>Date</th>
<th>Comment</th>
<th>Approve</th>
</tr>
</thead>
<tbody>
<?php foreach($comments as $comment): ?>
<tr>
<td>
<?php echo $comment->comment_author; ?>
</td>
<td><?php echo $comment->comment_author_email; ?></td>
<td><?php echo $comment->comment_author_IP; ?></td>
<td>
<?php
$date = new DateTime($comment->comment_date);
$date = $date->format('d/m/Y');
echo $date;
?>
</td>
<td>
<?php echo $comment->comment_content; ?>
</td>
<td>
<?php
$approve = array(
'name' => 'comment_approved',
'class' => 'approve',
'value' => '1',
'data-commentid' => $comment->id
);
echo form_checkbox($approve);
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>No comments were found for review.</p>
<?php endif; ?>
</div>
</div>
</div>
<script>
$(function()
{
$('.approve').on('click', function()
{
var id = $(this).data('commentid'); alert(id);
$.ajax({
url: "<?php echo site_url('blog/admin/review_comment'); ?>",
type:'POST',
data: { comment_id: id },
success: function(data) { $('#ajaxResult').html(data); } // End of success function of ajax form
}); // End of ajax call
});
});
</script>
My controller function:
public function review_comment()
{
$this->load->helper('form');
$this->load->model('blog_comment_m');
if(isset($_POST['comment_id']))
{
$id = $this->input->post('comment_id');
$data['comment_approved'] = 1;
$this->blog_comment_m->save($data, $id);
$this->data['comments'] = $this->blog_comment_m->get_by(array('comment_approved'=>0));
$this->load->view('admin/review_comments', $this->data);
}
else
{
$this->data['comments'] = $this->blog_comment_m->get_by(array('comment_approved'=>0));
$this->data['subview'] = 'admin/review_comments';
$this->load->view('admin/_layout_main', $this->data);
}
}
I tried to alert the comment_id and it does give me a value of 1. In the controller I do var_dump($_POST), always empty.
I also tried if(isset($_POST['comment_id'])). In the controller it never goes into the if(isset($_POST['comment_id']) statement.
Why is that?
Found it.
It was the fact that I started using csrf on form submits, a new thing for me, and I didn't add this url to the config file.
Adding this:
$config['csrf_exclude_uris'] = array('blog/admin/review_comment');
Solved the problem......
Thank you very very much for all the help.