Ok, so i have a test page right now that is just testing my point system. Pretty much when the user his the button, it gives them 5 points. The problem right now is everytime i refresh the page the user gets 5 points. What is up with this? If necessary I can post the libraries and models functions. Here is the controller and view:
welcome.php view:
Hi, <strong><?php echo $username; ?></strong>! <?php echo $fullname ?> is now logged in now. </br>
You have <?php echo $points; ?> Points.</br>
<?php echo anchor('/auth/logout/', 'Logout'); ?></br>
<?php echo form_open('add_points'); ?>
<?php echo form_submit('/auth/add_points/', 'Get 5 free Points!'); ?>
<?php echo form_close(); ?>
<?php echo var_dump($pointhistory); ?>
welcome.php controller:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library('tank_auth');
$this->lang->load('tank_auth');
}
function index()
{
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$data['fullname'] = $this->tank_auth->get_fullname();
$data['points'] = $this->tank_auth->get_points();
$data['pointhistory'] = $this->tank_auth->get_point_history();
$this->load->view('welcome', $data);
}
}
}
To fix what you have, just check in you controller if the page has a post submitted to it and not just a refresh:
You could use this:
if($_POST)
{
// do something
}
OR
A better way would be to use an AJAX call and not refresh the page at all. This would probably be the best solution.
To do an AJAX post in Codeigniter, you just need to call a public function in your controller and echo the value to return, I would use JSON.
jQuery makes this really easy on the client side: http://api.jquery.com/jQuery.ajax/
Related
I'm trying to get my head around custom URIs. I would like my view_county to have the URL:
http://localhost/chipadvisor2/controller_ca/all_county/1
and my view_all to have the URL:
http://localhost/chipadvisor2/controller_ca/all_chippers
If anyone can help me I would greatly appreciate it. The link works for view_county but when I click on the view_all link it shows:
{
"status": false,
"error": "Unknown method"
}
and if I click into view_county first then the view_all link it just keeps adding controller_ca/all_chippers to the URL and doing nothing. Any ideas why?
model_ca
<?php
class Model_ca extends CI_Model {
function __construct() {
parent::__construct();
}
function get_chipper() {
$query = $this->db->get('chipper_reviews');
return $query->result();
}
function get_county($id) {
$this->db->where('location', 'Westmeath');
$query = $this->db->get('chipper_reviews');
return $query->result();
}
}
?>
controller_ca
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'/libraries/REST_Controller.php';
class Controller_ca extends REST_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model('model_ca');
}
public function index_get() {
$this->all_chipper_get();
}
public function all_chipper_get() {
$data['chipper'] = $this->model_ca->get_chipper();
$this->load->view('view_all', $data);
}
public function all_county_get() {
$id = $this->uri->rsegment(3);
$data['location'] = $this->model_ca->get_county('location',$id);
$this->load->view('view_county', $data);
}
}
?>
view_county
and
view_all
<html>
<head> </head>
<body>
<div class="container">
View All Chippers
<br/>
View County
<?php echo "<br/><br/>";
foreach ($chipper as $chipperdata) {
echo "<b>Name: </b>".$chipperdata->name." "."<br/>"."<b>County: </b>".$chipperdata->location." "."<br/>"."<b>Description: </b>".$chipperdata->description." "."<br/>"."<br/>";
} ?>
</div>
</body>
</html>
I have added this to the route.php file also but don't know how to put it into effect or how to add the value 'westmeath' to the end. Ideally I would like to pass it as a variable rather than hardcoding it:
$route['default_controller'] = 'Controller_ca';
$route['404_override'] = '';
$route['translate_uri_dashes'] = false;
$route['all_chipper'] = 'controller_ca/all_chippers';
$route['all_county'] = 'controller_ca/all_county/$1';
Try to give controller method name same as url. Change all_chipper_get() to all_chipper(). Same for other method and check.
Regarding the issue:
and if I click into view_county first then the view_all link it just
keeps adding controller_ca/all_chippers to the URL and doing nothing.
Any ideas why?
Please add base_url() at the starting of URL in anchor tag. It will resolve the append issue in URL. Example:
View All Chippers
<br/>
View County
Also, change your routing as:
$route['controller_ca/all_county'] = 'controller_ca/all_county/$1';
$route['controller_ca/all_chipper'] = 'controller_ca/all_chippers';
I am a newbie to Codeigniter. As part of studying it, I created a form which inserts data to the database. After insertion it is not redirecting properly to the form view page (formvalidation.php).
Form Action
public function submitform()
{
$formdata['username']=$this->input->post('username');
$formdata['address']=$this->input->post('address');
$formdata['state']=$this->input->post('state');
$formdata['country']=$this->input->post('country');
$data['state']=$this->getstatearray();
$data['country']=$this->getcountries();
if($this->userprofile_model->setdata($formdata)=='success')
{
$this->load->view('formvalidation/formvalidation',$data);
}
else
{
$this->load->view('formvalidation/formvalidation',$data);
}
}
Form view page (formvalidation.php)
echo form_open('formvalidation/submitform');
echo form_input('username');
echo "<br>".form_textarea("address");
echo "<br>".form_dropdown('state',$state);
echo "<br>";
echo form_dropdown('country',$country);
echo "<br><br>".form_submit('submit','Submit');
echo form_close();
Model (userprofile_model.php)
class userprofile_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function setdata($data)
{
$this->db->insert('userprofile',$data);
if($this->db->affected_rows()>0)
{
echo "success";
}
else
{
echo "fail";
}
}
}
Now the value is getting inserted into the database. But after insertion I want to redirect it to the url http://www.example.com/codeigniter/index.php/formvalidation/. But instead, it is now redirecting to http://www.example.com/codeigniter/index.php/formvalidation/submitform where submitform is the form action method.
How can I solve this problem? I tried with putting exit; after the page redirect function. But it won't work. Please help me to fix this.
Thanks in advance.
yes it will, because there is no redirect code. When the form is submitted your submitform function is executed and in that function you are loading the view. So if you want to redirect
to something else you must use the redirect() function.
public function submitform()
{
$formdata['username']=$this->input->post('username');
$formdata['address']=$this->input->post('address');
$formdata['state']=$this->input->post('state');
$formdata['country']=$this->input->post('country');
$data['state']=$this->getstatearray();
$data['country']=$this->getcountries();
if($this->userprofile_model->setdata($formdata)=='success'){
redirect("Your URL");
}
else{
redirect("Your URL");
}
}
Also you must properly sanitize the user input. You must validate your form inputs before passing it to model. You can find more info here
First Load the view page in controller index function.
function index()
{
$this->load->view('v_bank_master');
}
Then in model page, after insertion done. give the below code.
redirect('give your controller page name here','refresh');
Your page will now redirect to the view page after insertion.
Hope it will help you...
I use redirect('yourControllerName', 'refresh').
Read more here: Redirect()
After insertion give the below code
redirect('controller_name/function_name');
I am working on a basic application using the CI framework.
I have the following error:
404 Page Not Found
The page you requested was not found.
Posted below are my code files.
My Controller code:
class Contact extends CI_Controller{
function _Contact(){
parent::CI_Controller();
}
/*function main(){
$this->load->model('contact_model');
$data = $this->books_model->general();
$this->load->view('books_main',$data);
}*/
function input(){
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('contact_model');
if($this->input->post('mysubmit')==true){
$this->contact_model->entry_insert();
}
$data = $this->contact_model->general();
$this->load->view('contact_input',$data);
}
}
Then in Model I have the following code:
class contact_model extends CI_Model{
function _contact_model(){
parent::Model();
$this->load->helper('url');
}
function entry_insert(){
$this->load->database();
$data = array(
'name'=>$this->input->post('title'),
'address'=>$this->input->post('author'),
'year'=>$this->input->post('year'),
);
$this->db->insert('contact',$data);
}
function general(){
$data['base'] = $this->config->item('base_url');
$data['name'] = 'Name';
$data['address'] = 'Address';
$data['year'] = 'Year';
$data['years'] = array('2007'=>'2007',
'2008'=>'2008',
'2009'=>'2009');
$data['forminput'] = 'Student Registration';
$data['fname'] = array('name'=>'name',
'size'=>30
);
$data['faddress'] = array('name'=>'address',
'size'=>30
);
return $data;
}
}
Finally, my View:
<html>
<head>
</head>
<body>
<div id="header">
<?php $this->load->view('contact_header'); ?>
</div>
<?php echo heading($forminput,3) ?>
<?php echo form_open('books/input'); ?>
<?php echo $name .' : '.
form_input($fname).br(); ?>
<?php echo $address .' : '.
form_input($faddress).br(); ?>
<?php echo $year .' : '.
form_dropdown('year',$years).br(); ?>
<?php echo form_submit('mysubmit','Submit!'); ?>
<?php echo form_close(); ?>
<div id="footer">
<?php $this->load->view('contact_footer'); ?>
</div>
</body>
</html>
Can any one please help me?
Remove this in your Contact Controller:
function _Contact(){
parent::CI_Controller();
}
Replace with this:
function __construct(){
parent::__construct();
}
And in your Contact Model remove this:
function _contact_model(){
parent::Model();
$this->load->helper('url');
}
Replace it with this:
function __construct(){
parent::__construct();
$this->load->helper('url');
}
Hey,
I often had similar Problems. Usually I check the following:
Check the Config file if the correct siteroot is set. I often had live server stuff in there.
Check in the .htaccess if the RewriteBase is set to the correct directory.
to make sure its reading the correct value an echo base_url(); (if this works its usually the .htaccess rewrite base).
Hope it helps.
r n8m
[enter link description here][1]
[1]: http://ellislab.com/forums/viewthread/105880/
hmc
If you have developed your application on Windows, you might have not set first character of Controllers and Models name as a capital character.
like /controllers/home.php to /controllers/Home.php
On Linux file names are case sensitive.
Note:- This is a solution to a possible problem. There may be issues of mis-configuration, server and path variables.
Possible Duplicate: CodeIgniter "The page you requested was not found." error?
I'm having a hard time debugging to locate the issue here.
I've tried echoing $login_by_username and $login_by_email but they aren't echoing.
The problem is when it loads the login form it always puts email as the label.
Controller:
$login_by_username = $this->config->item('login_by_username', 'config1');
$login_by_email = $this->config->item('login_by_email', 'config1');
$this->data['login_by_username'] = $login_by_username;
$this->data['login_by_email'] = $login_by_email;
View:
<?php
if ($login_by_username AND $login_by_email)
{
$login_label = 'Email or Username';
}
else if ($login_by_username)
{
$login_label = 'Username';
}
else
{
$login_label = 'Email';
}
?>
<?php echo form_label($login_label, 'login'); ?>
Config:
$config['login_by_username'] = TRUE;
$config['login_by_email'] = TRUE;
According to our comments.
You have used the autoload.php to call your config file.
You made some tests to make sure that the problem is with your config1.php file and not with CI.
I've made a test and here is what I did.
I have created a file config1.php inside: application/config.
Here it is:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['teste'] = 'config1 test';
On my autoload.php also inside application/config:
$autoload['config'] = array('config1');
My Home Controller:
public function index()
{
$data = array('config'=>$this->config->item('teste'));
$this->load->view('welcome_message', $data);
}
My view:
<?php echo $config;?>
And config1 test is echoed as expected.
Make sure that config items login_by_username and login_by_email are there in the config1.php file
Make sure that you are passing $this->data while loading the view eg:
$this->load->view('view.php', $this->data)
My View :-
<html>
<?= link_tag(base_url().'css/simple.css'); ?>
<body>
<?php $this->load->helper('form'); ?>
<?php $this->load->view('commentform'); ?>
<?php $id=$this->uri->segment(3);?>
<?php echo $id;?>
</body>
</html>
i would like to use the variable $id in my controller.I'm using codeigniter by the way, and am a beginner. I would appreciate any help on this.
you should not call the $id from the View, you should get it at the controller level and pass it to the View.
as Bulk said. you URL will be something like that
www.mysite.com/thecontrollername/thefunction/id
for example your controller if home and there is a show_id function in it and your view is call show_id_view.php.
you will have your url like this: www.mysite.com/home/show_id/id
your function in home will read the id"
in your home controller:
function show_id(){
$id=$this->uri->segment(3);
$view_data['id'] = $id;
$this->load->view('show_id_view',$view_data);
}
in the view (show_id_view):
<?php echo $id ?>
nothing else..
hope this helps.
Well, ideally you wouldn't do it this way. You should assign the variable first in the controller and pass it to the view if you need to use it there.
$data['id'] = $this->uri->segment(3);
$this->load->view('view_file', $data);
$id would then be available in your view as well.
in my view i add link
<li><a href="<?php echo base_url()?>link/show_id/mantap"> coba </li>
in my link.php controler i add function show_id
function show_id(){
$id=$this->uri->segment(3);
$data['coba'] = $id;
$this->mobile->view('**daftarmember_form**',$data);
in my next view daftarmember_form.html
)
<?php echo $id;?>
they print mantap,