Codeigniter form submission - php

So I'm rather new to Codeigniter...
I have a file on my local host that submits a form:
$id =array('id' => 'commentForm');
echo form_open("form_validate", $id);
public function form_validate()
{
$this->load->library("form_validation");
$this->form_validation->set_rules('date', 'Date', 'required');
$this->form_validation->set_rules('Cname', 'Client Name', 'required');
....
}
From what I understand is that when I hit submit on the form, it goes to the function "form_validate()"(or whatever you set at the submission page) in the controller and executes that.
The Code works just fine on my localhost, however once I upload it to my live server, instead of going to the function in the controller it looks for a page called form_validation(at least thats what I think from the URL)
I've rewritten my base path as:
$config['base_url'] = 'http://mysite.com/ci_form';
And gotten rid of the index.php under index
I've also taken the Codeigniter mod_rewrite form their site
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ci_form/
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php

You're missing your controller in the form_open("form_validate"); function, that's why it's looking for a controller called form_validate.
It needs to be form_open("your_controller_name/form_validate");
http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html
Also your forms should be in your views
Maybe this will help

Related

PHP CodeIgniter (how to make public folder)

I'm a new in CodeIgniter.
I made public folder, where I want to put my css/js/images folders.
My setup is like that:
application
cache
config
controllers
public
css
js
images
....
....
....
My .htaccess is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /moviesmvc/
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
how can I make this public folder public?
/////////////////////////
You really don't want to put your public folder inside your application folder. Your directory structure should be like this
-- application
-- system
-- public
-- css
-- js
-- images
-- index.php
-- .htaccess
This is the best practice to follow and you will save yourself a lot of time and potential problems in the future
If it's a Linux server then you need to change the folder permissions to 755 or 777. Have a Google around for this and you will get a lot more detail.
I would recommend not using CI at the minute as it isn't being developed on any more until they find a new owner. Instead, I would recommend Laravel as it's such an amazing framework, but you should also try out CakePHP, Symfony, Yii or something else which is still being developed.
Check Your Public Folder Permission as well as use base_url() function to get the public url..
for example
echo base_url('public/images'); to get image public path
I would not advise to implement this type of design and here is why:
Web servers are designed to not allow public access below the document root. This aides in both ease-of-setup and security.
If a major security change is made within the root then it has to be duplicated to your public folder.
You are adding unnecessary processing to EVERY HTTP/HTTPS call to your web server because now the rules have to be processed for every request. If you could build this into your httpd.conf file instead then the overhead is nearly non-existent.
TL;DR;
No need to re-invent the wheel by adding a lump around the perimeter.

CodeIgniter .htaccess links

I am new to codeIgniter and .htaccess stuff.
I already made to remove the index.php to localhost/ci/index.php/site/home.
So my home page can access now to localhost/ci or localhost/ci/site/home.
I have
Home and About
I can access Home and About if I Am on this link localhost/ci/site/home.
But once I'm on localhost/ci the problem exists because when I click to About the site is redirecting me to localhost/ci/about instead of localhost/ci/site/about. I change the links to Home and About the browser keeps adding the /site every click like
localhost/ci/site/site/site/site/site/site/home
Anyone can help me to fix the problem?
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ci
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
Have you looked at the URL Helper? It has many functions which will help in these situations.
First make sure you load the helper either in your Controller or autoload configuration.
anchor()
The anchor() helper would be ideal in this instance. e.g.
<?php echo anchor('home', 'Home'); ?>
base_url()
Or alternatively you could build up the href using base_url as previously mention.
About
Go to your config file and set index_page to be empty.
This will remove the index.php
and when you write links set / infront as :
Home and About
what #Svetlio said + i advise you to use site_url() when creating links
You're re-adding the main controller even though the htaccess is set to see that as the base. Writing your links as follows should fix the issue.
About
The other way around it is to run off the base at all times, which isn't really necessary but can work if you're having issues otherwise.
About
What that is going to do is echo out whatever you have set as the base URL in your config every time you write a link so if your base_url in your config is www.localhost.com the above will write www.localhost.com/about.
Don't worry you'll get the hang of it, the routing is probably the most complicated part of CI when you first start.
Home
About

routing/rewrites/removing index.php: Only works for default_controller, but not for the rest of the views

Followed a tutorial to remove index.php from the URLs in Code Igniter.
It works now for my default_controller, but not for other pages(or views) properly.
I have one controller, Pages, and it has one method, View($page = 'home'), to load pages with other content based on the parameter passed.
If I type localhost/dev into URL - I land on my home page, which is correct.
If I type localhost/dev/aboutus - I receive 404. It only works if I type localhost/dev/pages/view/aboutus.
What I wanted to happen is by typing localhost/dev/aboutus it would show me the AboutUs view.
Routes.php
$route['default_controller'] = "pages/view";
$route['dev/(:any)'] = "dev/pages/view/$1";
$route['404_override'] = '';
Pages.php (controller)
<?php
class Pages extends CI_Controller
{
public function view($page = 'home')
{
if (!file_exists('application/views/pages/' . $page . '.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/' . $page);
$this->load->view('templates/footer');
}
}
?>
.htaccess file (located in /dev/ folder)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dev/
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
This route $route['dev/(:any)'] = "dev/pages/view/$1"; is a little fishy because CodeIgniter routes should not include the project name (dev in your case). They begin on the controller level, not the project one.
The route you have written would imply that if a user typed http://localhost/dev/dev/something (yes, two dev's) they would be routed to a controller dev.php and into a function with prototype: function('view', 'something'){}
Why so complicated? You should make all of your main page names into controllers. Routes should only be touched for special cases.
Create a file aboutus.php in application/controllers with contents:
<?php
class Aboutus extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
"I'm in the about controller!";
//$this->load->view("some_view") ;
}
}
You can access it with http://localhost/dev/aboutus
In your config.php leave it blank if you haven't
$config['index.php'] = '';

Codeigniter - cannot access controller while I have a default controller set

I have two controllers in the same folder. One is named criteria, and one is named reviewApprove. Their code is identical (other than the class name).
If I set criteria to the default controller in routes, then I am able to access it, but no matter what I cannot navigate to the reviewApprove controller.
If I don't set a default controller and navigate to index.php/reviewApprove, or index.php/criteria.. that will work just fine.
If I set reviewApprove to the default controller I can see it as well.
My htaccess file is set. I have compared my project to a few other code igniter projects and can't tell what I'm doing wrong.
Anyone have any ideas?
criteria controller
class Criteria extends APN_Controller {
public function __construct() {
parent::__construct();
$this->load->vars(array('nav' => array('','', 'current', '', '')));
}
public function index(){
$this->_set('title', "Certified Analytics");
$this->_set('js', "/resources/authenticated/js/coverage");
$this->_set('css', "/resources/authenticated/css/enroll");
$this->load->view('authenticated/header/header', $this->_data);
$this->load->view('authenticated/content/coverage');
$this->load->view('authenticated/footer/footer');
}
}
reviewApprove controller
class ReviewApprove extends APN_Controller {
public function __construct() {
parent::__construct();
$this->load->vars(array('nav' => array('','current', '', '', '')));
}
public function index(){
$this->_set('title', "Certified Analytics");
$this->_set('js', "/resources/authenticated/js/criteria");
$this->_set('css', "/resources/authenticated/css/enroll");
$this->load->view('authenticated/header/header', $this->_data);
$this->load->view('authenticated/content/reviewApprove');
$this->load->view('authenticated/footer/footer');
}
}
routes
$route['default_controller'] = "criteria";
htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /certifiedAnalytics/
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Is anything else necessary?

Site developed in Codeigniter only loads home page

I'm trying to move a site developed in Codeigniter from one host to another. The only difference between hosts is that the server the site is moving from is a Windows server (with PHP etc. installed) and the new server is Linux.
However, when I uploaded the site, and changed all the url references, the site only loads the home page.
The address of the new site is http://pioneer.xssl.net/~admin341/
There is a htaccess file, which reads as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
The lines in config.php that relate to the url are:
$config['base_url'] = "http://pioneer.xssl.net/~admin341/index.php/";
$config['index_page'] = "";
Plus the developer (my predecessor) coded a url helper file:
function base_url($includeIndexPHP = true)
{
if($includeIndexPHP)
{
$CI =& get_instance();
return $CI->config->slash_item('base_url');
}
else
{
return 'http://pioneer.xssl.net/~admin341/';
}
}
Any ideas would be appreciated. Thanks.
You may well need to change the $config['uri_protocol'] around to get it working on your live site. This is often the cause of the "homepage only" routing issue in CodeIgniter.
The most common is REQUEST_URI but sometimes PATH_INFO works better.

Categories