2

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);
                }
        }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
ageoff
  • 2,798
  • 2
  • 24
  • 39
  • I havent used CI but it sounds like its not checking to see if the button was checked, it just giving you 5 points on refresh. Where is it checking to see if the page was posted? – Drewdin Sep 16 '12 at 04:10

1 Answers1

2

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/

Todd Moses
  • 10,969
  • 10
  • 47
  • 65