I am writing a prestashop 1.7 module and I am returning a view in the getContent() method like this:
public function getContent()
{
return $this->display(__FILE__, 'views/templates/admin/product_crawler.tpl');
}
My product_crawler.tpl file looks like this:
<form method="POST" action="{$this->context->link->getAdminLink('ProductCrawlerGet')}">
<input type="text" name="url">
<input type="submit">
</form>
And in modules/product_crawler/controllers/admin I have a getproducts.php file which looks like this:
<?php
/**
*
*/
class ProductCrawlerGetController extends ModuleAdminControllerCore
{
public function postProcess()
{
if (Tools::isSubmit('url'))
{
// form processing
return 'success';
}
}
}
?>
When I remove the getAdminLink() it shows the form just right, but when I add action to the controller it shows a white page
What am I doing wrong
You must use {$link->getAdminLink('ProductCrawlerGet')} instead.
Another alternative is to assign the link in the php, inside the getContent
$this->smarty->assign(array(
'this_link' => $this->context->link->getAdminLink('ProductCrawlerGet'),
));
Then use {$this_link} in the tpl
Related
Here is my case, I got a Request Class which I create using artisan :
php artisan make:request StoreSomethingRequest
Then I put my rules there, and then I can use it in my Controller method like this:
public function store(StoreSomethingRequest $request)
{
}
But what I need is, I want to separate 2 Request logic based on the button in my view (Assumes there is more than 1 submit button in my view). So my controller will look like this :
public function store(Request $request)
{
if($request->submit_button === 'button1')
{
// I want to validate using StoreSomethingRequest here
}
else
{
// I dont want to validate anything here
}
}
I would appreciate any suggestion / help. Please. :D
You can use something like this in your request class inside rules method.
public function rules()
{
$rules = [
'common_parameter_1' => 'rule:rule',
'common_parameter_2' => 'rule:rule',
];
if($this->submit_button === 'button1')
{
$rules['custom_parameter_for_button_1'] = 'rule:rule';
}
else
{
$rules['custom_parameter_for_button_2'] = 'rule:rule';
}
return $rules;
}
Add name and value attributes on the HTML submit buttons. Then check which one has been submitted. Example:
<button type="submit" name="action" value="button1">Save 1</button>
<button type="submit" name="action" value="button2">Save 2</button>
Then in the handler:
If (Request::input('action') === 'button1') {
//do action 1
} else {
// do action 2
}
I am new to CodeIgniter
I have the following code in my Model:
public function saveVar() {
$variable_limit=$this->input->post('var');
$data = array(
'variable_limit'=>'$variable_limit'
);
$this->db->insert('mytable',$data);
}
this should fetch Data from html form and insert a row 'variable_limit' into the table 'mytable', the code doent work I see no change in the mysql database.
Assumed that the first code works, I want to display this data to the view, I wrote this Code in my Controller:
function postVar(){
$this->load->model('tool');
$this->tool->saveVar();
}
I am trying to transfer the data to the model calling 'tool' by calling the saveVar() which is implemented in my model, does this Code work?
Edit: this is the Code in my View:
<form id="myform" action="<?php echo base_url()."tools/file";?>" method="post" onsubmit="return validateData();">
<div> Variable : <input type="number" id="var" name="var" value="<?php echo isset($_POST['var']) ? $_POST['var'] : '' ?>" /></div>
Thanks, Elmoud
Do like this:-
Controller:-
public function postVar(){
$variable_limit=$this->input->post('var');
$data = array(
'variable_limit'=>$variable_limit
);
$this->tool->saveVar($data);
}
Model:-
public function saveVar($data) {
$this->load->model('tool');
$this->db->insert('mytable',$data);
}
Controller:
function postVar(){
$this->load->model('tool');
$this->tool->saveVar();
}
Model
public function saveVar() {
$variable_limit=$this->input->post('var');
$data = array(
'variable_limit'=>$variable_limit
);
$this->db->insert('mytable',$data);
}
It need not use quotes for variables.Just use $variable_limitas such.
Controller:
function postVar(){
$this->load->model('tool');
$this->tool->saveVar();
}
Model:
public function saveVar() {
$data = array(
'variable_limit'=>$this->input->post('var')
);
$this->db->insert('mytable',$data);
}
I am working on a project which deals with lots of forms. I searched and tried the search results, but none of them worked.
My code view code "uprofile.php" is as follows:
<form class="form-horizontal" method="post" action='<?php echo base_url() . "home/addnewagency" ?>'>
<div class="form-group">
<label for="company" class="col-sm-3 control-label">New Agency Name:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="newagency" name="newagency" placeholder="Plase Enter New Agency Name">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</div>
</form>
My Controller "home.php" code is as follows:
public function addnewagency() {
$newagency = $this->input->post('newagency');
$this->dis_model->newagency_model(strtoupper($newagency));
$this->loadprofile();
}
public function loadprofile() {
$data['inscompanyname'] = $this->dis_model->getcompany();
$data['agentname'] = $this->dis_model->getagent();
$data['agencyname'] = $this->dis_model->getagency();
$data['clientname'] = $this->dis_model->getclient();
$data['deptname'] = $this->dis_model->getdept();
$this->load->view('uprofile', $data);
}
when I submit the form, the control is passed to addnewagency() data gets inserted in the database and the URL is http://localhost/dis/home/addnewagency. After getting back to the calling function, it goes to loadprofile() and loads the view uprofile. But, the URL still remains the same. But, I want it as http://localhost/dis/home/login and also want to pass the data. Does anyone have any idea regarding how to achieve this?
All positive suggestion are welcomed...
Thanks in advance...
Now you are calling $this->loadprofile() within addnewagency(). If you want to change URL, you need to use redirect() function of CodeIgniter.
Replace
$this->loadprofile()
with below line of code and it will work for you.
redirect(base_url() . 'home/loadprofile');
if you want to change the url use redirect(base_url() . 'home/loadprofile'); in your controller addnewagency(); as suggested by others and refer this question to know whether you can send data in redirect function or not,
Sending data along with a redirect in CodeIgniter
I found a alternative option to get this done.
You can't change URL string with $this->load->view function but you can redirect to target controller function using redirect method. Before you call redirect, set flash session data using $this->session->set_flashdata that will only be available for the next request, and is then automatically cleared. You can read it from official codeiginter documentation .
Example :
class Login extends CI_Controller {
public function index()
{
if ($this->session->login_temp_data) {
$data = $this->session->login_temp_data;
} else {
$data = array();
$data['name'] = 'Guest' ;
}
$this->load->view('login', $data);
}
public function get_name()
{
$data = [];
$data['name'] = 'John Doe';
$this->session->set_flashdata('login_temp_data', $data);
redirect("login");
}
}
You want to use the redirect() function
At the bottom of your addnewagency() method like so:
public function addnewagency() {
$newagency = $this->input->post('newagency');
$this->dis_model->newagency_model(strtoupper($newagency));
redirect(base_url() . 'home/loadprofile');
}
I am creating a form in codeignitor and every time I try to submit something the page does nothing in chrome or in firefox gives me this message:
The address wasn't understood
Firefox doesn't know how to open this address, because one of the
following protocols (localhost) isn't associated with any program or
is not allowed in this context.
You might need to install other software to open this address.
In internet explorer it trys to find an application to open the page.
I can access the same address directly but it won't let me do it when I submit the form.
this is the code for the form:
<?php
$hidden = array('account_id' => '1');
echo form_open('post', '', $hidden);
?>
<label for="post">Post:</label>
<input type="text" name="post" id="post"/>
<br/>
<input type="submit" value="post" />
</form>
This is the post controller:
<?php
class Post extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('posts_model');
$this->load->helper('url_helper');
$this->load->helper('form');
}
function index() {
$data['title'] = "posted";
$this->posts_model->add_post();
$this->load->view('templates/header', $data);
$this->load->view('comment/index', $data);
$this->load->view('templates/footer');
}
}
?>
This is the function in the posts_model:
function add_post() {
$data = array('person_acc_id' => $this-> input -> post('account_id'),
'post' => $this -> input -> post('post'),
'deleted' => 0,
'edited' => 0,
'post_time' => date('Y-m-d H:i:s', time()));
$this -> db -> insert('post', $data);
}
Actually you didn't set any method to form. Means your form action is Wrong.
In your function index() you call the view. So Then its load from in that. So if I click form submit <form> come to POST Controller. So it again execute function index() again. This will run like loop til you fix it.
So what you have to do is.
In controller Create new method to receive data
public function validate_form()
{
#your Form validate Code goes here
}
In view <form> should be
echo form_open('post/validate_form', '', $hidden);
this will act like
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/post/validate_form" account_id="1" />
To Know about Form Validation
Just check your config.php in application/config, then change your link like this:
$config['base_url'] = 'http://localhost/yourApp/';
I guess your value before is like this:
$config['base_url'] = 'localhost/yourApp/';
My code was actually fine. Once I put it on a real server it works. It would not work with localhost.
I am trying to insert a row to the db using codeigniter.
Model-post.php
class Post extends CI_Model{
function get_posts($num=20, $start=0){
$this->db->select()->from('posts')->where('active',1)->order_by('date_added','desc')->limit($num,$start);
$query=$this->db->get();
return $query->result_array();
}
function get_post($postid){
$this->db->select()->from('posts')->where(array('active' => 1, 'postID'=>$postid))->order_by('date_added','desc');
$query=$this->db->get();
return $query->first_row('array');
}
function insert_post($data){
$this->db->insert('posts',$data);
return $this->db->return_id();
}
Controller-posts.php
class Posts extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('post');
}
function index(){
$data['posts'] = $this->post->get_posts();
$this->load->view('post_index', $data);
}
function post($postid){
$data['post']=$this->post->get_post($postid);
$this->load->view('post',$data);
}
function new_post(){
if($_POST){
$data =array(
'title'=>$_POST['title'],
'post'=>$_POST['post'],
'active'=>1
);
$this->post->insert_post($data);
redirect(base_url());
}
else{
$this->load->view('new_post');
}
}
View-new_post.php
<form action="<?php base_url(); ?>posts/new_post" method="action">
<p>Title: <input type="text" name="title"></p>
<p>Description: <input type="textarea" name="post"></p>
<input type="submit" value="Add post">
</form>
Index view-post_index.php
foreach ($posts as $post) { ?>
<div id-="container">
<div><h3><?php echo $post['title']; ?> </h3>
<?php echo $post['post']; ?>
</div>
</div>
<?php
}
The index page shows all the posts from db. On clicking the title it takes to post.php view to show the respective data. This part is fine.
While trying to add a new post in new_post.php it is not reflecting in the db nor showing any error. Also I used redirect_url to redirect to the index page after inserting. So it shows the same available posts. On clicking the title it keeps on adding posts/post to the url repeatedly. Clicking the title once after redirecting the url shows
http://localhost/Codeigniter/posts/posts/post/1
Again on clicking the title it adds
http://localhost/Codeigniter/posts/posts/post/post/1
Can anyone help me? Thanks!
There are numerous issues across the entire application. These are what I found:
Views
Two problems in your new_post view.
You are not echoing out your base_url . You need to replace your form's action attribute.
the method attribute should either have post or get. In this case it should be post
Change it like this:
From this:
<form action="<?php base_url(); ?>posts/new_post" method="action">
To this:
<form action="<?= base_url(); ?>posts/new_post" method="post">
alternatively you can do this:
<form action="<?php echo base_url(); ?>posts/new_post" method="post">
Controller
In your posts controller, your new_post() function should be like this:
function new_post() {
if ($this->input->post()) {
$data = array(
'title' => $this->input->post('title'),
'post' => $this->input->post('post'),
'active' => 1
);
$id = $this->post->insert_post($data);// this is the id return by your model.. dont know what you wann do with it
// maybe some conditionals checking if the $id is valid
redirect(base_url());
} else {
$this->load->view('new_post');
}
}
Model
function insert_post() should not have $this->db->return_id();, instead it should be $this->db->insert_id();
in your model
function insert_post($newpost){
$this->db->insert('posts',$newpost);
// check if the record was added
if ( $this->db->affected_rows() == '1' ) {
// return new id
return $this->db->insert_id();}
else {return FALSE;}
}
any user input must be validated. if you are using Codeigniter then use its form validation and use its input library like:
$this->input->post('title')
an example for blog posts are in the tutorial https://ellislab.com/codeIgniter/user-guide/tutorial/create_news_items.html
otherwise in your controller -- check if the new post id did not come back from the model -- if it did not come back then just go to an error method within the same controller so you don't lose the php error messages.
if ( ! $postid = $this->post->insert_post($newpost); ){
// passing the insert array so it can be examined for errors
$this->showInsertError($newpost) ; }
else {
// success now do something else ;
}