0

我正在开发电子商务应用程序。用户可以对产品进行评分,产品评分应通过用户的每次单击来计算。我想从 jquery 中保存星级评分结果,并将其存储在数据库中。有没有可能的存储方式?

控制器代码:

function rating()
    {
        $rating_value = $this->input->post("rate_val", true);
        $designId = $this->input->post("id", true);
        $user = $this->model_name->getUser($this->session->userdata('user_name')); //session user details
        $rating_date = date("Y-m-d");

        if ($user['id'])   //logged in user id
        {
            if (!$this->model_name->is_design_rated($designId , $user['id']))
            {
                if ($this->model_name->insert_rating($designId, $user['id'], $rating_value, $rating_date))
                {
                    echo json_encode(array("code" => "Success", "msg" => "Your Design Rating has been posted"));
                }
                else
                {
                    echo json_encode(array("code" => "Error", "msg" => "There was a problem rating your design"));
                }
            }
            else
            {
                echo json_encode(array("code" => "Error", "msg" => "You have already rated this design"));
            }
        }
        else
        {
            echo json_encode(array("code" => "Error", "msg" => "You have to login to rate the design"));
        }
        exit(0);
    }

查看文件:

<p id="msg_rate"></p>

<input type="hidden" name="hidden_design_id" id="hidden_design_id" value="<?php echo $designId;?>"/>

  <?php 
        if (!$this->usermodel->isUserMember()) //Check if user logged in
        {
            $radio_level = "disabled";
        }
        else
        {
            $radio_level = " ";
        }

        for($i = 1;$i <= 5;$i++)
        {
            if ($i == round($avgVotes['rating_value']))
            {
           ?>
                <input class="auto-submit-star" type="radio" name="rating" <?php echo "$radio_level";?> value="<?php echo $i;?>" checked="checked"/>
           <?php
            }
            else
            {
           ?>
               <input class="auto-submit-star" type="radio" name="rating" <?php echo "$radio_level";?> value="<?php echo $i;?>"/>
           <?php
            }
        } //end of for
    ?>

js代码:

$('.auto-submit-star').rating({
               required: true,
               callback: function(value, link) {

                $.ajax({
                         type: "post",
                         url: baseurl + "designs/rating",
                         dataType: "json",
                         data: { id: $("#hidden_design_id").val(), rate_val: value } ,

                  success: function(e) {
                       //$.jGrowl(e.code + "
" + e.msg);
                             //alert(e.code + "
" + e.msg);
                             $('#msg_rate').html(e.msg);
                             $('#msg_rate').fadeIn();
                             $('#msg_rate').fadeOut(5000);
                             window.location.reload();
                   }
             });
         }
    });
4

1 回答 1

0

你可以做这样的事情

$("#msg_rate").raty({ 
    half: true,
    score: function() {

    },
    click: function(score, evt) {      
      $.ajax({
           type     : 'POST',
           url      : base_url+'controller_name/function_name',
           data     : {'id':$("#hidden_design_id").val(), 'rate_val':score},
           success  : function(resp){
                //alert(resp);
                if(resp == "1"){
                    alert("successfull");  
                }else{
                    alert("not successfull");
                }

           },
           error: function(resp){
                alert("error");
           }  
        });              
      }
});

如果您遇到任何问题,请告诉我

于 2013-08-01T12:30:04.980 回答