Welcome Guest, Not a member yet? Register   Sign In
Javascript Reload?
#7

[eluser]Corey Freeman[/eluser]
I found this tutorial: http://ad1987.blogspot.com/2009/02/reddi...l-and.html and tried adapting it to codeigniter. Of course, I got nothing...

VIEW
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
&lt;html lang="UTF-8"&gt;
&lt;head&gt;
&lt;title&gt;&lt;?=$title ?&gt; | ReveCloud - Aspiration Sharing&lt;/title&gt;
&lt;meta name="author" content="Minus the Pie Media" /&gt;
&lt;meta name="description" content="goal setting, dream sharing, goal social network, challenge setting" /&gt;
&lt;meta name="description" content="A website that allows you to share your goals and get encouragement to pursue your dreams." /&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?=base_url() ?&gt;style.css" /&gt;
[removed][removed]
[removed]
$(function(){
$("a.vote_up").click(function(){
//get the id
the_id = $(this).attr('id');

// show the spinner
$(this).parent().html("<img src='images/spinner.gif'/>");

//fadeout the vote-count
$("span#votes_count"+the_id).fadeOut("fast");

//the main ajax request
  $.ajax({
   type: "POST",
   data: "action=vote_up&id;="+$(this).attr("id"),
   url: "http://localhost:8888/dreams/index.php/votes",
   success: function(msg)
   {
    $("span#votes_count"+the_id).html(msg);
    //fadein the vote count
    $("span#votes_count"+the_id).fadeIn();
    //remove the spinner
    $("span#vote_buttons"+the_id).remove();
   }
  });
});
});
[removed]
&lt;/head&gt;
&lt;body&gt;
<div id="wrapper">
<div id="header">
<div id="logo">
&lt;?php if ($this->session->userdata('is_logged_in') != TRUE) { ?&gt;
<a href="&lt;?=base_url() ?&gt;"><img src="&lt;?php echo base_url(); ?&gt;images/logo.png" alt="Project 27" /></a>
&lt;?php }
else { ?&gt;
<a href="&lt;?=base_url() ?&gt;/index.php/dashboard"><img src="&lt;?php echo base_url(); ?&gt;images/logo.png" alt="Project 27" /></a>
&lt;?php } ?&gt;
</div>
<div id="user-info">
&lt;?php foreach($query->result() as $row): ?&gt;
<strong><a >id; ?&gt;">&lt;?=$row->username; ?&gt;</a></strong>
&lt;?php endforeach; ?&gt;
</div>
</div>
<div id="container">
<div id="profile-info">
&lt;?php foreach($query->result() as $row): ?&gt;
<img >avatar; ?&gt;" alt="&lt;?=$row->username; ?&gt;" />
<p><h3>&lt;?=$row->username; ?&gt;</h3>
This is where the description goes.</p>
&lt;?php endforeach; ?&gt;
</div>
<table id="goals">
<tbody>
&lt;?php foreach($goals->result() as $row):
$effective_vote = $row->votes;
?&gt;
<tr>
<td>&lt;?=$row->id ?&gt;</td>
<td>&lt;?=$row->image ?&gt;</td>
<td>&lt;?=$row->goal ?&gt;</td>
<td>
<span class='votes_count' id='votes_count&lt;?=$row->id; ?&gt;'>&lt;?php echo $effective_vote." votes"; ?&gt;</span>  
<span class='vote_buttons' id='vote_buttons&lt;?=$row->id; ?&gt;'>  
<a href='[removed];' class='vote_up'>id; ?&gt;'></a>  
</span>
</td>
</tr>
&lt;?php endforeach; ?&gt;
</tbody>
</table>
&lt;?php $this->load->view('footer'); ?&gt;

Votes Controller
Code:
&lt;?php
class Votes extends Controller {

    function Votes() {
        parent::Controller();
    }
    
    function index() {
        $id = $_POST['id'];
        $action = $_POST['action'];
        $cur_votes = getAllVotes($id);
        $votes_up = $cur_votes[0]+1;
        $data = array(
            'votes' => $votes_up
        );
        $r = $this->db->update('goals', $data);
        if($r) //voting done
        {
        $effectiveVote = getEffectiveVotes($id);
        echo $effectiveVote." votes";
        }
        elseif(!$r) //voting failed
        {
        echo "Failed!";
        }
    }
    
    function getAllVotes($id) {
        $votes = array();
        $this->db->where('id', $id);
        $this->db->get('goals');
        if($query->num_rows == 1) {
            $votes[0] = $row['votes'];
        }
        return $votes;
    }
    
    function getEffectiveVotes($id) {
        $votes = getAllVotes($id);
        $effectiveVote = $votes[0];
        return $effectiveVote;
    }
}

From what I understand, the ajax runs the PHP stuff in the browser, then the javascript makes it look all pretty. I assume my problem lies with getting the ajax to run the controller? I adapted it from the tutorial to have only a vote-up function...so I may have messed up there too...

help? Sad


Messages In This Thread
Javascript Reload? - by El Forum - 07-28-2010, 04:03 PM
Javascript Reload? - by El Forum - 07-28-2010, 04:35 PM
Javascript Reload? - by El Forum - 07-28-2010, 04:38 PM
Javascript Reload? - by El Forum - 07-28-2010, 05:14 PM
Javascript Reload? - by El Forum - 07-28-2010, 06:49 PM
Javascript Reload? - by El Forum - 07-28-2010, 07:49 PM
Javascript Reload? - by El Forum - 07-29-2010, 06:21 AM



Theme © iAndrew 2016 - Forum software by © MyBB