I have problem with custom made form that should make post request to custom controller, it's redirecting me to search page and i have no idea why.
When i try to paste action url into browser it's working corectlly.
my form phtml:
<form class="quick-order-list"
method="post"
action="<?php echo $block->getFormAction(); ?>"
name="listsform"
enctype='multipart/form-data'>
<?= $block->getChildHtml('quick_order_multipleskus') ?>
<?= $block->getChildHtml('quick_order_file') ?>
<div class="quick-order-list-button">
<div class="secondary">
<button type="submit"
name="lists"
title="<?= __('Add to List') ?>"
class="action submit primary">
<span><?= __('Add to List') ?></span>
</button>
</div>
</div>
get action method:
public function getFormAction()
{
return $this->getUrl('quickorder/lists/index', ['_secure' => true]);
}
controller is located in vendor\module\Controller\Lists\Index.php file
<?php
declare(strict_types=1);
namespace module\vendor\Controller\Lists;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\Result\Forward;
use Magento\Framework\Controller\Result\ForwardFactory;
class Index implements HttpGetActionInterface
{
private $forwardFactory;
public function __construct(
ForwardFactory $forwardFactory
) {
$this->forwardFactory = $forwardFactory;
}
public function execute()
{
die('ello');
}
}
in html for me it looks ok:
<form class="quick-order-list" method="post" action="http://pleasehelp.local/quickorder/lists/index/" name="listsform" enctype="multipart/form-data">
after pressing submit i'm landing at:
/catalogsearch/result/?q=quickorder+lists+index
Magento 2.4.0, php7.3
It's not even landing in execute function, but i can catch it in constructor when i place die() there it's working. Tried many thinks like, removing fields from form, trying to point to other controllers, placing static action url there,
for GET it's working corecctly...
I starteed to suspect that it has nothing to do with my code but something is broken in project, but don't know how to check it, can someone point me in correct direction ?
You should implements Magento\Framework\App\Action\HttpPostActionInterface instead of HttpGetActionInterface inside your controller class
i have a view file view.php .Have following code.
<form method="post">
<input type="text" name="profession_name" />
<input type="button" name="submit" value="Add Profession" onClick="add_profession" class="btn btn-success" />
</form>
and i have model file Model_model.php.Have following code.
<?php
class Model_model extends CI_Model{
function __construct(){
parent::__construct();
}
function insert_data(){
$data = array(
"profession_name" => $_POST['profession_name']
);
$this->db->insert('table_name',$data);
}
}?>
Is it possible without using controller i directly access form value in model?
For best practices you would want to handle this with a MVC approach where all form information etc is sent from the view to the controller, handled and processed in the controller and then passed to the model for access to the db.
However, you can access the model in the view exactly like you would in your controller:
<? $this->model_model->insert_data(); ?>
And you can get post values from a form on the same page by using the method shown in this answer: php form - on submit stay on same page
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
}
}