I am trying to create a new page that will bring up a form for them to enter a name for a group. It will then create the group and add them as a member to it. I have a similar sign up that I have copied and changed to fit the needs of this, but when I try to access it it gives me a 404 message.
I am accessing the controller (groups.php):
<?php
class Login extends CI_Controller {
function index() {
$this->load->view('includes/header');
$this->load->view('group_view');
$this->load->view('includes/footer');
$this->createGroup();
}
function createGroup() {
$this->load->library('form_validation');
// validation rules
$this->form_validation->set_rules('name', 'Name', 'trim|required');
if ($this->form_validation->run() == FALSE) { //didnt validate
$this->load->view('includes/header');
$this->load->view('group_view');
$this->load->view('includes/footer');
} else {
$this->load->model('model_groups');
if($query = $this->model_groups->createGroup()) {
//$data['account_created'] = 'Your account has been created.<br/><br/>You may now sign in.';
$this->load->view('includes/header');
$this->load->view('home_view');
$this->load->view('includes/footer');
} else {
$this->load->view('includes/header');
$this->load->view('group_view');
$this->load->view('includes/footer');
}
}
}
}
?>
I can post the model and view if necessary but Im pretty sure the problem is in the controller as that is what I am trying to load. Please let me know as soon as possible what my problem is! Thanks guys!
You should change your class name Login to Group
Related
Every time I login, in the model the session receives all the data correctly, but when the page is redirected to the home, the session data disappears.
I'm using codeigniter, and this is the model code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
if (isset($_SESSION['id_usuario']))
redirect(base_url());
$this->load->model('blogueiro_model');
$this->load->model('pagina_model');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('login.php');
}
public function checa()
{
if ( ! $this->input->post())
show_404();
$this->form_validation->set_rules('login', 'login', 'required');
$this->form_validation->set_rules('password', 'password', 'required');
$login = html_escape($this->input->post('login'));
$senha = html_escape($this->input->post('password'));
if ($this->form_validation->run() === TRUE)
{
$usuario = $this->blogueiro_model->autentica($login, $senha);
if ($usuario !== NULL)
{
$this->session->set_userdata('id_usuario', $usuario->id);
$this->session->set_userdata('nome_usuario', $usuario->nome);
$this->session->set_userdata('login_usuario', $usuario->login);
$this->session->set_userdata('e_admin', $usuario->e_admin);
$this->session->set_userdata('foto', $usuario->foto);
$permissoes = ($usuario->e_admin == 0) ? $this->blogueiro_model->busca_permissoes($usuario->id) : $this->blogueiro_model->lista_todas_permissoes();
$this->session->set_userdata('permissoes', $permissoes);
$this->blogueiro_model->limpa_trials($login);
}
else
{
$this->blogueiro_model->incrementa_trials($login);
$this->index();
}
}
else
{
$this->index();
}
redirect(base_url());
}
}
Another thing I noticed was that the status code is always between 302 and 303, I researched a lot but found no solution to this problem.
Follow the status, preview and request images.
Status Code: 302 found
Preview
Request
Are you using Ci3 ?
Are you loading the session library in the controller that has the method that shows the "home".
You can load the library in the controller or methods like this: $this->load->library('session'); or you just can auto load the session library everywhere
adding the session library in application/config/autoload.php.
For more please check Auto loading in CodeIgniter 3
Try like this
$_SESSION['id_usuario'] = (int) $usuario->id;
$_SESSION['nome_usuario'] = (string) $usuario->nome;
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();
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;
}
}
This is my first time using CodeIgniter and I am trying to create a Log In form. Whenever I click on the submit button that I have created, it brings me to a 404 Page Not Found Error.
I have made a view_login.php in the view folder with this code:
<?php echo form_open('user/login') ?>
<ul>
<li>
<label> username </label>
<div>
<?php echo form_input(array('id' => 'username', 'name' => 'username')); ?>
</div>
</li>
<li>
<label> password </label>
<div>
<?php echo form_password(array('id' => 'password', 'name' => 'password')); ?>
</div>
</li>
<li><?php echo validation_errors(); ?></li>
<li>
<?php echo form_submit(array('name' => 'submit'), 'login'); ?>
</li>
This is my user.php controller:
<?php
class User extends CI_controller
{
public function __construct()
{
parent::__construct();
$this->load->model('user_model');
}
public function register()
{
$this->load->library('form_validation');
if($this->form_validation->run() == FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('user/view_register');
$this->load->view('templates/footer');
}
else
{
echo 'this is being processed. thank you';
}
$this->load->view('user/view_register');
}
public function login()
{
$this->load->library('form_validation');
$data['title'] = 'Log In';
$this->load->view('user/view_login');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('user/view_login');
$this->load->view('templates/footer');
{
else
{
$user_id = $this->User_model->check_login($this->input->post('username',
$this->input->post('password');
Here is my User_model:
class User_model extends Model {
function User_model()
{
parent::Model();
}
function check_login($username, $password)
{
$sha1_password = sha1($password);
$query_str = "SELECT user_id FROM users WHERE username = ? and password = ?";
$result = $this->db->query($query_str, array($username, $sha1_password));
if ($result->num_rows() ==1)
{
return $result->row(0)->user_id;
}
else
{
return false;
}
Any help would be immensely appreciated :)
I know you're going to delete your question eventually, as you did with your previous three (that I know of), but hey.
You're doing a lot of things wrong. Let's do some cleanup:
<?php
class User extends CI_controller
{
// constructor is useless here, let's remove it.
function index()
{
redirect('login','refresh');
// this is useless too, but just in case someone navigates to the url 'user'
}
function login()
{
// you are calling form_validation methods, and loading the library _after_.
// It's the other way around.
// Also, no need to load the form helper, it's already loaded
// by the form_validation library
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|trim|max_length[50]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|trim|max_length[200]|xss_clean');
$this->load->view('pages/view_login');
if ($this->form_validation->run() == FALSE) {
$this->load->view('view_login');
{
else {
//extract($_POST);
// this is quite bad practice imho (it's like goring back to register_globals).
// You should fetch the post content directly:
$user_id = $this->user_model->check_login($this->input->post('username',
$this->input->post('password');
}
}
?>
Now, as I said in your previous questions, which all dealt with the SAME EXACT 404 ERROR, I'm going to tell you again: FOLLOW THE MANUAL. Don't invent new ways of using the built-in framework functions. They're there for a purpose.
You should open the form like this:
<?php echo form_open('user/login'); ?>
It will build the correct url by itself. And don't tell "I already did that" again, because you didn't, in fact every question of yours still shows the same mistakes in url building all over again.
I'm sorry to be harsh, I hope something will get grasped eventually.
function user()
{
parent::Controller();
}
should be
function user()
{
parent::__construct();
}
also you are loading
$this->load->view('pages/view_login');
and
$this->load->view('view_login');
check your code, sure these views exists both?
also i really think you can remove all this part:
function index()
{
$this->login();
}
also, you are using index.php in form open, are you removing that from url with htacces? 404 is page not found
This is where your problem could be. I suspect you are confusing your controller name with a subdirectory inside view:
$this->load->view('user/view_login');
Currently, You are loading the view_login.php from a folder/directory known as user. You get a 404 Page Not Found Error if page is not in the referenced directory. It does not exist.
I think this is some error, because user is actually your controller and should be used only from the form as:
<?php echo form_open('user/login'); ?>
except your have view_login.php existing in a subdirectory known as user.
view/
user/
view_login.php
Try putting view_login.php directly into the view folder and use this to call it:
view/
view_login.php
$this->load->view('view_login');
In same vein,
$this->load->view('templates/header', $data);
$this->load->view('user/view_login');
$this->load->view('templates/footer');
Also means that header.php and footer.php are in subdirectories "templates". An error with any of these would give you the 404 error. You might want to try commenting out your load view and test by echoing out some string -- just to prove your codes are all fine.
Verify if you might be having interference from your htaccess file by testing if every other url works well.
[UPDATE]
If you have doubts whether your form action is correct, try using base_url() if that is configured correctly.
<?php echo form_open(base_url().'user/login') ?>
That should accurately point to site/controller/method(function)
Like I mentioned in my comment, I didn't see you use form_validation->rule() anywhere in your code. You could possible check to see if it was false.
Replace your login function with the one below and test:
public function login() {
$this->load->library('form_validation');
$data['title'] = 'Log In';
if ($this->input->post()) {
print_r(); //test if you have values
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
//You could set delimiter
if ($this->form_validation->run() == FALSE) {
//What so ever you which to do if validation fails. { else {
$username = $this->input->post('username'); //Make your code a bit readable
$password = $this->input->post('password'); //Make your code a bit readable
$user_id = $this->User_model->check_login($username, $password);
$data["user_id"] = $user_id; //you can access $user_id from your view
}
}
//Pass $data to views, you might need it in more than one view. Also change directory from user to pages to match what you stated in comment below. And please make sure these views are in the directories specified below...
$this->load->view('templates/header', $data);
$this->load->view('pages/view_login', $data);
$this->load->view('templates/footer', $data);
}
I'm building a small application that will basically display 3 forms... the first one is optional, the second not and the third not. I'd like each form to be a separate url(controller). While reading the documentation for the CodeIgniter form_validation, it appears that the form can only submit to it's self to validate. If that's the case, the forms would keep showing on the same page... he's basically what I have... and commented in what i'd like to do...
class Index extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
//load front page that contains first form...
$content['title'] = 'Home';
$content['view'] = 'pages/index';
$this->load->view('template/default',$content);
}
function step_two()
{
//recieve information from front page. validate form. If validation is
//successful continue to step_two(url) if it fails redirect
//to front page with error...
$this->form_validation->set_rules('serial', 'Serial Number', 'required');
if ($this->form_validation->run() == FALSE)
{
//return to front page...
}else{
//do necessary work and load step_two view
}
}
}
?>
This is a snippet from my idea. But what I'm noticing is that you can't have form validation unless the form submits to it's self. Any ideas? Should I validate the form then just redirect to the new url/function?
Thanks guys...
this is how you do it
Controller -
public function step1()
{
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform1');
}
else
{
//do something to post data
redirect('/controller/step2');
}
}
public function step2()
{
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform2');
}
else
{
//do something to post data
redirect('/controller/step3');
}
}
so answer to your question is yes, you keep these in the same method and redirect on successful validation.