<?php
class Customer extends CI_controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Customer_model');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Title', 'required');
$this->form_validation->set_rules('address', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->helper('url');
$this->load->view('templates/header');
$this->load->view('master/customer');
}
else
{
$this->Customer_model->register();
$this->load->view('templates/header');
$this->load->view('templates/success');
}
}
}
I have inserted data successfully to the table using this code. After I have tried to display the same header template and the center part (success.php) it is showing a blank page. After commenting the header page it is working I.E. The success page is coming.
I think you need to load the helper also above it:
$this->load->helper('url');
Related
When I load the main page and try to leave the login details blank,I get page you requested is not found.This my code main and login files resp. kindly help
<?php
class Main extends CI_Controller
{
public function index()
{
$this->login();
}
public function login()
{
$this->load->view('login');
}
public function members()
{
$this->load->view('members');
}
public function login_validation()
{
//do validation here,load validation library
$this->load->library('form_validation');
//setting rules for input data
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if($this->form_validation->run())
{
redirect('main/members');
}
else
{
$this->load->view('login');
}
}
}
This means your login.php view does not exist.
You should check your view folder to see if login.php exist there.
I'm trying to have my login controller in my main page, but when I try to login it goes to a page with just the view/login code.
Here is my controller:
public function home(){
$this->load->model("get_products");
$data['results'] = $this->get_products->getData();
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_home", $data);
$this->load->view("content_left");
$this->load->view("content_right");
$this->login();
$this->login_validation();
$this->load->view("site_footer");
}
public function login(){
$this->load->view('login');
}
public function login_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if($this->form_validation->run()){
$this->load->view('logged');
}
}
How can I login in the home page, without loading a new page?
No need of 2 functions, use one function like this,
public function login(){
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|trim');
$this->form_validation->set_rules('password', 'Password', 'required|md5');
if($this->form_validation->run() == FALSE){
$this->load->view('login');
} else {
// here you can redirect to main controller instead of loading the view, since ligin part is related here
$this->load->view('logged');
}
}
And you are calling 2 functions in home()
$this->login();
$this->login_validation();
Both will not get called as you are loading a view in first one.
After following the CodeIgniter's excellent documentation I have a very simple news article system setup which allows anyone to post an article if they go to news/create. I am now trying to implement a simple log in solution which will only allow logged in users to post articles, I have followed this tutorial (http://www.iluv2code.com/login-with-codeigniter-php.html) and have it working as it shown in the tutorial however the issues arise when instead of getting the user to land on the private page once they have logged in I want them to land on news/create.
My attempts to get this working have allowed me to get the user to land on the page but the page then fails to render properly leaving the article submission form not existent. Here is the code which I believe is causing the issue (mainly the 'Create' function):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug)
{
$data['news_item'] = $this->news_model->get_news($slug);
if (empty($data['news_item']))
{
show_404();
}
$data['title'] = $data['news_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('news/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data1['username'] = $session_data['username'];
$this->load->view('news/create', $data1);
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('templates/header', $data);
$this->load->view('news/success');
$this->load->view('templates/footer');
}
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
public function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('news', 'refresh');
}
}
For those whose comments asking about the session, there is a separate controller creating that session (as seen below). This is functioning fine though as on the 'private page' which only logged in users can access it currently displays the username however it fails to render any of the create function after the initial 'is the user logged in or not'.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class VerifyLogin extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('user','',TRUE);
}
function index()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE)
{
//Field validation failed. User redirected to login page
$this->load->view('login_view');
}
else
{
//Go to private area
redirect('news/create', 'refresh');
}
}
function check_database($password)
{
//Field validation succeeded. Validate against database
$username = $this->input->post('username');
//query the database
$result = $this->user->login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array(
'id' => $row->id,
'username' => $row->username
);
$this->session->set_userdata('logged_in', $sess_array);
}
return TRUE;
}
else
{
$this->form_validation->set_message('check_database', 'Invalid username or password');
return false;
}
}
}
?>
Any guidance or help would be greatly appreciated.
After sometime of going through this code I finally arrived at the issue and will post the solution here incase anyone else comes across a similar problem and they can use this as reference.
I was trying to load a view in the initial check to see whether a user was logged on, deleting this line allowed the rest of the solution function as it should.
public function create()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data1['username'] = $session_data['username'];
$this->load->view('news/create', $data1); ----- LINE IN QUESTION
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
i made a controller name of user.php i test complete project on local host it works fine but when i upload online on website it shows error , i submit the form then form load controller called user.php and error on the first line its called parse error i check but still the same will you please suggest.
<?php
class User extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function create_user()
{
// field name, error message, validation rules
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('city', 'City', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$this->load->view('ar_signup');
}
else
{
$this->load->model('Users_model');
if($this->input->post("language")=="ar")
{
$this->load->view('ar_thanks');
}
else
{
$this->load->view('en_thanks');
}
if($query = $this->Users_model->create_member())
{
$this->load->view('ar_thanks');
}
}
}
}
Please format your code and use IDE. And you will not have these errors. Your formatted code:
<?php
class User extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function create_user()
{
// field name, error message, validation rules
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('city', 'City', 'trim|required');
if($this->form_validation->run() == FALSE) {
$this->load->view('ar_signup');
} else {
$this->load->model('Users_model');
if($this->input->post("language")=="ar") {
$this->load->view('ar_thanks');
} else {
$this->load->view('en_thanks');
}
if($query = $this->Users_model->create_member()) {
$this->load->view('ar_thanks');
}
}
}
}
Hello I am pretty new to codeIgniter and I've created a contact page with controller and a view.. the only problem is that I don't get anything when I go to the link http://www.mydomain.nl/index.php/contact
My controller is as follows
<?php
class Contact extends CI_Controller {
public function index()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Contact';
$this->form_validation->set_rules('name', 'Naam', 'trim|required');
$this->form_validation->set_rules('beschrijving', 'Beschrijving', 'trim');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email')
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('contact/index');
$this->load->view('templates/footer');
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('contact/formsucces');
$this->load->view('templates/footer');
}
}
}
with this controller i've also created a folder in my view called contact
in this folder i've got 2 files the index.php and a formsucces.php for when the form is beeing submitted.
My question is what am i doing wrong? i've looked all the examples I could find but nothing helps..
edit: added more code
This is a controller for the news section. the controller and the view is the same as the contact controller and view.
<?php
class News extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('news_model');
}
public function index()
{
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
and to be really really sure that my directory, paths etc are correct here is the print screen of my directory