page won't show up in codeigniter made website - php

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

Related

CodeIgniter Form is not Submitting

I am new to php and I'm trying to develop a simple project using CodeIgniter php framework. I'm following the tutorial in CodeIgniter website.
My problem is I have create a model,view and controller as the tutorial. But my create form is not submitted when I click the submit button. other functions are working correctly.
I followed below tutorial:
Tutorial
Thanks in advance.
View
<h2><?php echo $title; ?></h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/create'); ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Create news item" />
</form>
Model
<?php
class News_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_news($slug = FALSE) {
if ($slug === FALSE) {
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news() {
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
}
Controller
<?php
class News extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('news_model');
$this->load->helper('url_helper');
}
public function index() {
$data['news'] = $this->news_model->get_news();
$data['title'] = 'Title passes from conttoller';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug = NULL) {
$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() {
$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('news/success');
}
}
}
Routes:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
If you're new with Codeigniter, please dont play with "config/routes.php"
create form is not submitted when clicked the submit button? HTML form should submitted the form by default (browser will refresh)
Please check in the "Inspect Element > Network" at your browser before you click the form, and see what is going on

function view in controller codeigniter

I am working on the news section example as demonstrated in the link below:
http://www.codeigniter.com/userguide2/tutorial/news_section.html
It contains two functions in the controller index and view.
When I remove the view function from my controller even then I get the same output. Can any one of you help me understanding the need of function view in the controller?
<?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');
}
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');
}
}
How CodeIgniter URLS work is the following:
example.com/controller/method/param1[/param2...]
From: http://www.codeigniter.com/userguide2/general/urls.html
When you go to yoursite.com/news, it automatically runs the index() function. But, what if you went to yoursite.com/news/view/1234?
Then it would run your view() function and pass '1234' as a parameter ($slug).

CodeIgniter: Displaying Form Issue

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');

Reason for blank page

<?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');

CodeIgniter News Tutorial delete

I am using CodeIgniter and have completed the news tutorial. I am trying to add a delete and update button within the news section. I have seen your post on stackoverflow about the delete button on this tutorial but i still cant get it working.
Here is my coding that i have used from your post
news_model.php
<?php
class News_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_news($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => $slug));
return $query->row_array();
}
public function set_news()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
}
public function delete_news($id) {
$this->db->delete('news', array('id' => $id));
}
}
CONTROLLER - news.php
<?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');
}
public function view($slug)
{
$data['news'] = $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()
{
$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('news/success');
}
}
public function delete($id) {
$this->news_model->delete_news($id);
$this->load->helper('url');
redirect('/www.shelim786.co.uk/CodeIgniter/news');
}
}
VIEWS - index.php
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p>View article</p>
<p>delete article</p>
<?php endforeach ?>
and ROUTING
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
$route['news/delete/(:any)'] = 'news/delete/$1';
The problem i am having is that the news article in www.shelim786.co.uk/CodeIgniter/index.php/news
doesnt delete when i click the delete button.
i would really appreciate if you could help me.
Thanks,
Shelim
You don't have slash after the word 'delete' in the link. Your link gets displayed something like http://www.shelim786.co.uk/CodeIgniter/index.php/news/delete 6. Notice the space after 'delete'?
In your view, change the 'delete article' link line to this.
<p>delete article</p>
Notice the slash after 'delete'?
you should redirect your delete link delete_news function
not delete function as the delete query is written there

Categories