Welcome Guest, Not a member yet? Register   Sign In
Doubt about Ci interacting with view javascript (using jQuery)
#1

[eluser]Rubiz'[/eluser]
Hi all!

I'm using jQuery and I need a callback javascript function, but this callback function is called depending of what response CI gives me. If the user is not logged, it responses me non-logged alert; if the rating vote is ok, responses me ok! if you already voted, and you cant vote again, it responses ops! you already voted!!!!

I dont know how make CI interact with view javascript do make those function calls...

Anuone knows?
#2

[eluser]ScottBruin[/eluser]
Are you trying to do this with AJAX? If not, you don't need to use javascript.
#3

[eluser]Rubiz'[/eluser]
Yes, with Ajax, and I dont know hoew posting without refresh page, as it happen on You Tube.

My code view:
Code:
$.ajax({
            url: '<?=base_url()?>item/rating/<?=$item->id?>/' + grade,
            cache: false,
            type: 'POST',
            success: function(html){
                $("#response").html(html);
            }
        });

My controller:
Code:
function rating($id, $grade)
    {
        /* get user id based on the current session */
        $user_email = $this->db_session->userdata('email');
        $user_id    = $this->User_model->get_id_by_email($user_email);
        
        /* deny access to not logged users */
        if (!$user_id || !$this->db_session->userdata('logged_in')) {
            echo "warn_fadeIn('non_logged')";
            return;
        }
        
        /**
         * Update item_rating
         */
        $rated = $this->Item_model->rate($id, $user_id, $grade);


        if ($rated == TRUE)
            echo "warn_fadeIn('rating')";
        else
            echo "warn_fadeIn('already_voted')";
}

Those javascript calls tags only works with firefox.
#4

[eluser]ScottBruin[/eluser]
I would do something more like this:

Code:
$.post(
    url: '<?=base_url()?>item/rating/<?=$item->id?>/' + grade,
    // data { 'itemid': 12345 }   // you could also pass your item ID here as an object array
    function() {
        // Do whatever here with the json array, something like if (item == true) { "warn_fadeIn('rating')" }
        // not clear on json syntax and what jquery returns here but shouldn't be too hard to look up
    },
    "json"
);
#5

[eluser]Rubiz'[/eluser]
I dont know json, I need to bring back 3 messages: logged, vote sucsseeds, you've already voted once, thanx!.

Mabe I can bring the logged message to view by vars eliminating 1 function call. But I've never worked with json. How it works?
#6

[eluser]Rubiz'[/eluser]
A friend of mine helped me.

View:
Code:
function vote(grade)
    {
        var logged = <?=$logged ? "true" : "false"?>;
        
        if (!logged)
        {
            warn_fadeIn('non_logged');
            return;
        }
        else
        {
            $_postJSON( '<?=base_url()?>item/rating/<?=$item->id?>/' + grade,     { }, function(json){
                        if (json.success == true)
                        {
                            warn_fadeIn('vote_succeed');
                            $( '#vote_num_logged' ).html( '<strong>Votos:</strong> ' + json.rate );
                        }
                        else warn_fadeIn('already_voted');
                    });
        }

Controller:
Code:
function rating($id, $grade)
    {
        /**
         * Identifying user
         */
        $user_email = $this->db_session->userdata('email');
        $user_id   = $this->User_model->get_id_by_email($user_email);
        
        /**
         * Update item_rating
         */
        $rated = $this->Item_model->rate($id, $user_id, $grade);

        /**
         * Get new rating
         */
        $rating = $this->Item_model->get_rating($id);
        $rating = $rating->votes;


        if ($rated == TRUE)
            echo "{success:true, rate:" . $rating . "}";
        else
            echo "{success:false}";
        
        return;
    }




Theme © iAndrew 2016 - Forum software by © MyBB