Confirm form submission error in codeigniter grocery crud - php

When i click on back button, i got the confirm form submission error .How to prevent this issue?Please provide solution for this.
I am trying to login to the admi panel and edit in admin in this following code
Admin controller:
public function index()
{
$data = $this->data;
$this->load->view('admin/login.php',$data);
}
public function login()
{
$data = $this->data;
$username=$this->input->post("username");
$password=$this->input->post("password");
if($username=='admin'&&$password=='admin123')
{
$this->load->view('admin/home.php',$data);
}
else
{
$data['error']="Invalid Username or Password";
$this->load->view('admin/login.php',$data);
}
}
/* Function For Displaying the home page*/
public function home()
{
$data = $this->data;
$this->load->view('admin/index.php',$data);
}
/* Common Function for calling the View*/
public function _admin_output($output = null)
{
$output->base=$this->config->item('base_url');
$output->site=$this->config->item('site_url');
$output->css=$this->config->item('css');
$output->js=$this->config->item('js');
$this->load->view('admin/home_page.php',$output,'refresh');
}
public function loadSlidingimg()
{
$crud = new grocery_CRUD();
$crud->set_table('tbl_slideimg');
$crud->columns('imgname','active');
$crud->set_subject('Frontpage Sliding Images');
$crud->display_as('imgname','Image name');
$crud->set_field_upload('imgname','uploads');
$crud->display_as('active','Active Flag');
$crud->callback_after_update(array($this,'rename_slideimg_db'));
$crud->callback_after_insert(array($this,'rename_slideimg_db'));
$output = $crud->render();
$this->_admin_output($output);
}
}
Please provide solution for this issue

It happens because the action that took you to that page was a POST request. The browser wants to save you from unintentional double-ordering/paying/whatever when you go "back", when loading that page would need to do the form posted again.
How to work that around? I usually redirect the user to a page which was loaded by a GET request, it can be even the same page, see this answer: https://stackoverflow.com/a/3968038/357403
Or, you could modify the history: https://developer.mozilla.org/en-US/docs/Web/API/History_API
Also, please see the PRG pattern: https://en.wikipedia.org/wiki/Post/Redirect/Get

Try this code format:
$this->load->view('admin/home.php', 'refresh', $data);
or
redirect('home', 'refresh');

Related

Unable to load the requested file: validation.php

I am new to developing, and I want to do a simple validation in Codeigniter. I don't know where I am going wrong.
This is my form to be validated
And my controller is this
public function __construct()
{
parent::__construct();
$this->load->model('M_menu');
$this->load->model('master/M_user_type');
$this->load->helper(array('form'));
$this->load->library('form_validation');
}
public function index()
{
$data['menus']=$this->M_menu->getSideBarMenu_m();$data['error_message'] = '';
$this->load->view('master/V_user_type',$data);
}
function saveUserType_c()
{
$data['error_message'] = '';
$this->form_validation->set_rules('userType', 'UserTypeName', 'required');
echo var_dump($this->form_validation->run());
if (!$this->form_validation->run() )
{$data['menus']=$this->M_menu->getSideBarMenu_m();
$data['error_message'] .= validation_errors();
$this->load->view('master/V_user_type',$data);
}
else
{
$insert=$this->M_user_type->saveUserType_m();
if($insert){
$response=array("insert"=>true);
}else{
$response=array("insert"=>false);
}
echo json_encode($response);
}
}
With this I get the network error, and data is being saved to db. But no action in form(form not loading). Please guide me, where I go wrong. Also, if I need to give any more details
Please check your usertype view page, you can also load usertype view page before validation, so that you will be confirmed that usertype view page exist, I hope you understand my point.
You have to load a model before you call its methods. Use this:
$this->load->model('M_user_type');
$insert=$this->M_user_type->saveUserType_m();

Redirect to Login View from Controller In Code-igniter Error occured

Hi I'm new to php and code igniter. what I tried is to get login info from view and validated user and need to send a message to user whether login details are incorrect.
Controller Code :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('login_model');
$this->load->helper('form');
$this->load->helper('url');
}
public function index()
{
//$this->load->helper('url');
$this->load->view('login/login');
}
public function login_check()
{
//$this->load->view('hello');
//echo "directed";
$user_id = $this->input->post('usernm');
$userPassword = $this->input->post('passwordd');
//echo $user_id.' and'.$userPassword;
$var = $this->login_model->check_login($user_id);
$status = 0;
if(empty($var))
{
echo "Invalid user";
$status = 0;
}
else
{
//echo var_dump($var);
$username = $var->username;
//echo $username;
$status = 1;
}
$this->load->helper('url');
redirect('login/login');
//$this->load_>view('login\login');// at this point it does not redirect to login page and instead of that displaying error 404.page not found.
//echo $status;
}
}
Same url ,earlier loaded when it called from from index function but fi it is calling from login check()function does not directed to the view and displaying error 404 page not found.
Any assistance regarding this world be a great help.
Thanks a Lot!
You need to write redirect('welcome'); instead of redirect('login/login');. Because login/login is the view page and you are trying to redirect direct on view without using controller. So you have two option which I had written bellow.
redirect('welcome');
$this->load->view('login/login');
My suggestion is please choose 1st option because 2nd option already you have implemented in first one.
I hope this one will work on you.
public function login_check()
{
$user_id = $this->input->post('usernm');
$userPassword = $this->input->post('passwordd');
$var = $this->login_model->check_login($user_id, $userPassword); //pass username and password to your model to authenticate if input exists.
$data['error'] = '';
if(!empty($var)) //check if var is not empty
{
$this->session->set_userdata($var); //set user_data to var
redirect('Account/page'); //redirect it to your account or success page
}else{
$data['error'] = 'Invalid Username or Password.';
}
$this->load->view('login/login',$data); //pass the error notification to your page.
}

codeigniter - how to prevent F5 from resubmitting a form

I have the following method in my controller:
(shortened version but all the key pieces are here...)
class Widget extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('widget_model');
$this->load->helper('form');
$this->load->library('form_validation');
}
public function assign()
{
//logic to display form.
if ($this->input->method() == "get" ) {
$data['save_status'] = '';
$data['title'] = 'Assign Widget';
$data['main_content'] = "assign";
$this->load->view('includes/template',$data);
}
else
{
//logic to handle POST
...
$data['save_status'] = 'Sucessfully saved to db';
$data['title'] = 'Assign Widget';
$data['main_content'] = "assign";
$this->load->view('includes/template',$data);
}
}
Everything works, except I don't know what the proper way to clear the form data is... because when I press F5 thinking I'm refreshing my page, it's actually resubmitting the data to the database.
Sorry, i'm sure this is a noob question.
Thanks.
EDIT 1
for now, I added a redirect at the end of post logic like this;
redirect(base_url()."index.php/thesamecontroller/thesamemethod");
and that takes the user to the same page, but without the form data being available.
Is this the best way to handle it?

Codeigniter $this->load->view to a Particular Section of page (Data-Toggle - Tab)

I have this admin panel where I use different data-toggle tabs.
In CI(3), if I go with redirect('user/dashboard#new'); , it redirects me to correct section of view but not with form_validation errors.
And if I try $this->dashboard('user/dashboard#new'); it renders the errors but leads me to the wrong section of page (not at #new).
I have just started developing with CI and looking for some help from seniors.
Thanks in advance.
Controller (user)
public function dashboard() {
if($this->session->userdata('is_logged_in')){
$data['homepage'] = '../../templates/vacations/users/dashboard';
$this->load->view('template_users',$data);
}else{
$data['session_error']='Either the session has expired or you have tried to access this page directly';
$this->load->view('../../templates/vacations/headfoot/header-login');
$this->load->view('../../templates/vacations/users/session-error', $data);
$this->load->view('../../templates/vacations/headfoot/footer-login');
}}
Form Validation
if($this->form_validation->run() == FALSE)
{
$this->dashboard('user/dashboard#new');
} else {
$this->load->model('model_users');
if($query = $this->model_users->insert_property_details())
{
redirect('user/dashboard#new');
} else {
redirect('user/dashboard#new');
}}}
$this->dashboard('user/dashboard#new'); just runs/calls the method within the current page. 'user/dashboard#new' does nothing because the method is not written to accept arguments anyway:
public function dashboard(/* arguments would be here normally */) { ... }
Redirecting right after running validation won't work because you will lose the validation errors when you load a new page.
You need to save the errors somewhere, such as to session data, then redirect to dashboard, and then load the errors from saved location and display them on the dashboard view.
Here's an example using session data.
Form method:
if($this->form_validation->run() == FALSE)
{
$this->session->set_userdata('validation_errors', validation_errors());
$this->session->mark_as_flash('validation_errors'); // data will automatically delete themselves after redirect
redirect('user/dashboard#new');
}
else { ... }
Dashboard method:
public function dashboard()
{
if($this->session->userdata('is_logged_in')){
$data['validation_errors'] = $this->session->userdata('validation_errors');
$data['homepage'] = '../../templates/vacations/users/dashboard';
$this->load->view('template_users',$data);
} else { ... }
}
Getting error array from form validation class (for comments below):
class MY_Form_validation extends CI_Form_validation {
public function error_array()
{
return $this->_error_array;
}
}

How to get back to the redirecting page after login (codeigniter)?

I have login controller in my CI app:
function index()
{
if($this->session->userdata('logged_in')==TRUE)
redirect('/success');
$data['error']=$this->session->flashdata('errormessage');
$this->load->view('auth',$data);
}
function process_login()
{
$username=$this->input->post('username');
$password=$this->input->post('password');
if($password == "good_pwd")
{
$data=array('username'=>$username,'logged_in'=>TRUE);
$this->session->set_userdata($data);
redirect('/success');
}
else
{
$this->session->set_flashdata('errormessage','Login failed');
redirect('/failed');
}
}
Thats my securing constructor in main controller:
function __construct()
{
parent::__construct();
if($this->session->userdata('logged_in')!=TRUE) redirect('/login');
}
When I'm trying to get into www.mysite.com/main/function1/ and I'm not logged in, then constructor redirects me into login page - when I log in correct I'm being redirected into main home page instead of page which redirected me into login page (in this example case: www.mysite.com/main/function1/ ) - how to do it?
you'd need to store your request URI in a session for this, so you can return to the previous page, something along the lines of:
function __construct()
{
parent::__construct();
if($this->session->userdata('logged_in')!=TRUE) {
$this->load->helper('url');
$this->session->set_userdata('last_page', current_url());
redirect('/login');
}
}
... you can then use session data to redirect back
Set the redirection url in the controller of the
$redirect_to = "where you want to redirect after login";
$this->session->set_userdata('redirect_to',$redirect_to);
Use Some thing like this in the User controller
$redirect_to = $this->input->post('redirect_to') ? $this->input->post('redirect_to') : $this->session->userdata('redirect_to');
$this->template->build('login', array(
'_user' => $user,
'redirect_to' => $redirect_to,
));

Categories