Form submit in opencart - php

I'm new to opencart. I have to write a custom Log-in form for users. Then i design a small code for log-in form in opencart like below. path is (MyTheme/temlate/auth/Sign.tpl)
<form action="<?php echo $Sub; ?>" method="GET" enctype="multipart/form-data">
Name:<Input type="text" name="txtUser">
<br>
Password:<input type="password" name="txtPassword"><br>
<input type="submit">
and controller is like (Path is controller/auth/Sign.php)
<?php
class ControllerAuthSign extends Controller{
public function index() {
$data['Sub']=$this->url->link('auth/result','','SSL');
if(file_exists(DIR_TEMPLATE . $this->config->get('config_template'). '/template/auth/sign.tpl')){
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/auth/sign.tpl',$data));
}
else{
$this->response->setOutput($this->load->view('default/template/account/login.tpl'));
}
}
}
?>
when a user submit the form have to navigate to Result page (Path is /auth/result.tpl)
<?php
echo "Welcome : Mr./Mrs. ".$User;
?>
<br><p>Your are Loged-In</p>
and the controller for Result is.. (Path is /auth/result.php)
<?php
class ControllerAuthResult extends Controller{
public function index() {
$data['User']=$_REQUEST['txtUser'];
$data['Password']=$_REQUEST['txtPassword'];
if(isset($data)){
$this->response->redirect($this->url->link('auth/sign', '', 'SSL'))
}
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/auth/result.tp',$data));
}
}
?>
but the problem is when i click on submit , page navigate to
http://localhost/opencart/index.php?txtUser=Narayana&txtPassword=narayana
and displayed index page. Can any one help how to navigate to result page...?
Thanks in Advance.

Use this
<form action="<?php echo $Sub; ?>" method="POST" enctype="multipart/form-data">
Name:<Input type="text" name="txtUser">
<br>
Password:<input type="password" name="txtPassword"><br>
<input type="submit">

Related

An Error Was Encountered in CI forms

Iam a newbie in Codeigniter , Iam learning it from watching videos , the instructor did the same what I did , But it gives me an error like this "The action you have requested is not allowed." ,and it worked with him, I don't know why, any help ! .
this is my Controller code
public function index(){
if($this->input->post('submit')){
echo $this->input->post('first_name');
}
$this->load->view('forms');
}
this is my View code
<form method="POST">
<input type="text" name="first_name" />
<input type="submit" name=submit" />
</form>
Use form_open() helper which automatically adds hidden input with CSRF value.
So your view should be:
<?php echo form_open('action_url'); ?>
<input type="text" name="first_name" />
<input type="submit" name=submit" />
<?php echo form_close(); ?>
Disabling CSRF protection also works but it's a bad idea.
you almost right, you need to add some parts like action in your form, and isset or empty in your controller like
class Test_form extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->view('form_test');
}
//using your example. good
public function check_form(){
if( isset($this->input->post('first_name', TRUE))){
echo "success <br>$$this->input->post('fisrt_name', TRUE)";
}
else{
echo "error";
}
}
//using form_validation. best
public function check_form_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('first_name', 'first Name', 'trim|required|xss_clean');
if( ! $this->form_validation->run()){
echo "error <br>" . validation_errors();
}
else{
echo "success <br>$$this->input->post('fisrt_name', TRUE)";
}
}
}
form_test.php
first method
<form method="post" action="<?= base_url()?>index.php/test_form/check_form">
<input type="text" name="first_name">
<input type="submit" value="test">
</form>
<hr>
second method
<form method="post" action="<?= base_url()?>index.php/test_form/check_form_validation">
<input type="text" name="first_name">
<input type="submit" value="test">
</form>

PHP My code doesn't send to the right page after using edit button

I have the following problem. I created a .php file that creates(extends) two new pages. In the first page there is a button called submit which after click on it send me to the next page called editname. When I made click on the button (edit)of the second page, it send me to the third page called recorndname. In the third page, there are two buttons called update and delete which send me to the second page. But, actually it is working as it should. Here is part of my code.
//here is my main class
class main {
public function __construct() {
$page_request = 'homepage';
if(isset($_REQUEST['page'])) {
$page_request = $_REQUEST['page'];
}
$page = new $page_request;
if($_SERVER['REQUEST_METHOD'] == 'GET') {
$page->get();
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST'){
$page->post();
} else{
$page->delete();
}
}
}
Here is my page class. In this part seems neither one of the functions are called.
//Page class
class page {
public function get() {
echo 'I am a get method page';
}
public function post() {
echo 'I am a post method';
$user=new users();
//$user->addUser($_POST["firstname"],$_POST["lastname"],$_POST["email"]);
if($_POST['botton'] == 'Submit'){
$user->addUser($_POST["firstname"],$_POST["lastname"],$_POST["email"]);
} else if($_POST['botton'] == 'Update'){
$user->update($_POST["email"],$_POST["firstname"],$_POST["lastname"]);
} else if($_POST['botton'] == 'Delete'){
$user->deleteUser($_POST["email"]);
}
}
public function delete(){
echo 'I am a delete method';
}
}
Here are my extended pages.
//Page classes
class homepage extends page {
public function get() {
echo 'homepage';
echo'<form action="projectBackup.php?page=editname" method="post">
First name:<br>
<input type="text" name="firstname" value="">
<br>
Last name:<br>
<input type="text" name="lastname" value="">
<br>
Email Address:<br>
<input type="text" name="email" value="">
<br><br>
<input type="submit" name="button" value="Submit">
</form>';
}
}
class editname extends page {
public function post(){
echo 'editname';
$users=new users();
$userlist=$users->listUsers();
echo '<table><tr><th>First Name</th><th>Last Name</th><th>Email Address</th></tr>';
foreach($userlist as $Kuser=>$user){
echo '<tr><td>'.$user['fname'].'</td><td>'.$user['lname'].'</td><td>'.$Kuser.'</td><td>';
echo '<form action="projectBackup.php?page=recordname" method="get">
<input type="hidden" name="email" value="$Kuser">
<input type="submit" value="Edit"></form>';
}
echo '</table>';
}
}
class recordname extends page {
public function get() {
echo 'recordname';
$users=new users();
$userlist=$users->listUsers();
echo'<form action="projectBackup.php?page=editname" method="post">
First name:<br>
<input type="text" name="firstname" value="$userlist[$_GET["email"]["fname"]">
<br>
Last name:<br>
<input type="text name="lastname" value="$userlist[$_GET["email"]["lname"]">
<br>
Email Address:<br>
<input type="text" name="email" value="$_GET["email"]">
<br><br>
<input type="submit" name="button" value="Update">
<input type="submit" name="button" value="Delete">
</form>';
}
}
Can someone help me with this? Thank you in advance.

Form method Post not working

My problem seams to be simple, but I am struggling to get this to work.
Here's my html:
<form class="form" role="form" id="form" method="post" action="<?php echo site_url("register"); ?>" onsubmit="" >
<div class="col-md-6 col-sm-6 col-xs-12 fm">
<input type="text" class="form-control" id="email" name="email"
value="<?php if(isset($validation_errors['post_data']['email'])) echo $validation_errors['post_data']['email']; ?>"
placeholder="Email" >
</div>
<div class="text-center">
<input type="submit" id="btn" name="btn" class="btn" value="Quero ganhar um Cartão">
</div>
</form>
I made the code a bit simple, so you could read it fast.
When I submit the form to the register page, I try to see if it is a post or not like this :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller {
function Register() {
parent::__construct();
$this->load->model('register_model');
}
public function index() {
if($_POST) { echo 'post'; } else { echo 'not a post'; }
}
}
And the controller keeps printing 'not a post'.
It seams like the form only redirect to the URL and do nothing more.
Edit :
I did echo $_SERVER['REQUEST_METHOD']; in my controller and it prints " GET ", so, I guess something is wrong with the method ? I Already tried method="POST", method="post" and it keeps printing " GET ". Don't know what else to do.
add url helper in controller, then only you can access site_url() in your view page
$this->load->helper('url');
remove unnecessary onsubmit="" in your form tag
i did manage to get this going.
My redirect link didn't have "www" on it, and despite the redirect, it needed the "www" to do the POST.
Thanks to everyone that tried to help !

how to pass id in controller from form action using codeigniter

hey guys i am new in codeigniter,i have a form like this
<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment/$id" name="application" method="post" >
//some code
</form>
i have a controller methode
function input_investment($id)
{
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('mod_user');
$this->mod_user->insertinvestment($id);
}
i want to get $id from form action to controller methode how can i do that . . pls help me . .
better to pass the value in the hidden field
<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment" name="application" method="post" >
<input type="hidden" name="my_id" value="<?php echo $id; ?>"/>
</form>
in your ci function
function input_investment() {
$id = $this->input->post('my_id');
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('mod_user');
$this->mod_user->insertinvestment($id);
}
or if you want (A test)
// Sample view
<?php $id = 1; ?>
<form action="<?php echo base_url('my_class/my_method/' . $id); ?>" method="post" >
<input type="submit" />
</form>
// Controller
class My_class extends CI_Controller {
public function index() {
$this->load->view('my_class');
}
public function my_method($id) {
echo $id; // outputs 1
}
}
You need to use PHP and echo $id in the element if you want the value, right now you're sending '$id' to input_investment($id).
<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment/<?php echo $id; ?>" name="application" method="post" >
//some code
</form>
Here your form method is post so you cont get the id through the get method ,you can do like
<form class="addinvestmentform" action="<?php echo base_url();?>index.php/ctl_dbcont/input_investment" name="application" method="post" >
<input type="hidden" name='id' value="<?php echo $id;?>">
</form>
and in your controller you can try with post like
$id = $_POST['id'];
or
$id = $this->input->post('id');
it willl better option for you in all cases if you are trying to send single or multiple data to the controller from an form....
$route['ctl_dbcont/input_investment/(:num)'] = "ctl_dbcont/input_investment/$1";
Just add this line at your config/route :) .
This will work with numbers only, if you have other type of IDs you can use (:any)
Other option is to catch the id directly using :
$id = $this->uri->segment(3);
Where segment(3) is the third element after your domain :
http://domain/segment1/segment2/segment3

View rendering as blank in Zend Framework action

<?php
class SignupController extends Zend_Controller_Action
{
function indexAction()
{
if ($this->_request->isPost())
{
echo "Your email address is: " . $this->_request->getPost('email');
}
$this->render("signup");
}
}
This is my controller
this is my view
<html>
<body>
<form action="/signup" method="post">
Email: <input name="email" type="text" />
<input name="btnSubmit" value="Click me" type="submit" />
</form>
</body>
</html>
when I tried to run the view
I am getting result
blank/signup
why this happens
please rectify that error, and tell me the reason for that error
If you are using signup view then replace your render code from this :-
$this->_helper->viewRenderer('signup');
Try it with else:
if ($this->_request->isPost()) {
echo "Your email address is: " . $this->_request->getPost('email');
} else {
$this->render("signup");
}

Categories