"Unable to load the requested file: search_result.php" error - codeigniter

i want create a search box , where we input student name then show all records of that students.my controller code is
public function search_function_in_controller()
{
if ($this->session->userdata('admin_login') != 1)
redirect('login', 'refresh');
$keyword = $_POST['keyword']; // you can also use $this->input->post('keyword');
$data['search_result'] = $this->crud_model->search($keyword);
$this->load->view('search_result', $data);
}
my model is :
function search($keyword)
{
$this->db->like('name',$keyword);
$query = $this->db->get('student');
return $query->result();
}
my view is :
<body>
<form action="<?=site_url('admin/search_function_in_controller')?>" method="post">
search: <input type="text" name="keyword" />
<input type="submit" value="Submit" />
</form>
<div>
<?php
// List up all results.
foreach ($results as $val)
{
echo $val['username'];
}
?>
</div>
</div>
</body>
but when we give input on search box then it give "Unable to load the requested file: search_result.php" , can anyone help me??

Verify the path that you've called in your view.
I think you intended to call the view admin/search_result in your function search_function_in_controller;

Related

Error: page requested not found codeigniter

Controller
public function index()
{
//load session library
$this->load->library('session');
if($this->session->userdata('user')){
// redirect('home');
$this->load->view('heropage');
}
else{
$this->load->view('login_page');
}
}
public function login(){
$email = $_POST['email'];
$password = $_POST['password'];
$data = $this->Users_model->login($email, $password);
if($data)
{
$id=$data[0]->id;
$first_name=$data[0]->firstname;
$last_name=$data[0]->lastname;
$grade=$data[0]->grade;
$points=$data[0]->points;
$this->session->set_userdata('user_id',$id);
$this->session->set_userdata('lname',$last_name);
$this->session->set_userdata('user', $email);
$this->session->set_userdata('fname',$first_name);
$this->session->set_userdata('grade',$grade);
$this->session->set_userdata('pts',$points);
$this->getImg();
redirect('home');
}
else{
header('location:'.base_url().$this->index());
$this->session->set_flashdata('error','Invalid login. User not found'); }
}
View
<?php if(isset($_SESSION['success'])) :?>
<div class="alert alert-success"><?=$_SESSION['success'];?></div>
<?php endif; if(isset($_SESSION['error'])) :?>
<div class="alert alert-warning"><?=$_SESSION['error'];?></div>
<?php endif;?>
<!-- End alerts -->
<form action="<?php echo base_url();?>index.php/User/login" method="post" accept-charset="utf-8">
<div class="form-group">
<label>Email:</label>
<input type="text" class="form-control" name="email" placeholder="Email">
<?php echo form_error('email'); ?>
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" class="form-control"name="password" placeholder="Password">
<?php echo form_error('password'); ?>
</div>
<div class="form-group">
<button class="btn btn-sm btn-success" type="submit" align="center" name="login" class="submit">Log in</button>
</div>
</div>
</form>
model
public function login($email,$password)
{
$query = $this->db->get_where('users', array('email'=>$email));
if($query->num_rows() == 1 )
{
return $query->result();
}
}
Upon trying to log in, I got the error page cant be found. I want it to go to the home page if the session is correct. here is the error message:
404 Page Not Found
The page you requested was not found.
How can I solve the error because I have also set as needed in the routes
I think your form action should be <?php echo base_url(); ?>user/login
Also in your model you're not checking for password anywhere.
You're also not returning anything if the email is not found or more than 1 results are found -
($query->num_rows() == 1)
Model
public function login($email,$password)
{
$query = $this->db->get_where('users', array('email' => $email, 'password' => $password))->result(); // you should use row() here to return only 1 row.
return $query; // you should check the uniqueness of email on registration, not here -- not allow duplicate email on registration
}
Controller
public function login(){
$email = $_POST['email']; // $this->input->post('email');
$password = $_POST['password'];
$data = $this->Users_model->login($email, $password);
if( !empty($data) ) // if no result found it'll be empty
{
// your code
}
else{
header('location:'.base_url().$this->index());
$this->session->set_flashdata('error','Invalid login. User not found');
}
}
See, if this helps you.

how to Pass array from view to controller in Laravel?

I make a form in blade.php, Here I can select multiple checkbox, and I want to pass selected input’s value to controller in a array.But I failed, I can not send the data.
Here is code from view. The selected input’s value can be 1,2 etc;
<form method="post" action="{{action('votesubmitController#votesubmit')}}" class="form">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#foreach($candidate_list[$post_list->id] as $candidate_list[$post_list->id])
<li>
<input type="checkbox" name= "selected[]" value= {{
$candidate_list[$post_list->id]->id }}>
<label>
<!-- some code here -->
</label>
</li>
#endforeach
<button type="submit" id="login-button">Submit</button>
</form>
Here is route-
Route::post('/votesubmit','votesubmitController#votesubmit');
If I write return $input in controller I find –
{"_token":"TQIUxVz0LjzM84Z7TaUPk3Y7BLZPjmXUyhXhlQfp","selected":
["1","2"]}
That’s I need. I do not know how to get selected value. When I get specific route error exception happens . and says "Undefined variable: selected".
Here is my Controller’s code-
class votesubmitController extends Controller
{
public function votesubmit()
{
$input = Input::all();
// return $input;
foreach ($selected as $selected) {
echo $selected;
}
}
}
// You can try this
class votesubmitController extends Controller
{
public function votesubmit()
{
//$input = Input::all();
//$selected = $input['selected'];
$selected = Input::get('selected');
foreach ($selected as $selected)
{
echo $selected;
}
}
}
Either use
$selected = $input['selected']
Or
pass it using Ajax.

Search is not working in codeigniter

i did search like this but it is not taking passed value in text box,can any one help me to solve this issue..thanks in advances........my model query is correct or any error is there......
if i enter in search term it is redirecting to search results page but it is showing blank results.......and it is saying invalid argument supplied for foreach()
if i print last_query() in that it is showing Like below.....
SELECT * FROM (`clients`) WHERE `clientName` LIKE '%%'
This is My Controller:
class Client extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("client_model");
$this->load->model("interview_model");
$this->load->model("candidate_model");
$this->load->model("requirement_model");
}
function search()
{
$search_term = $this->input->post('search');
$data['query'] = $this->client_model->search_client($search_term);
echo "<pre>"; print_R($this->db->last_query()); exit;
$data['page_title'] = "Search Results";
$this->layout->view("client/search",$data);
}
}
Model:
function search_client($search_term)
{
$this->db->select('*');
$this->db->from('clients');
$this->db->like('clientName',$search_term);
// Execute the query.
$query = $this->db->get();
// Return the results.
return $query->result_array();
}
This is form in index.php(View):
<form action="<?php base_url();?>client/search">
<input type="text" name="search" id="search" style="width:180px" placeholder="Search here..." />
<input type="submit" value="Search" name="submit" class="btn btn-primary btn-sm" /></form>
This is search.php(view):
<?php foreach($query as $client){ ?>
<tr>
<td><?php echo $client->clientName;?></td>
<td><?php echo $client->clientSName; ?></td></td>
<td>
<?php } ?>
change this line:
<form method="POST" action="<?php base_url();?>client/search">
to this:
<form action="<?php echo base_url();?>client/search">
and try the following approch:
function search_client($search_term){
$this->db->select('*');
$this->db->from('clients');
$this->db->like('clientName',$search_term);
// Execute the query.
$query = $this->db->get();
if($query->num_rows()>0)
return $query->result();
else
return false;
}

creating own form validation rule in codeigniter

I am using codeigniter to build a website. There is a "forgot password" link on the login page.When the user clicks on it ,he is asked to enter his email and then will further proceed.I want to set a rule on the email entered by user to check whether it exists in the database or not.In case the email does not exist in the database an error message should be displayed that the email does not exist and redirect the user to the same page. I am new to codeigniter.Please help me.Thanks in advance.This is what I have tried with no success.
view 'change password'
<!DOCTYPE html>
<html>
<head>
<title> Login</title>
<link rel="stylesheet" href="http://localhost/cinema/assets/css/form.css">
</head>
<body>
<form action="http://localhost/cinema/verifyque/sec_que" method="post" accept-charset="utf-8" class="username">
<br>
<p>
<label for="email_id">Email ID</label>
<input type="text" id="username" name="email_id"/>
</p>
<input type="submit" name="btnSubmit" class="styled-button-8" value="Submit"
/>
<font color="red" size="4"><b>
<?php echo validation_errors(); ?></b></font>
</form></body></html>
Controller
function sec_que(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email_id', 'Email', 'callback_email_available');
function email_available($str)
{
// You can access $_POST variable
$this->load->model('user');
$result = $this->user->emailAvailability($str);
if ($result)
{
return TRUE;
}else{
$this->form_validation->set_message('email_available', 'The %s does not exist');
return FALSE;
}
}
if($this->form_validation->run() === TRUE) {
$this->load->model('user');
$email['email_id'] = $this->input->post('email_id');
$this->session->set_userdata($email);
$data['que_id_1']= $this->user->display_que();
$data['que_id_2']= $this->user->display_que2();
$this->load->view('forgot_password_2', $data);
}
else{
$this->load->view('change_password');
}
}
Model
public function emailAvailability($email)
{
$this->db->where('user_email',$email);
$query = $this->db->get('users');
return $query->row();
}
Here is the code that you need to use..
Use the Callback method,
Form Validation:
$this->form_validation->set_rules('Email', 'Email', 'callback_emailAvailability');
Model:
public function emailAvailability($email)
{
$query = $this->db->get_where('user_email',$email);
if($query > 0){
return true;
}else{
return false;
}
}
Hope it helps.

CodeIgniter Update Not Working

I have a form in a modal that I'm using to do an update on a record. The form seems to submit okay, the URL in the browser points to the right location (domain.net/cities/update_city/1) but no view shows up and the record is never updated. I've searched the internet but most tutorials seem to skim over the update or hardcode the id to update. I've only been using CI for a week.
Model Code
// Edit City
function edit_city($city_id) {
$data = array('city_name' => $this->input->post('city_name'));
$this->db->where('city_id', $city_id);
$this->db->update('cities', $data);
}
View Code (Form is in a modal and gets city_id added by jQuery)
<div class="modal-body">
<?php $this->load->helper('form'); ?>
<?php echo form_open('/cities/update_city/'); ?>
<label class="control-label" for="name">City</label>
<input type="text" name="city_name" id="city_name" value=""/><br />
<input type="submit" name="submit" class="btn-small btn-primary" value="Edit City" />
<button class="btn-small" data-dismiss="modal">Cancel</button>
<?php echo form_close(); ?>
</div>
<script>
// scripts for modal windows
$(document).on("click", ".edit-modal", function () {
var city_name = $(this).data('name');
var city_id = $(this).data('id');
$(".modal-body #city_name").val( city_name );
//set the forms action to include the city_id
$(".modal-body form").attr('action','/cities/update_city/'+city_id);
$('#editCityDialog').modal('show');
});
Controller Code
// Update City
function update_city($city_id) {
$this->load->model('cities_model');
if ($this->input->post('submit')) {
$data = array('city_name' => $this->input->post('city_name'));
$this->cities_model->edit_city('cities', array('city_id' => $city_id),$data);
redirect('/cities');
}
}
Here is an example, you can use it to update easily (i'm bad at english, sorry).
Model:
class Cities_model extends CI_Model{
function edit($table,$where,$data){
foreach ($where as $key => $value){
$this->db->where($key,$value);
}
$this->db->update($table,$data);
}
}
Controller:
function update_city($city_id){
$this->load->model('cities_model');
if($this->input->post('submit'){
$data = array('city_name' => $this->input->post('city_name'));
$this->cities_model->edit('cities',array('city_id' => $city_id),$data); //(table,where,data); see in model.
redirect('');
}
}
* if you go a link as "http://......update_city/2" and a function update_city($city_id), $city_id will receive as "2" and you need not to write uri_segment to get $city_id. Hope that help you
EDIT :
Controller:
function update_city($city_id) {
$this->load->model('cities_model');
if ($this->input->post('submit')) {
$this->cities_model->edit_city($city_id);
redirect('/cities');
}
}

Resources