Hello i'm new in laravel. My application has a search bar component in almost every view.
The user types there the ID of the client, makes a query to db and compares the user's typed ID with the DB client ID.
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class SearchController extends Controller
{
function get_kcli(Request $request) {
$id = $request->input('kcli');
$current_page = $request->input('currentPage') . '.index';
$data = DB::connection('oracle')->table('CLIENTS')->where('KCLI', $id)->get();
return view($current_page, compact('data'));
}
}
Web.php
Route::post('/search', [App\Http\Controllers\SearchController::class, 'get_kcli'])->name('search');
Form search component view:
<nav class="navbar navbar-light">
<form method="POST" class="form-inline position-relative" action="{{ route('/search') }}">
#csrf
#method('POST')
<input class="form-control shadow-none" name="kcli" id="kcli" type="number" placeholder="Codice..." aria-label="Search">
<input type="text" name="currentPage" value="{{ Route::currentRouteName() }}" hidden>
<button type="submit" class="btn btn-light search-btn"><i class="fas fa-search"></i></button>
</form>
Anyone knows the best practice to store that data request globally an mantain it for every view that need to exctract it?
Example : I search for the client ID '5', the search controller makes a db query with the passed ID, then compacts data. That data has to be stored globally for get the results and post it in other views, without searching again on every view switch(the typed ID '5' has to remain in the search field on every view switch).
To print client_id you can store that "id" in session and print from the session by this way your search value keep remains and to get data in all views there is the concept View::share you can register that in AppServiceProvider please go through this link
https://laravel.com/docs/9.x/views#sharing-data-with-all-views
I have form in my nav template in codeigniter view
<form action="<?php echo base_url()?>account/login/" class="navbar-form navbar-left" method="post" accept-charset="utf-8">
<div class="form-group">
<input type="text" name="adres" class="form-control" placeholder="Email">
<input type="password" name="pass" class="form-control" placeholder="Password">
</div>
<button type="submit" class="btn btn-default">Login</button>
</form>
In my controller method i can echo $_POST['adres'] and its fine, but when i try with codeigniter helper $this->input->post('adres') its empty . Whats wrong ?
I'm using input->post in my registration form and its working fine.
General Advice of using the forms in CodeIgnitor
Step 1: Try to use the native form method that are available in CI.
Syntax for Form Open:
form_open('[controller]/[action]')
Hence below is the sample example of how to open up the form based on the controller that we have created.
<?php echo form_open('todos/update_completed'); ?>
<?php echo form_close(); ?>
Where todos/update_completed means
todos - Controller Name
update_completed - Method name in that Controller.
Step 2: Load up the form elements during the auto load itself or you can load up the form elements in the Construct function itself.
Loading the form attributes via the helper in the construct function.
$this->load->helper('form');
Below is the example of how to call it.
function __construct()
{
parent::__construct();
$this->load->helper('form');
}
Step 3: And in the Controller or in the model you have to get the Input that has been posted like this below.
$this->input->post('title')
$this->input->post('username')
These are all the General Checks that has to be made while using the form elements in CodeIgnitor.
Yes, problem solved. I was using curly brace and i should use a normal one. My bad
Have you used helper class in constructor $this->load->helper('form'); or in method .
You can load helper function in your controller like this
function __construct()
{
$this->load->helper('form');
}
Want to use the luggage_id in the form input type="text" name="luggage_id" as a where clause in my model to update a row 'status' in 'consignment' table
My view
<form action='<?php echo site_url('clientaccount_ctrl/confirm_load_pickup'); ?>' method='post' name='process'>
<div class="form-group">
<input type="text" class="form-control" name="luggage_id" placeholder="Driver ID">
</div>
</div><!-- /.col -->
<div class="col-sm-4 invoice-col">
<button type="submit" class="btn bg-blue btn-flat margin">CONFIRM PICKUP</button>
</form>
My Controller
public function confirm_load_pickup(){
$this->load->model('Clientaccount_model');
$this->Clientaccount_model->confirm_load_pickup();
redirect('clientaccount_ctrl');
}
My Model
public function confirm_load_pickup(){
$luggage_id = $this->input->post('luggage_id');
$data['status']=3;
$this->db->where('luggage_id',$luggage_id);
$query=$this->db->update('consignment',$data);
return $query;
}
I am not an active code ignighter developer, but basic experience with several php mvc frameworks leads me to believe that the model doesn't extend a way to process form data as a controller would.
In the controller scope, $this refers to the controller.
In the model scope, $this refers to the model.
Assuming the model scope doesn't explicitly handle the controller scope and post / get methods, it would be unable to process the variable.
So, I would define the input in the controller and inject it into the model
Controller
public function confirm_load_pickup(){
$luggage_id = $this->input->post('luggage_id');
$this->load->model('Clientaccount_model');
$this->Clientaccount_model->confirm_load_pickup($luggage_id);
redirect('clientaccount_ctrl');
}
Model
public function confirm_load_pickup($luggage_id){
$data['status']=3;
$this->db->where('luggage_id',$luggage_id);
$query=$this->db->update('consignment',$data);
return $query;
}
I was trying to set the action of a form in the form_open(). And there I specified the login file (present in the controller and its also the base controller file), and its function named as validate. But the page is giving 404 object not found error. What is it that I'm doing wrong? And the same error occurs in anchor('login/signup','Create Account');. I hope I'm able to explain myself.
/controller/login.php
<?php
class Login extends CI_Controller
{
function index()
{
$this->load->helper('HTML');
$this->load->view('includes/header');
$this->load->view('login_form');
}
function signup()
{
$this->load->view('signup_form');
}
function validate()
{
}
}
?>
/view/login_form.php
<div class="form_login">
<?php echo heading("Login",1); ?>
<?php
echo form_open('login/validate');
echo form_input('username','','placeholder ="Enter Username"');
echo form_password('pass','','placeholder ="Enter Password"');
echo form_submit('sub','Submit');
echo br();
echo anchor('login/signup','Create Account');
echo form_close();
?>
</div>
you can create form also like that --
<div class="form_login">
<form method="post" action="<?php echo base_url(); ?>login/insert">
<input type="text" name="username" placeholder ="Enter Username">
<input type="password" name="pass" placeholder ="Enter Password">
<input type="submit" value="Submit"></br>
Create Account
</form>
</div>
You did not load the form helper. If you load models, helpers, etc in a class constructor they are available for every method in your Class. Even easier is going to application/config/autoload.php and putting them there. But here is an example constructor and a bunch of different things that can happen including loading your helpers...
class Login extends CI_Controller{
function __construct() {
// this is always required
parent::__construct();
// load some helpers
$this->load->helper('html');
$this->load->helper('url');
$this->load->helper('form');
// load some libraries
$this->load->library('form_validation');
$this->load->library('database');
$this->load->library('session');
// load models
$this->load->model( 'superusers' );
$this->load->model( 'dailyword' );
// create a variable with $this that can be used anywhere
$this->devmessage = '' ;
// call a method and return something that can be used anywhere
$this->wordoftheday = $this->dailyword->returnword() ;
}
so now any method in the Login class, and any Model or View file called by the Login class will have all the above helpers, libraries, models, etc available. Also spelling the helper names upper case might work on some systems but i would suggest always naming them lower case.
I am new to CodeIgniter. I need to process a form. I have a form.html page in view
<html>
<head>
<title>Search</title>
</head>
<body>
<form action="search">
<input type="text" name="search" value="" size="50" />
<div>
<input type="submit" value="Submit" />
</div>
</form>
</body>
</html>
and form controller
class Form extends Controller {
function Form() {
parent::Controller();
}
function index() {
$this->load->view('form');
}
}
and I have an view file search.php but when it is processed it shows page not found...
In M.odel V.iew C.ontroller setups like CodeIgniter the Views are user interface elements. They should not be parsing results.
If I am not mistaken, what you are looking to do is pass data from www.yoursite.com/index.php/form to www.yoursite.com/index.php/search
In unstructured php you might have a form.html with a form action of search.php. A user would navigate to yoursite.com/form.html, which would call yoursite.com/search.php, which might redirect to yoursite.com/results.php.
In CodeIgniter (and, as far as I understand it, in any MVC system, regardless of language) your Controller, Form calls a function which loads the form.html View into itself and then runs it. The View generates the code (generally HTML, but not necessarily) which the user interacts with. When the user makes a request that the View cannot handle (requests for more data or another page) it passes that request back to the Controller, which loads in more data or another View.
In other words, the View determines how the data is going to be displayed. The Controller maps requests to Views.
It gets slightly more complicated when you want to have complex and / or changing data displayed in a view. In order to maintain the separation of concerns that MVC requires CodeIgniter also provides you with Models.
Models are responsible for the most difficult part of any web application - managing data flow. They contain methods to read data, write data, and most importantly, methods for ensuring data integrity. In other words Models should:
Ensure that the data is in the correct format.
Ensure that the data contains nothing (malicious or otherwise) that could break the environment it is destined for.
Possess methods for C.reating, R.eading, U.pdating, and D.eleting data within the above constraints.
Akelos has a good graphic laying out the components of MVC:
(source: akelos.org)
That being said, the simplest (read "easiest", not "most expandable") way to accomplish what you want to do is:
function Form()
{
parent::Controller();
}
function index()
{
$this->load->view('form');
}
function search()
{
$term = $this->input->post('search');
/*
In order for this to work you will need to
change the method on your form.
(Since you do not specify a method in your form,
it will default to the *get* method -- and CodeIgniter
destroys the $_GET variable unless you change its
default settings.)
The *action* your form needs to have is
index.php/form/search/
*/
// Operate on your search data here.
// One possible way to do this:
$this->load->model('search_model');
$results_from_search = $this->search->find_data($term);
// Make sure your model properly escapes incoming data.
$this->load->view('results', $results_from_search);
}
View file is useless without the controller to load and displaying it. You must create a controller to receive the form data, process it, then displaying the process result.
You can use a form helper to set the form open tags, also the close tags:
<?php echo form_open('form/search'); ?>
<input type="text" name="search" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
<?php echo form_close(); ?>
Without using form helper, you can still write it this way:
<form action="<?php echo site_url('form/search'); ?>">
Then add the search method into form controller:
function search()
{
//get form field
$search = $this->input->post('search');
// do stuffs here
//...
}
Remember that CI only help you with the basic code organization and provide a helpful library and helper. But you still need to write the algorithm of the process in your site.
Don't forget to read the included user guide in the downloaded codeigniter package. You can learn many stuffs from the example in there. Don't hesitate to ask things you don't know here, many member of stackoverflow will help you.
This is form validation and submit in controller
My whole controller class
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library(array('session','form_validation'));
$this->load->helper(array('form', 'url', 'date'));
//$this->load->config('app', TRUE);
//$this->data['app'] = $this->config->item('app');
}
}
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Article extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->model('article_model');
}
public function index() {
$data['allArticles'] = $this->article_model->getAll();
$data['content'] = $this->load->view('article', $data, true);
$this->load->view('layout', $data);
}
public function displayAll() {
$data['allArticles'] = $this->article_model->getAll();
$data['content'] = $this->load->view('displayAllArticles', $data, true);
$this->load->view('layout', $data);
}
public function displayArticle($id) {
$data['article'] = $this->article_model->read($id);
$data['articleId'] = $id;
$data['comment'] = $this->load->view('addComment', $data, true);
$data['content'] = $this->load->view('displayArticle', $data, true);
$this->load->view('layout', $data);
}
public function add() {
$this->form_validation->set_message('required', '%s is required');
$this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
$this->form_validation->set_rules('description', 'Description type', 'required|xss_clean');
$this->form_validation->set_error_delimiters('<p class="alert alert-danger"><a class="close" data-dismiss="alert" href="#">×</a>', '</p>');
if ($this->form_validation->run() == TRUE) {
$article = array(
'title' => $this->input->post('title'),
'description' => $this->input->post('description'),
'created' => date("Y-m-d H:i:s")
);
$this->article_model->create($article);
redirect('article', 'refresh');
} else {
$data['article'] = array(
'title' => $this->input->post('title'),
'description' => $this->input->post('description'),
);
$data['message'] = validation_errors();
$data['content'] = $this->load->view('addArticle', $data, true);
$this->load->view('layout', $data);
}
}
}
We can use normal html form like this.
<?php echo $message; ?>
<form method="post" action="article/add" id="article" >
<div class="form-group">
<label for="title">Article Title</label>
<input type="text" class="form-control" id="title" name="title" value="<?php echo $article['title']; ?>" >
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea class="form-control" rows="13" name="description" id="description"><?php echo $article['description']; ?></textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
Try using the codeigniter 'site_url' in your action to make sure you are pointing to the right place. The action in your example would have gone to the 'search' controller rather than the 'form' controller.
<html>
<head>
<title>Search</title>
</head>
<body>
<form action="<?= site_url('form/process_search') ?>">
<input type="text" name="search" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
index is only used in your controller when nothing else is passed.. So in the case of my example above you would want something like this:
function Form()
{
parent::Controller();
}
function process_search()
{
print "<pre>";
print_r($_POST);
print "</pre>";
}
Nettuts has a great tutorial for CodeIgniter for Login form. Follow the screencast and it will clear up your questions.
http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-6-login/
replace this <form action="search"> with <?php echo form_open('form/search');?>
and autoload.php file add $autoload['helper'] = array('form');
and then file dont use file search.php just add your search.php code into your Controller file
like here
class Form extends Controller {
function Form() {
parent::Controller();
}
function index() {
$this->load->view('form');
}
function search(){
//your code here
}
}