I am facing a problem. I have a normal html files (contact.html) in cpanel's public_html folder. And admin panel which is developed in CodeIgniter. Now what i am trying to do is print or receive input data from contact.html file to codeigniter controller. Below mention the code
contact.html
<html>
<form name ="userinput" action="admin/index.php/contact/contact" method="post">
<div class="row contact-form">
<div class="col-lg-6">
<input type="text" name="yourname" id="yourname" class="form-control" placeholder="Your Name">
</div><!-- end col -->
</div><!-- end row -->
</form>
controller : contact.php
<?php
class Contact extends CI_Controller
{
function contact()
{
echo 'test';
echo $yourname = $this->input->post('yourname');
}
}?>
now when i am submitting data 'test' is printing but i can not get the post data of textbox.
The error is mention below.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Contact::$input
Filename: controllers/contact.php
Line Number: 12
please help me how will i get it.
<?php
class Contact extends CI_Controller
{
public function contact(){
echo 'test';
echo $yourname = $this->input->post('yourname');
$this->load->view('index'); // put this file in application/views/index.php ,also rename to .php
}
}
?>
Try creating the function in your controller. Hope it helps !
You need to have a index function or another function
File name: Contact.php file name and class should have only first letter upper case with controllers and models.
<?php
class Contact extends CI_Controller {
public function index() {
$yourname = $this->input->post('yourname');
echo $yourname;
// Since you had named the view file with a .html you need to add .html
$this->load->view('contact.html');
}
}
If you name your view files with .php instead of a .html
$this->load->view('contact');
I would autoload the url helper on autoload.php and would look into form helper
It's possible that since your function name is same as class name that it's treating it as constructor. Rename your function to something else, update your action link in form and try again.
Better try native PHP way of getting value
echo $_POST['yourname'];
Hope this helps
Related
I create a hook allowing the user to send a csv file.
I want to get the data (the file) that the user sends via a form (present in views/templates/hook), to get it in controller/front/files.php, and insert it in my database, anyone have an idea?
my hook in mymodule.php
public function hookDisplayLeftColumnProduct($params)
{
$this->context->smarty->assign([
'files' => Tools::getValue('files')
]);
return $this->display(__FILE__, 'mymodule.tpl');
}
views/templates/hook/mymodule.tpl
<div id="mymodule_block_home">
<form method="POST">
<label for="files">Envoyer un fichier CSV</label>
<input type="file" name="files" id="files">
<button>Envoyer</button>
</form>
</div>
my front controller who's not good I guess
class MyModuleFilesFrontController extends ModuleFrontController
{
public function initContent()
{
parent::initContent();
$files = Tools::getValue('files');
$this->setTemplate('module:mymodule/views/templates/front/files.tpl');
}
}
Thank you for your help
It is not a PrestaShop-specific problem. You might want to read about uploading files in PHP there:
https://www.tutorialspoint.com/php/php_file_uploading.htm
A few hints:
check if there is something to upload, always
make sure to validate user input, always
One more thing. I see that you hook your module into the product page. You could have the logic of uploading a file directly in your hook. You don't need an extra controller for that.
You could do something like:
public function hookDisplayLeftColumnProduct($params)
{
if (Tools::isSubmit('yourSubmitButtonName') {
// here's the logic of uploading the file
}
// the rest of the code
}
I have a form in a tpl file:
<form action="{$link->getModuleLink('virtual_pos', 'validation', [], true)|escape:'html'}" method="post">
...
</form>
On submit I would like to get all the variables from the form and pass them to the controller 'validation'.
I don't wanna use any JS. It is a payment module for a store.
How can I do this?
I have found a solution in another thread.
When the link to the controller is created you can fill the variables that you need in the empty array parameter:
<form action="{$link->getModuleLink('virtual_pos', 'validation', ['id'=>$cart_id], true)|escape:'html'}" method="post">
Then in the controller you can get the data with the super global
$id_from_form_submit = $GET['id'];
If you know any other option please let me know.
In your module create a file controllers/front/validation.php.
There you need a class:
class virtual_posValidationModuleFrontController extends ModuleFrontController
{
public function postProcess()
{
/* where you get the values and validate the order */
}
public function initContent()
{
parent::initContent();
/* where you set data for a last page order confirmation */
}
}
Have you created this already?
I'm a bit confused on including a controller / view within another controller/view. I'm doing the following and getting funny rendering issues:
//CONTROLLER About
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function about(){ //the standalone about page
$data['about_inner'] = $this->about_inner();
$this->load->view('pages/about',$data);
}
public function about_inner(){ //this is separate so it can be loaded by the landing page without the html shell around it
$this->load->model('about_model');
$data['about'] = $this->about_model->get_content();
$this->load->view('pages/about-inner',$data);
}
}
//view about.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<div class="container about" data-page="about">
<div class="scroll-container about">
<?=$about_inner; ?>
</div>
</div>
</body>
</html>
The issue I'm getting is that $about_inner does not end up loading inside of the "scroll-container" - it loads and renders before everything else, as shown in the screenshot.
What's the best way to get the about_inner view and all of its associated data to load within the about view? I need all the content ($this->about_model->get_content()) to come from about_inner since it can also be loaded by other pages via ajax.
SOLUTION
public function about(){ //the standalone about page
$data['nav'] = $this->load->view('templates/nav', NULL, TRUE);
$data['about_inner'] = $this->about_inner(true);
$this->load->view('pages/about',$data);
}
public function about_inner($print =false){ //this is separate so it can be loaded by the landing page without the html shell around it
$this->load->model('about_model');
$data['about'] = $this->about_model->get_content();
return $this->load->view('pages/about-inner',$data, $print);
}
//HTML
//view about:
<div class="container about" data-page="about">
<?=$nav; ?>
<div class="scroll-container about">
<?=$about_inner; ?>
</div>
</div>
You just need to tell the about_inner function to return the data and not print the data. That's the 3rd argument to function view($template, $data, $return);
$this->load->view('pages/about-inner',$data, true);
If you need to do either or, just set a boolean flag as the argument to the function
function about_inner($print = false){
...
$this->load->view('pages/about-inner', $data, $print);
}
Then when you call the function you can simply pass true to the function in order to get it to return the HTML instead of printing the HTML.
$this->about_inner(true)
if your JS folder is in root you have to define it with base_url() function. So correct all the path in get_content method
Ex
<script src ="<?php echo base_url() ?>path/to/your/folder/aaa.js" .....
And i think you can keep one controller method and remove one.(You can keep about and remove about_inner)
I'm new to opencart. I want to create a custom theme and some custom controllers and models. I can't find any tutorials relative to this, but I tried to create a view along a controller. When I call that view from home or header view page, like $header (in home file) and $search (in header file), then it shows undefined variable.
My code looks like this. It's in controller (path is catalog\controller\common\test.php).
<?php
class ControllerCommonTest extends Controller{
public function index() {
if(file_exists(DIR_TEMPLATE.this->config->get('config_template').'/template/test/test.tpl')) {
$this->response->setOutput($this->render());
} else {
return $this->load->view('default/template/common/header.tpl');
}
}
}
?>
And my view is in \view\theme\MyTheme\template\common\test.tpl
<?php
echo "Test file";
?>
And in my home file, I call my controller like below...
<?php
echo $header;
echo $test;
echo $footer;
?>
When I run this it shows the below error:
Notice: Undefined variable: test in C:\xampp\htdocs\opencart\catalog\view\theme\MyCustome\template\common\home.tpl on line 4
So, please provide any tutorial links and any examples for developing a custom module in opencart.
Thanks in advance.
To display test module tpl i.e. test.tpl on home page, You have load test controller on home controller. Please add following code in catalog/controller/common/home.php
add this code
$data['test'] = $this->load->controller('common/test');
After
$data['header'] = $this->load->controller('common/header');
I have done very much research on this topic but I cannot find how i can do this. I am trying to add data to the $data parameter in a view that is being called from the controller of another view. However, any data i add to the subview via the subcontroller cannot be accessed by the subview. However, when I try to pass data to the subview via the client view, it works just fine. Most of the fixes on SO seem to reference just calling the $key in $data['key'] rather than $data so that doesn't seem really relevant here...
I have two classes:
welcome.php - a page
welcomemenu.php - a set of controls intended to
be loaded into welcome.php
Here is my Client Controller (the page that it's on, welcome.php), which stores the return val from subview $welcomemenu in its own $data array...:
<?php
class Welcome extends CI_Controller {
function __construct() {
parent::__construct();
}
function index() {
//echo 'this is the Welcome index function';
$data['clienttestdata'] = 'data from welcome.php!';
$data['welcomemenu'] = $this->load->view('welcome/welcomemenu', $data, true);
$this->load->helper('url');
$this->load->view('templates/header');
$this->load->view('pages/welcome', $data);
$this->load->view('templates/footer');
}
}
And here is the client view ("welcome_view.php" - seems simple enough. The $welcomemenu var is where I put the returns from my component class...):
<section id="allWelcomeContent" class="mainBody">
<header id="mainPageHdr" class=mainPageHdr>
<!-- other stuff from my header -->
</header>
<!-- this is where i want to put the welcome menu... -->
<section id="mainWelcomeContent" class="mainContent">
<div>
<?php echo $welcomemenu;?>
</div>
</section>
</section>
And here is the Controller for my sub-component welcomemenu.php:
<?php
class Welcomemenu extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$data['menu_items'] = array('About', 'Portfolio', 'Resume', 'Fun', 'Blog');
$data['testdata'] = 'data from welcomemenu.php!';
$this->load->view('welcome/welcomemenu', $data);
}
}
And lastly: Here is the sub-view that is supposed to get data from its own controller, but cannot, even though it can take data from a calling client (i.e., the $clienttestdata shows up fine but $testdata doesn't)!
<section>
<!-- TODO: make this element repeatable so content can load from controller and/or model. -->
<div id="divMenuItems">
<?php echo $clienttestdata;?>
<?php echo $testdata;?>
</div>
</section>
Still i couldn't find any proper solution. if anyone then please give me
When you're including the welcomemenu partial in your Welcome/index method, you have to remember that the view does not go through its own controller. Instead, its contents are returned as a string and stored as a parameter. It gets all of its own parameters through the ones you send to it via $data:
$data['welcomemenu'] = $this->load->view('welcome/welcomemenu', $data, true);
This view will thus have access to everything in $data so far - nothing extra is added through the Welcomemenu controller. So, in the above case, it will have:
array
(
'clienttestdata' => 'data from welcome.php!'
)
If you add the parameters you need to $data (as $data['testdata']), your sub-view will have what it needs.