Wątek przeniesiony 2015-10-28 14:33 z Webmastering przez dzek69.

Głosowanie w javascript.

0

Witam,

Mam taki kod:

Plik z materiałem do oceniania:

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>                            
                            
                            <script>
                               $(document).ready(function () {
                                   $("#demo3 .stars").click(function () {
                                    
                                     $.post('rating.php',{rate:$(this).val()},function(d){
                                         if(d>0)
                                         {
                                             alert('Juz glosowales !');
                                         }
                                     });
                                     $(this).attr("checked");
                                      
                                   });
                               });
                           </script>
                       
                      <fieldset id='demo3' class="rating">
                       <input class="stars" type="radio" id="star53" name="rating" value="5" />
                       <label class = "full" for="star53" title="Niesamowity"></label>
                       <input class="stars" type="radio" id="star4half3" name="rating" value="4.5" />
                       <label class="half" for="star4half3" title="Bardzo dobry"></label>
                       <input class="stars" type="radio" id="star43" name="rating" value="4" />
                       <label class = "full" for="star43" title="Dobry"></label>
                       <input class="stars" type="radio" id="star3half3" name="rating" value="3.5" />
                       <label class="half" for="star3half3" title="Dostatecznie dobry"></label>
                       <input class="stars" type="radio" id="star33" name="rating" value="3" />
                       <label class = "full" for="star33" title="Taki sobie"></label>
                       <input class="stars" type="radio" id="star2half3" name="rating" value="2.5" />
                       <label class="half" for="star2half3" title="Malo porywający"></label>
                       <input class="stars" type="radio" id="star23" name="rating" value="2" />
                       <label class = "full" for="star23" title="Niezbyt ciekawy"></label>
                       <input class="stars" type="radio" id="star1half3" name="rating" value="1.5" />
                       <label class="half" for="star1half3" title="Nie ciekawy"></label>
                       <input class="stars" type="radio" id="star13" name="rating" value="1" />
                       <label class = "full" for="star13" title="Tragiczny"></label>
                       <input class="stars" type="radio" id="starhalf3" name="rating" value="0.5" />
                       <label class="half" for="starhalf3" title="Nie ogladac"></label>
                   </fieldset>

rating.php

 require_once "cconfig.php";
 
    if($_SERVER['HTTP_CLIENT_IP'])
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    else if($_SERVER['HTTP_X_FORWARDED_FOR'])
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
 
 
if (isset($_POST['rate']) && !empty($_POST['rate'])) {
 
    $rate = $polaczenie->real_escape_string($_POST['rate']);
 
    $sql = "SELECT `id` FROM `tbl_rating` WHERE `user_id`='" . $ip . "'";
    $result = $polaczenie->query($sql);
    $row = $result->fetch_assoc();
    if ($result->num_rows > 0) {
        echo $row['id'];
    } else {
 
        $sql = "INSERT INTO `tbl_rating` ( `rate`, `user_id`) VALUES ('" . $rate . "', '" . $ip . "'); ";
        if (mysqli_query($polaczenie, $sql)) {
            echo "0";
        }
    }
}

Nie znam się za bardzo na javasript, dlatego mam pytanie.

  1. Jak przesłać $_POST do rating.php również id ocenianego materiału, który jest zapisany w zmiennej $id ?
  2. W zmiennej $ocena mam aktualną ocenę materiału, jak podświetlić odpowiednią ilość gwiazdek ?
1
//wrzucasz do tego swojego eventa, gdzie masz kliknięcie na gwiazdkę.
var mat = $(this).parent().attr('id');

//później ajaxem przesyłasz:
{rate:$(this).val(), material: mat}
0

Nie załapałem, mógłbyś pokazać dokładniej gdzie wstawić

var mat = $(this).parent().attr('id');

?

1

Wewnątrz metody click.

1 użytkowników online, w tym zalogowanych: 0, gości: 1